Anvia
Structured Output

Zod Schema

Convert Zod schemas into provider-compatible structured output contracts.

Anvia accepts Zod schemas and converts them into provider-facing JSON Schema. Use Zod as the source of truth in your application code.

1. Create a Zod Schema

import { z } from "zod";

const summarySchema = z
  .object({
    title: z.string(),
    bullets: z.array(z.string()),
  })
  .meta({ title: "summary_response" });

The .meta({ title }) value is preserved in the JSON Schema sent to the model provider.

2. Use It With an Agent

const agent = new AgentBuilder("summarizer", model)
  .instructions("Return a concise structured summary.")
  .outputSchema(summarySchema)
  .build();

outputSchema(...) constrains the model response at the provider request layer. The prompt response still contains response.output as text, so parse or validate it before using it as application data.

3. Use It With an Extractor

const extractor = new ExtractorBuilder(model, summarySchema)
  .retries(1)
  .build();

Extractors validate the submitted data against the schema and return typed data from extract(...).

4. Keep Provider Limits in Mind

Use schema features that map cleanly to JSON Schema:

PreferBe Careful With
z.object, z.string, z.number, z.booleandeeply recursive schemas
z.enum([...])broad z.any() fields
z.array(...)complex unions
.describe(...)descriptions that repeat the field name

Next, use the schema with Agent Output or Extractors.