> ## Documentation Index
> Fetch the complete documentation index at: https://docs.opper.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Structured Output

> Return validated, typed data from an agent run

<Info>
  Source: [`01_agent_with_schema.py`](https://github.com/opper-ai/opper-sdks/blob/main/python/examples/agents/01_agent_with_schema.py) · [`01-agent-with-schema.ts`](https://github.com/opper-ai/opper-sdks/blob/main/typescript/examples/agents/01-agent-with-schema.ts)
</Info>

Pass an `output_schema` to constrain the agent's final answer to a typed shape. The SDK validates the model's output against the schema and returns a typed object.

## Pydantic / Zod

<CodeGroup>
  ```python Python theme={null}
  from pydantic import BaseModel
  from opperai.agent import Agent

  class Summary(BaseModel):
      title: str
      key_points: list[str]
      sentiment: str  # "positive" | "negative" | "neutral"

  agent = Agent(
      name="summarizer",
      instructions="Summarize the input.",
      output_schema=Summary,
  )

  result = await agent.run("Python 3.12 brings major performance improvements...")

  summary = result.output  # validated Summary instance
  print(summary.title)
  print(summary.sentiment)
  for point in summary.key_points:
      print("-", point)
  ```

  ```typescript TypeScript theme={null}
  import { z } from "zod";
  import { Agent } from "opperai";

  const Summary = z.object({
    title: z.string().describe("A short title"),
    key_points: z.array(z.string()).describe("3-5 key points"),
    sentiment: z.enum(["positive", "negative", "neutral"]),
  });

  const agent = new Agent({
    name: "summarizer",
    instructions: "Summarize the input.",
    outputSchema: Summary,
  });

  const result = await agent.run("Python 3.12 brings major performance improvements...");

  // result.output is typed — no cast needed
  console.log(result.output.title);
  console.log(result.output.sentiment);
  for (const point of result.output.key_points) console.log("-", point);
  ```
</CodeGroup>

## Other schema sources

Both SDKs accept raw JSON Schema. Python additionally accepts dataclasses and `TypedDict`. TypeScript accepts any Standard Schema implementation (Valibot, ArkType, etc.).

```python Python theme={null}
output_schema = {
    "type": "object",
    "properties": {
        "title": {"type": "string"},
        "key_points": {"type": "array", "items": {"type": "string"}},
    },
    "required": ["title", "key_points"],
}
```

## Without a schema

Omit `output_schema` and the agent returns a string in `result.output`.
