You can ask a model to output an answer in a structured, step-by-step way, to guide the user through the solution. The published example defines a Step schema with an explanation and an output, then a MathReasoning schema with a list of steps and a final_answer, and the model fills both. Keeping the steps and the final answer in separate fields means code can display the reasoning and check the answer independently.
What you need
- A problem that needs step-by-step working, such as a maths question
- A nested schema: a Step object inside a reasoning object
- Code that reads the final answer separately from the steps
Step 1: Define the nested schema
Define a Step with an explanation string and an output string, then a parent schema with a list of steps and a final_answer string. Mark both fields required.
Step 2: Ask for step-by-step reasoning
Send the problem with the schema as the response format and a system instruction to guide the user through the solution step by step.
Step 3: Check the final answer against the steps
Read the steps in order and confirm the final answer follows from them. If a step contradicts the final answer, trust neither until you have checked the working.
Worked example: synthetic structured maths reasoning
The table below is a synthetic example. It shows the expected result of a nested-schema reply; no API call was made.
| Field | Value | Check |
|---|---|---|
| Step 1 | Subtract 7 from both sides | Pass |
| Step 2 | Divide both sides by 8 | Pass |
| final_answer | x = -15 / 4 | Follows from the steps |
| Steps count | 5 steps | Complete |
Expected result (UNTESTED). the reply has 5 steps that lead to a final answer of x = -15 / 4, and the answer follows from the steps. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- Structured reasoning is still model output; the steps can be wrong even when they look orderly.
- Long reasoning can hit the maximum output token limit; reserve room for the final answer.
- Check the final answer against the steps, not just the steps against each other.
- Chain-of-thought output guides the user; it does not prove the answer is right.
