What are Structured Outputs?
Structured outputs define formats LLMs must follow when generating responses:- Consistent response formats
- Easier parsing and validation
- More reliable integration with your applications
- Less error handling
Creating Structured Outputs with JSON Schema
To add a JSON schema to your prompt template:- Edit your prompt template
- Click “Add Response Format”
- Select “JSON Schema”
- Define your schema structure
Example: Customer Review Analysis Schema
Using Variables in Structured Outputs
You can make your schemas dynamic by using template variables:Variables in structured outputs only work with Jinja2 format with the Jinja2 option enabled. F-string format isn’t supported.
Interactive Mode
When using the interactive schema editor, you can add variables in two ways:- For enum values: Click the enum field and toggle the switch to “Use Variable”
- For text/string values: Type
{{ variable_name }}
directly in any text field
JSON Mode
Variables must be in quotes, except for enum variables:Jinja2
Dynamic Schema Injection with Variable Type
For advanced use cases, you can inject entire schema sections dynamically at runtime using thevariable
type. This allows you to define different schemas based on runtime conditions without creating multiple prompt templates.
How It Works
- In the schema editor, select “variable” as the type for a field
- Specify a variable name (e.g.,
userSchema
) - At runtime, pass the complete schema object for that variable
Example: Dynamic User Schema
In your prompt template schema:user_data
field will be replaced entirely with the schema you provide, allowing different structures for different use cases.
Important Notes
- Variable schemas only work with Jinja2 template format
- The entire field is replaced with the provided schema object
- Ensure the injected schema is valid JSON schema syntax
Best Practices
- For enum values: Use
{name: "variable_name", type: "enum_variable"}
in JSON mode or the variable selector in interactive mode - For text variables: Include them within quotes as
{{ variable_name }}
in both modes - For dynamic schemas: Use
type: "variable"
with a descriptive variable name - Only Jinja2 format works for variables in structured outputs
- Ensure all variables used in the schema are provided
- Use proper JSON formatting with variables