Skip to content
ai-effect
Esc
↑↓navigate↵open⌘Jpreview

Schemas

Use Effect Schemas for structured output with effectSchema.

effectSchema turns an Effect Schema into an AI SDK schema, for places that take one directly, such as structured output. tool calls it for you on inputSchema and outputSchema; you need it yourself for Output:

import { Output } from 'ai';
import { effectSchema, generateText } from 'ai-effect';
import { Effect, Schema } from 'effect';

const Recipe = Schema.Struct({
	name: Schema.String,
	steps: Schema.Array(Schema.String),
});

const recipe = generateText({
	model: 'openai/gpt-5',
	prompt: 'Give me a pancake recipe.',
	output: Output.object({ schema: effectSchema(Recipe) }),
}).pipe(Effect.map((result) => result.output));

The model’s JSON is decoded with your schema, so transformations such as Schema.NumberFromString apply and result.output has the decoded type.

Validation failures

When the model’s output does not match the schema, validation fails with the SDK’s TypeValidationError, which generateText reports as an AiError with an ai-effect/InvalidOutputError reason. That reason is retryable, so you can retry it as shown in Errors.

JSON Schema

The JSON Schema sent to the model is generated by Effect (draft-07). Structs are closed with additionalProperties: false, but free-form records such as Schema.Record(Schema.String, Schema.Unknown) stay open, so the model can still return arbitrary keys there. The AI SDK on its own would close every object, including those.

Was this page helpful?