Learning Paths
Return Structured Output
Move from free-form text to schema-shaped data.
Use this path when a workflow needs JSON-shaped data your application can trust.
Goal
By the end, you should know when to use:
- output schemas on agents
- extractors for schema-first extraction
- validation and failure handling
Path
- Read Schemas to define the target shape.
- Read Zod Schema to understand schema conversion.
- Read Agent Output when the agent should respond in a structured shape.
- Read Extractors when you need to extract data from existing text.
- Read Output Validation and Failure Handling.
Choosing the Primitive
| Need | Use |
|---|---|
| The agent should produce typed final output | Agent output schema |
| Existing text should be converted into data | Extractor |
| A pipeline step should normalize data | Extractor step |
| Tool output should be validated | Tool output schema |
Minimal Extractor Shape
Use an extractor when you have text and want validated data back.
import { ExtractorBuilder } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
import { z } from "zod";
const ticketSchema = z.object({
customer: z.string(), priority: z.enum(["low", "medium", "high"]), summary: z.string(), });
const model = new OpenAIClient({ apiKey }).completionModel("gpt-5");
const extractor = new ExtractorBuilder(model, ticketSchema)
.instructions("Extract support ticket fields.")
.retries(1)
.build();
const ticket = await extractor.extract(`
Acme Co. reports checkout failures for all users.
The issue is urgent and blocking revenue.
`);
console.log(ticket.priority);Agent Output Shape
Use an agent output schema when the agent itself should produce structured final output.
import { AgentBuilder } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
import { z } from "zod";
const model = new OpenAIClient({ apiKey }).completionModel("gpt-5");
const agent = new AgentBuilder("classifier", model)
.instructions("Classify support messages.")
.outputSchema(
z.object({
category: z.enum(["billing", "technical", "account"]),
confidence: z.number(),
}),
)
.build();
const response = await agent.prompt("I cannot update my payment method.").send();Add Next
| Need | Read |
|---|---|
| Extraction inside workflows | Extractor Steps |
| Structured tool results | Tool Results |
| Schema errors | Errors |
