An instruction is prompt text that tells a language model what task to perform or how to perform it. For an intent classifier, “Classify the message into one of the possible labels” states the task; adding what each label means can clarify distinctions that label names alone cannot capture. section 5.1 section 6.1.2
In DSPy, a signature supplies a program’s starting instructions. An unoptimized class-based signature uses its docstring; without one, DSPy falls back to a description of the input and output fields. Instructions appear in the System message after the field descriptions and before demonstrations and the current task. You can inspect them with `print(intent_classifier.signature.instructions)`. section 6.1.2
There are two practical ways to improve instructions:
- Write a useful signature docstring rather than relying on field names alone. It provides a stronger starting point even if an optimizer later rewrites it. section 6.1.2
- Evaluate optimized alternatives. DSPy treats instruction optimization and demonstration selection as distinct processes; MIPROv2 can generate candidate instructions and combine them with demonstrations. section 5.1 section 7.6.2
Clarifying intent labels
“Flight” can be defined as queries about available flights and their details, while “Booking” covers requests to book or modify a reservation. This adds a classification rule where short label names leave room for ambiguity. section 5.1
Listing 7.11 Custom module to add a closing section
from dspy.primitives.module import Module
from dspy.signatures.signature import Signature, ensure_signature
from typing import Any
class Closer_Example(Module):
def __init__(self, signature: str, **config: dict[str, Any]):
super().__init__()
signature = ensure_signature(signature)
signature.instructions += " Answer very carefully."
self.predict = dspy.Predict(signature, **config) #1
def forward(self, **kwargs):
return self.predict(**kwargs)
async def aforward(self, **kwargs):
return await self.predict.acall(**kwargs)
prog = Closer_Example("question -> answer")
prog(question="What is the capital of Spain?")
print(lm.inspect_history(n=1))
The Python custom module appends “Answer very carefully.” to `signature.instructions` before creating a `dspy.Predict` instance. This is a direct way to add a closing direction, though an optimizer may later rewrite it. section 7.4.1




