Hi @john47 ,
I was actually about to post a similar question, meaning if there’s a fundamental difference between using the client.chat.completions.create
and the client.beta.chat.completions.parse
(using pydantic model).
Below is a very simple example:
class CalendarEvent(BaseModel):
name: str = Field(description="the name")
date: str = Field(description="the date")
participants: list[str]
res = client.beta.chat.completions.parse(
model=config["OPEN_AI_MODEL_4o_MINI"],
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,
)
res.choices[0].message.parsed.model_dump()
# {'name': 'Science Fair', 'date': 'Friday', 'participants': ['Alice', 'Bob']}
res2 = client.chat.completions.create(
model=config["OPEN_AI_MODEL_4o_MINI"],
messages=[
{"role": "system", "content": """
- Extract the event information.
- The structure should be returned in JSON containing:
- the name of the event, the key name should be `event`
- the date of the event, the key name should be `date`
- an array of all participants, the key name should be `participants`
"""},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
],
response_format={ "type": "json_object" },
)
print(res2.choices[0].message.content)
#{
# "event": "science fair",
# "date": "Friday",
# "participants": ["Alice", "Bob"]
#}
In this example both output are the same but I prefer pydantic
because it seems to offer more control on the desired output, with less prompting.
It also provides the possibility to have default values, so it can be useful in case the data extraction fails.
Final point is that you can add all the pydantic
validation tools directly.
I did not see on larger examples a difference in term on timing between both methods.