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

# Structured Outputs

> Learn how to use structured outputs with IronlabsAI to get responses in a specific format.

IronlabsAI allows you to specify the format of the response from the language model using structured outputs. This feature ensures that the output adheres to a defined structure, such as JSON or a Pydantic model, making it easier to parse and integrate into your application.

## When to use structured outputs

Use structured outputs when you need the language model's response to follow a specific schema. This is ideal for:

* Generating structured data for databases
* Creating consistent API responses
* Enforcing a predefined output format
* Structured outputs can be specified as either a JSON object or a Pydantic model.

### Using JSON object as response format

To request a JSON object, pass `response_format={"type": "json_object"}` to the `create` method. The response will be a valid JSON string.

```python theme={null}
response = client.completions.create(
    response_format={"type": "json_object"},
    messages=[
        {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
        {"role": "user", "content": "Who won the world series in 2020?"}
    ]
)
print(response.choices[0].message.content)
```

The response will be a JSON string that you can parse with `json.loads()` in Python or `JSON.parse()` in TypeScript.

### Using Pydantic model as response format

For more complex structures, define a Pydantic model and pass it as the `response_format`. The response will be an instance of that model.

```python theme={null}
from pydantic import BaseModel

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

class EventsList(BaseModel):
    events: list[CalendarEvent]

messages = [{"role": "user", "content": "List 5 important events in the XIX century"}]

resp = client.completions.create(
    messages=messages,
    response_format=EventsList
)
print(resp)
```

The response will be an `EventsList` instance, allowing direct access to its attributes.

Note: Pydantic models are Python-specific. In TypeScript, use JSON schema or other validation tools.
