Structured Output
Define JSON schemas to ensure structured and validated responses from AI models.
Latitude allows you to enforce structured JSON output from AI models by defining a JSON schema directly in the prompt’s configuration (frontmatter). This ensures responses are consistent, automatically validated, and easy to integrate into your applications.
Specifying the Schema
Use the schema
key within the prompt’s configuration block (---
). Define the expected JSON structure using standard JSON Schema syntax.
In this example:
- We expect a JSON object (
type: object
). - It must have
properties
:sentiment
,confidence
, andexplanation
. sentiment
must be one of the strings in theenum
list.confidence
must be anumber
between 0 and 1.explanation
is an optionalstring
.sentiment
andconfidence
are mandatory (required
list).
How it Works
When a schema
is defined:
- Provider Integration: Latitude communicates the schema to the AI provider (if the provider supports JSON mode or function calling with schemas, like recent OpenAI, Anthropic, and Google models).
- Model Guidance: The model is instructed to generate output strictly adhering to the provided schema.
Benefits
- Reliability: Guarantees consistent data structures from the AI.
- Ease of Use: Simplifies parsing and using AI responses in downstream code.
- Reduced Errors: Catches formatting issues automatically.
- Clear Intent: Explicitly tells the model the desired output format.
Chains and Agents
When working with chains and agents, there’s some things to keep in mind:
Chains
When using chains, the configuration added to the general configuration section in the prompt will be applied to all steps in the chain. This means that if you have a schema defined in the general configuration, it will be applied to all steps, which may not be the expected behavior. To avoid this, you can define the schema in the specific step configuration instead.
Agents
When creating agents, the only schema to have in mind is the schema of the Agent response, since any intermediate steps are used as Chain of Thought, reasoning steps, or tool calling. This is why, even if an Agent has a schema
property defined in the configuration, it will only be applied to the final response of the Agent, and not any intermediate steps.
Next Steps
- Configure other Prompt Settings
- Learn about using Tools for more complex interactions.
- Test your JSON-output prompts in the Playground