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.

```Python
from r2r import R2RClient, GenerationConfig
from pydantic import BaseModel
import json

# Define a response model
class ResponseModel(BaseModel):
    answer: str
    confidence: float
    comments: str
    related_joke: str

rag_response = client.retrieval.rag(
    query="What is a Hopfield Network?",
    rag_generation_config=GenerationConfig(
        response_format=ResponseModel
    )
)
```

## 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.

```Python
content = json.loads(rag_response.results.completion)
print(json.dumps(content, indent=2))

response_obj = ResponseModel.model_validate(content)
print("\nAs a Pydantic object:")
print(f"Confidence: {response_obj.confidence}")
print(f"Comments: {response_obj.comments}")
print(f"Related Joke: {response_obj.related_joke}")
print("\nDetailed Answer:")
print(response_obj.answer)
```

## Example Output

Here's what the output looks like when running the code above:

```zsh wordWrap
{
  "answer": "A Hopfield Network is a type of recurrent neural network introduced by John Hopfield in 1982, designed to function as an associative memory system. It consists of binary nodes with symmetric weights, and its dynamics are governed by an energy function that decreases over time, leading the network to stable states that represent stored memories [1], [2].",
  "confidence": 0.95,
  "comments": "The Hopfield Network is a foundational concept in neural networks, and its principles are widely studied in computational neuroscience and machine learning.",
  "related_joke": "Why did the neural network go to therapy? It had too many weights to carry!"
}

As a Pydantic object:
Confidence: 0.95
Comments: The Hopfield Network is a foundational concept in neural networks, and its principles are widely studied in computational neuroscience and machine learning.
Related Joke: Why did the neural network go to therapy? It had too many weights to carry!

Detailed Answer:
A Hopfield Network is a type of recurrent neural network introduced by John Hopfield in 1982, designed to function as an associative memory system. It consists of binary nodes with symmetric weights, and its dynamics are governed by an energy function that decreases over time, leading the network to stable states that represent stored memories [1], [2].
```
