Anvia
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

  1. Read Schemas to define the target shape.
  2. Read Zod Schema to understand schema conversion.
  3. Read Agent Output when the agent should respond in a structured shape.
  4. Read Extractors when you need to extract data from existing text.
  5. Read Output Validation and Failure Handling.

Choosing the Primitive

NeedUse
The agent should produce typed final outputAgent output schema
Existing text should be converted into dataExtractor
A pipeline step should normalize dataExtractor step
Tool output should be validatedTool 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

NeedRead
Extraction inside workflowsExtractor Steps
Structured tool resultsTool Results
Schema errorsErrors