DSPyDSPy 3.3 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson

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.

Example
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.

Example
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.

Models that reason on their own
Reasoning models such as OpenAI's o-series think before answering without being asked. For those, DSPy's dspy.Reasoning output type reads the provider's reasoning instead of asking for a text field.
Try it yourself
  • Run ChainOfThought on "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.