Get consistent JSON responses across different LLMs
Requesty router supports structured JSON outputs from various model providers, making it easy to get consistent, parseable responses across different LLMs.
For all models, you can request responses in JSON format by specifying response_format={"type": "json_object"}:
Copy
import osfrom openai import OpenAIfrom pydantic import BaseModelfrom typing import List# Define your data modelclass Entities(BaseModel): attributes: List[str] colors: List[str] animals: List[str]requesty_api_key = "YOUR_REQUESTY_API_KEY" # Safely load your API key# Initialize OpenAI client with Requesty routerclient = OpenAI( api_key=requesty_api_key, base_url="https://router.requesty.ai/v1",)# Request a JSON responseresponse = client.chat.completions.create( model="openai/gpt-4o", # Works with any supported model messages=[ { "role": "system", "content": "Extract entities from the input text and return them in JSON format with the following structure: {\"attributes\": [...], \"colors\": [...], \"animals\": [...]}" }, { "role": "user", "content": "The quick brown fox jumps over the lazy dog with piercing blue eyes", }, ], response_format={"type": "json_object"})# Parse with Pydanticcontent = response.choices[0].message.contentextracted = Entities.model_validate_json(content)print(f"Attributes: {extracted.attributes}")print(f"Colors: {extracted.colors}")print(f"Animals: {extracted.animals}")
For models that support JSON schema (currently OpenAI and Anthropic models), you can use the more powerful parse method with a Pydantic model:
Copy
from openai import OpenAIfrom pydantic import BaseModelfrom typing import Listclass Animals(BaseModel): animals: List[str]requesty_api_key = "YOUR_REQUESTY_API_KEY" # Safely load your API keyclient = OpenAI( api_key=request_api_key, base_url="https://router.requesty.ai/v1",)# Use the parse helper with a Pydantic modelresponse = client.beta.chat.completions.parse( model="anthropic/claude-3-7-sonnet-latest", messages=[ { "role": "system", "content": "Extract the animals from the input text" }, { "role": "user", "content": "The quick brown fox jumps over the lazy dog" }, ], response_format=Animals,)animals = Animals.model_validate_json(response.choices[0].message.content)print(f"Found animals: {animals.animals}") # ['fox', 'dog']
When working with structured outputs, it’s important to handle potential parsing errors:
Copy
try: extracted = Entities.model_validate_json(content) # Process the dataexcept Exception as e: print(f"Error parsing response: {e}") # Handle the error appropriately