Skip to content

Which structured-output primitive should I use? ​

Choose based on the workflow before the object is produced.

NeedPrimitive
One model call returns one validated objectgenerateCompletion(...)
An agent uses tools, memory, or retrieval firstAgent output schema
Existing content must be converted into fieldsextract()
A tool returns typed data to the modelTool outputSchema
Typed data is one stage in a larger workflowPipeline

Does a schema guarantee valid model output? ​

No. It defines the requested shape and the application validation boundary. Provider output-schema support varies, and a model can still produce invalid JSON or data that fails validation. Handle the documented errors instead of trusting unvalidated text.

ts
import { generateCompletion } from '@anvia/core'
import { z } from 'zod'

const result = await generateCompletion({
    prompt: 'Checkout is unavailable for all customers.',
    model,
    outputSchema: z.object({ priority: z.enum(['low', 'high']) })
})

console.log(result.output.priority)

Should I parse result.text myself after validation fails? ​

No. That bypasses the schema boundary. Return a controlled failure, retry only under an explicit policy, or route the input for review.

Check the exact model's schema support in the provider capability matrix, then follow Schema design and Validation errors.

Built for Anvia.