← Claude Docs

Get structured output from agents - Claude Code Docs

Claude Docs · June 12, 2026
Structured outputs allow agents to return validated JSON data matching a defined schema instead of free-form text, enabling programmatic use of the results in applications. Developers can define schemas using JSON Schema directly or leverage Zod (TypeScript) and Pydantic (Python) for full type safety and runtime validation. The agent autonomously uses necessary tools to complete tasks and returns results formatted according to the specified schema, with retry mechanisms to handle validation failures.

Detailed Analysis

Anthropic's Claude Code SDK documentation outlines a structured outputs feature that enables developers to define precise data schemas for agent responses, ensuring that the output of an AI agent conforms to a validated JSON structure rather than returning free-form text. The feature works by accepting a JSON Schema definition passed through the `outputFormat` (TypeScript) or `output_format` (Python) option in the SDK's `query()` function. Once the agent completes its task — which may involve multiple tool-use steps — the result includes a `structured_output` field containing data validated against the specified schema. If the agent fails to produce conforming output within the retry limit, the SDK returns an error with a subtype of `error_max_structured_output_retries` rather than unvalidated data, ensuring downstream application logic is never handed malformed responses.

A central design goal of the feature is integration with established type-safety ecosystems. Developers can use Zod in TypeScript or Pydantic in Python to define schemas programmatically, avoiding the need to hand-write raw JSON Schema. These libraries generate the underlying JSON Schema automatically while also enabling runtime validation and full type inference, meaning developers get IDE autocomplete, type checking, and reusable schema components throughout their codebase. The documentation illustrates this with a feature implementation plan example that returns a strongly-typed object with properties like `plan.summary` and `plan.steps`, demonstrating that the output is immediately consumable by application logic without additional parsing or transformation.

The practical significance of structured outputs becomes apparent in multi-step agentic workflows, which the documentation illustrates through a TODO tracking agent example. In that scenario, the agent autonomously selects tools — such as Grep for file searches and Bash for git commands — to gather information across multiple steps, then consolidates the results into a single structured response. This pattern highlights a critical distinction between traditional API calls and agentic execution: the agent may take an unpredictable number of intermediate actions, yet the developer still receives a predictable, schema-validated output. The inclusion of optional schema fields for cases where information may be unavailable (e.g., git blame data) reflects a pragmatic approach to real-world data incompleteness.

The error handling model reveals important design tradeoffs in production-grade agent systems. Beyond straightforward validation failures, the documentation acknowledges a more subtle failure mode: a model fallback mechanism can retract an already-completed output mid-stream, resulting in the same error subtype as a validation failure. The documentation advises inspecting the `errors` text field to distinguish between these two causes before attempting to debug the schema, indicating that agent reliability in structured output scenarios involves infrastructure-level concerns beyond schema design alone. Best practice guidance emphasizes keeping schemas focused and minimal, matching schema requirements to what the task can realistically produce, and writing unambiguous prompts — all of which point to the iterative, empirical nature of agent prompt engineering.

This feature situates Anthropic's Claude Code SDK within a broader industry trend toward making AI agents first-class components of production software architectures. The ability to receive strongly-typed, validated outputs from agents that use tools autonomously mirrors patterns long established in traditional API design — essentially treating the agent as a structured service endpoint rather than a conversational interface. Competing frameworks such as OpenAI's structured outputs and LangChain's output parsers address similar needs, but Anthropic's integration of schema validation directly into the SDK's query lifecycle, combined with native support for Zod and Pydantic, signals a deliberate effort to align agent development workflows with existing software engineering practices rather than requiring developers to adopt entirely new paradigms.

Read original article →