Structured Outputs
Structured outputs allow you to control and define the format of a model’s response using a schema. This makes it easy to enforce consistency, guarantee valid JSON, and parse results programmatically. Below we show two approaches: simple JSON output using json_object and fully validated structured output using Pydantic classes.
Simple JSON Example
JSON output is ideal for simple structured responses where you only need the model to return well-formed JSON.
import json
from openai import OpenAI
client = OpenAI(
api_key="<YOUR_API_KEY>",
base_url="https://api.arcee.ai/api/v1"
)
system_prompt = """
The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format.
EXAMPLE INPUT:
Which is the highest mountain in the world? Mount Everest.
EXAMPLE JSON OUTPUT:
{
"question": "What is the capital of France?",
"answer": "Paris"
}
"""
user_prompt = "What is the capital of Italy?"
messages = [{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}]
response = client.chat.completions.create(
model="MODEL_NAME",
messages=messages,
response_format={
'type': 'json_object'
}
)
print(json.loads(response.choices[0].message.content))Last updated


