Skip to content

Structured Output ​

@aeye can enforce that AI responses conform to a Zod schema, giving you type-safe structured data instead of free-form text.

TIP

For classification, routing, and scoring (picking from a known set of answers), Decisions are about 10x faster and cheaper than structured output, and return probabilities.

Basic Usage ​

typescript
import z from 'zod';

const analyzer = ai.prompt({
  name: 'analyzer',
  description: 'Analyze text sentiment',
  content: 'Analyze the sentiment of: {{text}}',
  input: (input: { text: string }) => ({ text: input.text }),
  schema: z.object({
    sentiment: z.enum(['positive', 'negative', 'neutral']),
    confidence: z.number().min(0).max(1),
    keywords: z.array(z.string()),
  }),
});

const result = await analyzer.get('result', {
  text: 'This product is amazing!',
});

// result is fully typed:
// { sentiment: 'positive', confidence: 0.95, keywords: ['amazing'] }

How It Works ​

  1. The Zod schema is converted to JSON Schema
  2. Sent as responseFormat to the AI model
  3. The model generates JSON conforming to the schema
  4. @aeye parses and validates the response with Zod
  5. If validation fails, the prompt retries with an error message

Strict Mode ​

Prompt.strict is boolean | number (default: 1):

ValueMeaning
trueHard requirement — selection filters to strict-capable models.
falseForce lenient. Standard JSON Schema, no strict flag on the wire.
number > 0 (default 1)Best-effort preference. Strict on models that support it, lenient fallback elsewhere.
typescript
const prompt = ai.prompt({
  schema: z.object({ /* ... */ }),
  // strict omitted → priority 1 (best-effort, default)
});

const strictPrompt = ai.prompt({
  schema: z.object({ /* ... */ }),
  strict: true,  // require a strict-capable model
});

A schema the model can't express, or one a provider rejects, falls back on its own; see Schema Delivery and Fallbacks.

@aeye reconciles each provider's strict-mode dialect (OpenAI / Anthropic / Google / xAI) so the same Zod schema works everywhere — see the Strict Mode guide for format families, per-request budgets, the curated strictSupport overrides, and how to register a custom dialect.

Schema Delivery and Fallbacks ​

schemaDelivery decides how the schema reaches the model:

ValueDelivery
'auto' (default)Structured output when the model's dialect can express the schema, otherwise the schema as prompt text
'structured'Always structured output
'json'JSON mode, with the schema in the prompt
'prompt'Plain text, with the schema in the prompt

"Can express" is canExpress(schema, descriptor). Under Anthropic's strict dialect it's false for a schema with z.any(), z.unknown() or recursion, which Anthropic strict can't spell, and for one over the strict limits on its own (24 optional parameters, 16 unions), since Anthropic compiles every structured output whether or not it's flagged strict. Such a schema goes as prompt text rather than earning a 400.

The prompt's parsing and validation run the same way under every delivery. Providers map them to their own API: OpenAI and OpenRouter send json_schema (strict or not) or json_object; Bedrock Converse and Replicate have neither, so they always get the schema as prompt text.

When a provider rejects a schema, it throws a SchemaRejectedError naming the output or the tool, and the prompt steps down its schemaFallbacks and sends the request again. The default, SCHEMA_FALLBACKS, goes:

  1. non-strict — the rejected schema (output or tool) drops strict. A required strict: true is kept.
  2. json — the output moves to JSON mode with its schema in the prompt.
  3. text — the output moves to plain text with its schema in the prompt.

Each step is taken at most once per schema; the error is thrown when none applies. A step stays on the request for the rest of the run, so later iterations (after tool calls, retries, or a dynamic re-resolve) start there instead of failing first. Fallbacks don't use outputRetries, and runtimeSchemaFallback: false turns them off.

typescript
import { SCHEMA_FALLBACKS, type SchemaFallback } from '@aeye/core';

// Skip JSON mode
const prompt = ai.prompt({
  schema,
  schemaFallbacks: SCHEMA_FALLBACKS.filter(step => step.name !== 'json'),
});

// Your own step: apply() changes the request and returns true, or returns false when it doesn't apply
const dropTools: SchemaFallback = {
  name: 'drop-tool',
  apply: (request, target) => {
    if (target.kind !== 'tool') return false;
    request.tools = request.tools?.filter(tool => tool.name !== target.name);
    return true;
  },
};

// AI-wide, through the default context
const ai = AI.with<{ schemaFallbacks?: readonly SchemaFallback[] }>()
  .providers({ openai })
  .create({ defaultContext: { schemaFallbacks: [...SCHEMA_FALLBACKS, dropTools] } });

Every step emits a schemaFallback event with the step's fallback name and the error.

Dynamic Schema ​

The schema can change based on context:

typescript
const dynamicPrompt = ai.prompt({
  name: 'extract',
  content: 'Extract data from: {{text}}',
  input: (input: { text: string }) => input,
  schema: (ctx) => {
    if (ctx.extractMode === 'simple') {
      return z.object({ summary: z.string() });
    }
    return z.object({
      summary: z.string(),
      entities: z.array(z.object({
        name: z.string(),
        type: z.string(),
      })),
      dates: z.array(z.string()),
    });
  },
});

Return false to disable structured output for that call:

typescript
schema: (ctx) => {
  if (ctx.freeform) return false;
  return z.object({ /* ... */ });
},

Output Validation ​

Add custom validation beyond the schema:

typescript
const prompt = ai.prompt({
  schema: z.object({
    items: z.array(z.string()),
    count: z.number(),
  }),
  validate: (output, ctx) => {
    if (output.items.length !== output.count) {
      throw new Error(`Count ${output.count} doesn't match ${output.items.length} items`);
    }
  },
});

Validation errors trigger a retry with the error message sent to the model.

Custom Parser (bring your own) ​

Supply a parse function to replace Zod for structured output. It receives the raw JSON.parse-d value and fully owns turning it into the decoded value; its return type drives the decoded output (the trailing TDecoded type parameter). Return (or throw) an Error to reject — routed through the same outputRetries channel a Zod failure would, surfacing the error's own .message.

typescript
const prompt = ai.prompt({
  schema: z.object({ expr: z.string() }),      // still required — drives the model wire format
  parse: (raw, ctx) => {
    const result = compile((raw as { expr: string }).expr); // your parser → a built AST
    if (result.errors) return new Error(result.report);      // compiler-style diagnostics to the model
    return result.program;                                   // decoded value flows out typed
  },
});

Only runs where Zod validation runs today (a structured schema is present); the schema is still required and still drives the wire format. Absent ⇒ the unchanged Zod path. This is how @aeye/query returns concise, compiler-style errors instead of Zod's aggregate messages.

Retry Behavior ​

When output parsing or validation fails:

  1. The error is sent back to the model as a user message, and a textReset event carries it as an OutputValidationError
  2. The model tries again (up to outputRetries times, default: 2)
  3. If all retries fail, the prompt throws a PromptError (kind: 'model') whose cause is the last OutputValidationError

A review can then check an output that passed.

typescript
const prompt = ai.prompt({
  schema: z.object({ /* ... */ }),
  outputRetries: 3, // try up to 3 times
});

JSON Response Format ​

For simple JSON without schema validation, use responseFormat:

typescript
const response = await ai.chat.get({
  messages: [{ role: 'user', content: 'List 3 colors as JSON' }],
  responseFormat: 'json',
});

const data = JSON.parse(response.content);

Schema Utilities ​

@aeye provides utilities for working with Zod schemas and JSON Schema:

typescript
import { toJSONSchema, strictify } from '@aeye/core';

// Convert Zod to JSON Schema
const jsonSchema = toJSONSchema(myZodSchema);

// Make a schema strict-mode compatible
const strictSchema = strictify(myZodSchema);

Released under the GPL-3.0 License.