How it works
When you pass anoutput.schema to generate(), Genkit:
- Injects instructions into the prompt telling the model to respond in JSON matching the schema.
- Parses the model response and extracts the JSON.
- Validates the parsed value against the schema.
- Retries the request (up to
maxTurnstimes) if validation fails.
response.output.
Basic example
- TypeScript
- Python
- Go
Output formats
Theoutput.format field controls how Genkit instructs the model to format its response. Available formats:
Enum output
Useformat: 'enum' to constrain a response to one of a specific set of values:
Array output
Useformat: '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. Setoutput.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
TheGenerateResponse object exposes several accessors:
Schema validation and retries
Genkit callsresponse.assertValidSchema() internally. If the model returns output that fails schema validation, Genkit throws a GenkitError. You can call response.isValid() to check without throwing:
Using schemas in flows
Structured output works inside flows just like it does in standalonegenerate() 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.
