Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pydantic dataclasses in structured outputs #1614

Closed
1 task done
9dogs opened this issue Aug 7, 2024 · 3 comments
Closed
1 task done

Support pydantic dataclasses in structured outputs #1614

9dogs opened this issue Aug 7, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@9dogs
Copy link

9dogs commented Aug 7, 2024

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • This is a feature request for the Python library

Describe the feature or improvement you're requesting

It would be great to be able to use data schemas defined as pydantic dataclasses in structured outputs. E.g.

from pydantic.dataclasses import dataclass
from openai import OpenAI

client = OpenAI()

@dataclass
class CalendarEvent:
    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

Pydantic dataclasses can be easily transformed into JSON schema via the model_json_schema function.

Additional context

No response

@UtsavChokshiCNU
Copy link

@9dogs ... Are you able to even run the code from their tutorial with pydanitc?
It returns me an error :
AttributeError: 'Completions' object has no attribute 'parse'

@parth126
Copy link

parth126 commented Aug 8, 2024

@UtsavChokshiCNU You need to update the api version to the latest version. Older version did not have parse attribute

@RobertCraigie
Copy link
Collaborator

Support for pydantic.dataclasses.dataclass has been added in v1.42.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants