A baseline for prompt optimization is the starting point that added instructions can strengthen. Prefer generally useful additions; extensive manual tweaks can drift into prompt engineering. section 7.4.1
Adding a closing instruction
A DSPy module can append “Answer very carefully” to a signature before prediction. An optimizer may rewrite the instruction, so manual additions should be useful beyond a single prompt. section 7.4.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))
Shows a custom module that adds a closing instruction to a signature and delegates prediction to DSPy. section 7.4.1















