Array as completions.parse's response_format #1734
Replies: 2 comments 1 reply
-
|
Beta Was this translation helpful? Give feedback.
-
|
The usual pattern is to wrap the array in a model: from pydantic import BaseModel
class UserDetail(BaseModel):
age: int
name: str
class UserDetailsResponse(BaseModel):
users: list[UserDetail]
completion = client.beta.chat.completions.parse(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Provide synthetic data of 10 users.",
}
],
response_format=UserDetailsResponse,
)
users = completion.choices[0].message.parsed.usersThis also lines up with the Structured Outputs schema constraints: the top-level schema is normally an object, and arrays should be placed under a named property. The current Structured Outputs docs describe the supported schema subset here: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to adjust the
response_formatto instead be anIterable[UserDetail]and have the response return an array of UserDetail's rather than a single.Explicitly setting
response_format=Iterable[UserDetail]gives me the error:TypeError: issubclass() arg 1 must be a class.I was able to get this to work using Instructor's implementation, so it's possible. But I'm not sure how they're making it work.
Beta Was this translation helpful? Give feedback.
All reactions