Hi everyone !
I’m not sur to understand the different use case for pydantic_function_tool vs response_format when structured output.
Do you have information about best practice ?
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
from openai import OpenAI
client = OpenAI()
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "system",
"content": "Extract the names and ages of the people mentioned in the following text."
},
{
"role": "user",
"content": "John is 30 years old and his sister Alice is 25."
}
],
tools=[
openai.pydantic_function_tool(Person)
]
)
print(completion.choices[0].message.tool_calls[0].function.parsed_arguments)
from pydantic import BaseModel
from openai import OpenAI
client = OpenAI()
class Step(BaseModel):
explanation: str
output: str
class MathReasoning(BaseModel):
steps: list[Step]
final_answer: str
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."},
{"role": "user", "content": "how can I solve 8x + 7 = -23"}
],
response_format=MathReasoning,
)
math_reasoning = completion.choices[0].message.parsed