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 objectcreateParsedCompletion(...)
An agent uses tools, memory, or retrieval firstAgent output schema
Existing content must be converted into fieldsExtractor
A tool returns typed data to the modelTool output schema
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 { createParsedCompletion } from '@anvia/core'
import { z } from 'zod'

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

console.log(result.data.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.