
Stop Parsing Agent Output Yourself: Get Validated JSON From Any Agent Workflow With the Claude Agent SDK
Chris Harper
2 min read
Jul 21, 2026 · 04:12 UTC
TL;DR: The Claude Agent SDK's outputFormat option validates agent output against a Zod or Pydantic model, giving you typed JSON with auto-retry — no manual parsing needed.
Every multi-step agent eventually returns something you need to parse. Ad-hoc json.loads() and regex string parsing works until the model rephrases its answer — then your pipeline breaks silently. The Claude Agent SDK has a cleaner primitive: outputFormat.
Define the shape once. The SDK validates the agent's output, re-prompts on mismatch (within a retry budget), and returns a fully typed object.
TypeScript / Zod:
import { query } from "@anthropic-ai/claude-code";
import { z } from "zod";
const BugReport = z.object({
file: z.string(),
line: z.number(),
severity: z.enum(["critical", "high", "medium", "low"]),
description: z.string(),
suggestion: z.string(),
});
const result = await query({
prompt: "Find bugs in src/auth/token.ts. Be thorough.",
outputFormat: z.object({ bugs: z.array(BugReport) }),
});
// result.structured_output is fully typed — no cast needed
const critical = result.structured_output.bugs.filter(
b => b.severity === "critical"
);
Python / Pydantic:
from anthropic_agent_sdk import query
from pydantic import BaseModel
class BugReport(BaseModel):
file: str
line: int
severity: str
description: str
suggestion: str
class BugList(BaseModel):
bugs: list[BugReport]
result = query(prompt="Find bugs in src/auth/token.ts.", output_format=BugList)
critical = [b for b in result.structured_output.bugs if b.severity == "critical"]
The SDK injects schema validation through the agent's StructuredOutput tool, so the model is constrained at generation time — not just post-checked. On validation failure it retries; after the retry budget you get a typed error, not silent garbage.
Use this wherever you'd otherwise parse agent output: code review pipelines, changelog drafters, dependency scanners, any workflow that fans out to structured data.
Sources: Get structured output from agents — Claude Code Docs · Structured outputs — Claude Platform Docs