Structured output
Free-text answers are fine for chat, but when your agent feeds another program you want a typed, validated result, not prose to parse. Set outputSchema — a Zod object schema — and the run's final answer is forced through it: the SDK registers a final_answer tool with your schema as its parameters, instructs the model to call it exactly once when done, validates the arguments, and surfaces them as result.output.
Usage
query() accepts the same option. Its generator resolves to the LiteAgentResult, so drive it manually if you need result.output:
How it works
- A
final_answertool is registered with your schema as its parameter schema, so the model can only produce structurally valid arguments. - A
## Final answersection is appended to the system prompt: the model must callfinal_answerexactly once when the task is complete, and only that call is read as the answer. - The tool handler records the validated arguments for the current session; when the run resolves, they are attached as
result.output.
LiteAgentResult is RunResult & { output?: unknown } — output is only present when outputSchema is set and the model produced the final answer.
Details
See also
- System prompt — how
outputSchemaextends the prompt with a## Final answersection. - Subagents — why children don't inherit the schema.
- Getting started — install and run your first agent.