Skip to main content
Instead of parsing free-form text, you can ask a model to return data that matches a specific structure. Genkit validates the output against your schema and automatically retries if the model returns invalid JSON.

How it works

When you pass an output.schema to generate(), Genkit:
  1. Injects instructions into the prompt telling the model to respond in JSON matching the schema.
  2. Parses the model response and extracts the JSON.
  3. Validates the parsed value against the schema.
  4. Retries the request (up to maxTurns times) if validation fails.
The validated object is then available on response.output.

Basic example

Output formats

The output.format field controls how Genkit instructs the model to format its response. Available formats:

Enum output

Use format: 'enum' to constrain a response to one of a specific set of values:

Array output

Use format: 'array' to request a JSON array. Pair it with a Zod array schema:

Constrained generation

Some models support native constrained generation — the model is instructed at the inference level to only produce tokens that are valid for the given schema. This is more reliable than prompt-based instructions. Set output.constrained: true to enable it when available:
Not all models support constrained generation. Genkit falls back to prompt-based instructions when the model does not support it. You can check model.supports.constrained to see what a model supports.

Extracting structured data from text

A common use case is extracting structured data from unstructured input such as an email, document, or web page.

Accessing the output

The GenerateResponse object exposes several accessors:

Schema validation and retries

Genkit calls response.assertValidSchema() internally. If the model returns output that fails schema validation, Genkit throws a GenkitError. You can call response.isValid() to check without throwing:
For more reliable structured output, use Gemini models with constrained: true. These models support native JSON mode and are less likely to produce invalid output.

Using schemas in flows

Structured output works inside flows just like it does in standalone generate() calls. Define the flow’s output schema with Zod and use it in the generate call:

Streaming

Stream structured output chunks as they arrive.

Flows

Wrap generate calls in type-safe, observable flows.

Prompts

Define output schemas in .prompt files.

Models

See which models support constrained generation.