Specifying the Schema
Use theschema
key within the prompt’s configuration block (---
). Define the expected JSON structure using standard JSON Schema syntax.
- 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 aschema
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 aschema
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