Structured Output
Enable JSON Outputs
Overview
Structured outputs allow users to ensure that the retrieval response generated by the LLM follows a user-defined structure. This provides reliable type-safety, making it easier to generate high-quality, production-ready applications.
R2R supports passing Pydantic models via our Python SDK.
With this, you can:
- Define the exact structure you expect for responses
- Automatically validate that responses match your schema
- Access response fields with proper typing and autocompletion
- Handle errors gracefully when responses don’t match expectations
Using Structured Outputs with R2R
The example below demonstrates how to define a simple Pydantic model that specifies the expected structure for responses to a query about Hopfield Networks. The model includes fields for the main answer, a confidence score, additional comments, and even a related joke.
Processing the Response
Once you’ve received a response, you can parse it as JSON and validate it against your Pydantic model. This ensures that the response contains all required fields with the correct data types.
Example Output
Here’s what the output looks like when running the code above: