> ## Documentation Index
> Fetch the complete documentation index at: https://docs.redpill.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Structured outputs

> Constrain a response to a JSON schema with response_format, from a Pydantic model or a raw schema.

Structured outputs constrain the response to a JSON schema you supply in `response_format`, using
the standard OpenAI parameter. Pass a Pydantic model through the SDK, or a raw JSON schema.

## Pydantic models

```python theme={null}
import os
from openai import OpenAI
from pydantic import BaseModel

client = OpenAI(
    api_key=os.environ["REDPILL_AI_API_KEY"],
    base_url="https://api.redpill.ai/v1"
)

class CalendarEvent(BaseModel):
    name: str
    date: str
    participants: list[str]

response = client.beta.chat.completions.parse(
    model="openai/gpt-5",
    messages=[
        {"role": "system", "content": "Extract calendar events from text"},
        {"role": "user", "content": "Team meeting tomorrow at 2pm with Alice and Bob"}
    ],
    response_format=CalendarEvent
)

event = response.choices[0].message.parsed
print(event.name)  # "Team meeting"
print(event.participants)  # ["Alice", "Bob"]
```

## JSON schema

```python theme={null}
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "email": {"type": "string", "format": "email"},
        "age": {"type": "integer", "minimum": 0},
        "interests": {
            "type": "array",
            "items": {"type": "string"}
        }
    },
    "required": ["name", "email"],
    "additionalProperties": False
}

response = client.chat.completions.create(
    model="openai/gpt-5",
    messages=[
        {"role": "user", "content": "Extract user info: John Doe, john@example.com, 30 years old, likes hiking and coding"}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "user_profile",
            "strict": True,
            "schema": schema
        }
    }
)

import json
user = json.loads(response.choices[0].message.content)
```

## Supported models

Structured outputs work on models whose `supported_features` include `structured_outputs`. Check
[`GET /v1/models`](/api-reference/models).

<Tip>
  For models without native structured output support, use function calling with a single function whose parameters match your desired schema.
</Tip>

## Related

<CardGroup cols={2}>
  <Card title="Function calling" icon="function" href="/guides/function-calling" />

  <Card title="Chat completions" icon="code" href="/api-reference/chat-completions" />
</CardGroup>
