Structured Outputs
Learn how to use structured outputs with IronaAI to get responses in a specific format.
IronaAI 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.
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.
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.