Anvia
Structured Output

Agent Output

Use schemas directly on agent responses.

Use agent output schemas when the agent's final answer should follow a JSON-shaped contract.

1. Define the Response Shape

import { AgentBuilder } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
import { z } from "zod";

const model = new OpenAIClient({ apiKey }).completionModel("gpt-5");

const classificationSchema = z.object({
  category: z.enum(["billing", "technical", "account"]),
  confidence: z.number(),
});

2. Attach It to the Agent

const agent = new AgentBuilder("classifier", model)
  .instructions("Classify support messages.")
  .outputSchema(classificationSchema)
  .build();

3. Send the Prompt

const response = await agent
  .prompt("I cannot update my payment method.")
  .send();

response.output is the final assistant text. When your application needs typed data, parse and validate it at the boundary.

const data = classificationSchema.parse(JSON.parse(response.output));

console.log(data.category);

When to Use Agent Output

Use this when the agent is already doing the reasoning and should return a structured final answer. Use an Extractor when the job is only to convert existing text into data.