Structured outputs allow users to define an output scheme using pydantic. OpenAI and most others support this now (see e.g. OpenAI and their docs).
from pydantic import BaseModel
from openai import OpenAI
client = OpenAI()
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
],
response_format=CalendarEvent,
)
event = completion.choices[0].message.parsed
In my own tests using scikit-ollama I found the models to adhere much better to the output scheme. It barely ever had to fall back to the default label which made it overall more accurate.
It was also usually faster.