Structured Output

Enforce exact response formats using schemas like Zod, ensuring predictable and validated data every time.

Overview

Structured output allows you to define the exact shape of the data you expect from the AI. This is essential for building reliable applications where you need to process AI responses programmatically.

Note

Mosaic uses Zod for schema validation. Make sure to install it:yarn add zod

Basic Example

Define a schema and pass it to your command:

structured-basic.ts

Complex Schemas

You can create complex, nested schemas for sophisticated use cases:

structured-complex.ts

Type Inference

Zod schemas automatically infer TypeScript types:

type-inference.ts

Best Practices

  • Be specific: The more detailed your schema, the more accurate the AI's response will be.
  • Use enums: When you have a fixed set of options, use z.enum() instead of strings.
  • Add constraints: Use .min(), .max(), .length() to enforce data quality.
  • Keep schemas reusable: Define schemas in separate files and reuse them across agents.

Next Steps