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.
Use the schema
key within the prompt’s configuration block (---
). Define the expected JSON structure using standard JSON Schema syntax.
In this example:
type: object
).properties
: sentiment
, confidence
, and explanation
.sentiment
must be one of the strings in the enum
list.confidence
must be a number
between 0 and 1.explanation
is an optional string
.sentiment
and confidence
are mandatory (required
list).When a schema
is defined:
When working with chains and agents, there’s some things to keep in mind:
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.
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.
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.
Use the schema
key within the prompt’s configuration block (---
). Define the expected JSON structure using standard JSON Schema syntax.
In this example:
type: object
).properties
: sentiment
, confidence
, and explanation
.sentiment
must be one of the strings in the enum
list.confidence
must be a number
between 0 and 1.explanation
is an optional string
.sentiment
and confidence
are mandatory (required
list).When a schema
is defined:
When working with chains and agents, there’s some things to keep in mind:
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.
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.