This topic describes how to return schema-validated JSON from agent workflows using JSON Schema. The agent can use any
tools it needs to complete the task, and on successful validation the result includes structured data that matches your
schema.
Define a JSON Schema for the structure you need, and the
SDK validates the model’s final output against it.
Agents return free-form text by default, which works for conversational use cases but not when you need to use the
output programmatically. Structured outputs provide typed data you can pass directly to your application logic,
database, or UI components.
Consider an agent that analyzes a codebase. Without structured outputs you get free-form text that you would need to
parse yourself. With structured outputs you define the shape you want and get typed data you can use directly:
Without structured outputs
With structured outputs
This codebase uses Pythonand
TypeScript. It has 42filesand the main entry point is...
Pass a JSON Schema to the outputFormat (TypeScript) or output_format (Python) option. When validation
succeeds, the result message includes a structured_output field with data matching your schema. If the agent
cannot satisfy the schema after retries, the SDK returns an error result instead.
Instead of writing JSON Schema by hand, use Zod (TypeScript) or
Pydantic (Python) to define your schema. These libraries generate the
JSON Schema for you and let you parse the response into a fully typed object with autocomplete and type checking.
This example shows structured outputs with multi-step tool use. The agent finds TODO comments in a codebase using
built-in tools (Grep, Bash), then returns the results as structured data. Optional fields like author handle cases
where git blame information may not be available.
The outputFormat (TypeScript) or output_format (Python) option accepts an object with the following fields:
Field
Value
Description
type
"json_schema"
Required. Only json_schema is supported.
schema
JSON Schema object
Defines the output structure. Generate from Zod with z.toJSONSchema() or Pydantic with .model_json_schema().
Standard JSON Schema features are supported: all basic types (object, array, string, number,
boolean, null), enum, const, required, nested objects, and $ref definitions.
Structured output generation can fail when the agent cannot produce valid JSON matching your schema. When this happens,
the result message has a subtype indicating what went wrong:
Subtype
Meaning
success
Output was generated and validated successfully
error_max_structured_output_retries
Agent could not produce valid output after multiple attempts
forawait (const msg ofquery({
prompt: "Extract contact info from the document",
options: {
cwd: process.cwd(),
outputFormat: { type: "json_schema", schema: contactSchema },
},
})) {
if (msg.type === "result") {
if (msg.subtype === "success" && msg.structured_output) {
console.log(msg.structured_output);
} elseif (msg.subtype === "error_max_structured_output_retries") {
console.error("Could not produce valid output");
}
}
}
asyncfor message in query(
prompt="Extract contact info from the document",
options=CortexCodeAgentOptions(
cwd=".",
output_format={"type": "json_schema", "schema": contact_schema},
),
):
ifisinstance(message, ResultMessage):
if message.subtype == "success"and message.structured_output:
print(message.structured_output)
elif message.subtype == "error_max_structured_output_retries":
print("Could not produce valid output")
Tip
Tips for avoiding errors:
Keep schemas focused. Deeply nested schemas with many required fields are harder to satisfy. Start simple and
add complexity as needed.
Match schema to task. If the task might not have all the information your schema requires, make those fields
optional.
Use clear prompts. Ambiguous prompts make it harder for the agent to know what output to produce.
Where your configuration of Cortex Code uses a model provided on the
Model and Service Pass-Through Terms,
your use of that model is further subject to the terms for that model on that page.
The data classification of inputs and outputs are as set forth in the following table.