ChainOfThought: asking for reasoning first
ChainOfThought runs the same signature but adds a reasoning output before the others, so the model writes its reasoning before the answer.
think = dspy.ChainOfThought(Triage)
result = think(ticket="I want my money back")
print(result.reasoning)
print(result.category)The signature is still Triage, but the result has a reasoning field too. The stand-in's reasoning states what it did: no keyword matched, so it guessed.
think = dspy.ChainOfThought(Triage)
print(list(think.predict.signature.output_fields))
messages = dspy.ChatAdapter().format(think.predict.signature, demos=[], inputs={"ticket": "I want my money back"})
print(messages[-1]["content"])ChainOfThought is a small module: it holds a Predict whose signature has reasoning added as the first output. Because outputs are written in order, the model produces its reasoning before it commits to a category.
Does it help?
That depends on the model and the task, and it costs more output tokens on every call. Large models often get multi-step problems right more often with reasoning first. Small models can do worse, talking themselves into a wrong label. The way to decide is to measure both on the same tickets, which lesson 14 does.
dspy.Reasoning output type reads the provider's reasoning instead of asking for a text field.- Run
ChainOfThoughton"I was charged twice"and read the reasoning. - Print
think.predict.signature.instructions. Did it change? - Use
dspy.ChainOfThought("question -> answer")with the mock from lesson 2. Why does parsing fail?
You understood something today that you didn't yesterday.