Sub topic 15
Tool Calling Versus Structured Output
This chapter assumes you know chat messages, JSON Schema, and the tool-calling loop from earlier chapters. It explains when to use a tool call and when to ask for structured output instead.
Two ways to get JSON from a model
Both features make the model produce JSON that follows a schema. They differ in who acts on that JSON.
- Tool calling: the JSON is a request. The model proposes a function call, your code runs it, and the result goes back to the model. The conversation continues.
- Structured output: the JSON is the answer. The model's final reply must match your schema, and your code reads it directly. Nothing is executed on the model's behalf.
In both cases the model only writes text. Your program decides what happens next.
Choosing between them
Ask one question: do you need something to happen or to be looked up before the model can finish?
| Need | Use |
|---|---|
| Fetch live data, such as weather or an order status | Tool calling |
| Perform an action, such as sending an email | Tool calling |
| Extract fields from text, such as names and dates from an invoice | Structured output |
| Classify input into fixed labels | Structured output |
| Return a result that a program will consume, with no further steps | Structured output |
If the model already has everything it needs in the prompt, structured output is simpler. It needs one request, no loop, and no dispatcher.
Using a tool to get structured output
Some providers have no separate structured output mode. A common workaround is to define a tool whose parameters are the shape you want, then force the model to call it. You never run the function. You read the arguments as your result.
{
"name": "record_invoice",
"description": "Record the fields extracted from an invoice.",
"parameters": {
"type": "object",
"properties": {
"vendor": {"type": "string"},
"total": {"type": "number"},
"currency": {"type": "string", "enum": ["USD", "EUR", "NOK"]}
},
"required": ["vendor", "total", "currency"],
"additionalProperties": false
}
}Combine this with forced tool choice, covered in the tool choice chapter, so the model always calls record_invoice. Then take the arguments from the call and stop. Do not send a tool result back.
Common errors
- Trusting the schema without checking. Native structured output modes can enforce a schema, but support varies by provider, and not every schema feature is supported. Parse and validate the result anyway, as you do for tool arguments.
- Using tools for plain extraction. Adding a loop and dispatcher for a task that needs no outside data adds code and latency for no benefit.
- Expecting both at once. Some providers restrict combining a response schema with tools, or apply the schema only to the final reply. Check your provider's documentation.
- Forgetting refusals and truncation. The model may refuse a request or hit its token limit mid-JSON. Handle a missing or incomplete result.
Exercise
You build a feature that reads a support email and returns its category (billing, bug, or other) and a priority from 1 to 5, to be stored in a database. No outside data is needed. Do you use tool calling or structured output, and why?
Show answer
Structured output. The model has all the information in the email, and the JSON is the final answer that your code stores. A tool-calling loop would add a dispatcher and extra round trips without giving the model anything new. If your provider lacks a structured output mode, define a tool with the same schema and force it, then read the arguments without executing anything.