Structured output ​
Structured output turns a model response into application data that has crossed a local schema-validation boundary.
Model response -> JSON parsing -> schema validation -> application dataA provider may use the schema to guide generation, but product code should trust only the value produced after local validation.
1. Define the contract ​
Anvia uses Zod schemas for agent output schemas, extractors, tool inputs and outputs, and pipeline inputs. Parsed completions (generateCompletion() / streamCompletion()) accept any Standard Schema: Zod, Valibot, ArkType, and other implementing libraries (see Schema design):
import { z } from 'zod'
const ticketSchema = z.object({
category: z.enum(['billing', 'technical', 'account']),
priority: z.enum(['low', 'normal', 'high']),
summary: z.string().min(1),
needsHumanReview: z.boolean(),
})
type Ticket = z.infer<typeof ticketSchema>Keep the schema small, explicit, and aligned with the decision the application needs to make.
2. Choose where validation happens ​
Use generateCompletion() when one model call should produce one validated value:
import { generateCompletion } from '@anvia/core'
const result = await generateCompletion({
prompt: message,
model,
outputSchema: ticketSchema,
instructions: 'Classify the support request.'
})
console.log(result.output.priority)Use an agent outputSchema when tools, retrieval, memory, or multiple turns are needed before the final object. Anvia validates that schema before returning a response outcome; response.output is already typed.
Use extract() when structured fields already exist in source text and a required submission tool is more appropriate than provider-native output schemas.
3. Continue through the section ​
- Design portable schemas
- Use parsed completion
- Validate agent output
- Extract fields from text
- Handle validation failures
- Choose the right primitive
Use a regular completion or prose agent response when only a person will read the answer and no application code needs to branch on its fields.