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

fix(json schema): support recursive BaseModels in Pydantic v1 #1623

Merged
merged 2 commits into from
Aug 8, 2024

Conversation

Craq
Copy link
Contributor

@Craq Craq commented Aug 8, 2024

Pydantic 2 introduced $defs and removed definitions. The latter is not handled during json parsing in "Structured Outputs" mode, so additionalProperties: False is not set and the request fails with pydantic<2 with nested objects in the schema, as they are not getting the flag set.

Added definitions in _ensure_strict_json_schema to correctly parse the schema with pydantic<2 and not fail on requests with nested objects.

Steps to reproduce the error:
openai==1.40.1
pydantic==1.10.12

from pydantic import BaseModel
from openai import OpenAI

client = OpenAI(api_key=OPENAI_API_KEY)

class Step(BaseModel):
    explanation: str
    output: str

class MathReasoning(BaseModel):
    final_answer: str
    step: list[Step]

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

@Craq Craq requested a review from a team as a code owner August 8, 2024 12:25
@RobertCraigie
Copy link
Collaborator

Thanks!

We actually did have a test case for this but it looks like we aren't running our Pydantic v1 tests in the CI for this repo, in the meantime, can you run this command and update and push the results?

nox -s test-pydantic-v1 -- tests/lib/ --inline-snapshot=fix

@Craq
Copy link
Contributor Author

Craq commented Aug 8, 2024

@RobertCraigie Done.

Copy link
Collaborator

@RobertCraigie RobertCraigie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@RobertCraigie RobertCraigie changed the title Added backward compatibility to pydantic < 2 during schema parsing fix(parsing): support recursive BaseModels in Pydantic v1 Aug 8, 2024
@RobertCraigie RobertCraigie changed the title fix(parsing): support recursive BaseModels in Pydantic v1 fix(json schema): support recursive BaseModels in Pydantic v1 Aug 8, 2024
@RobertCraigie RobertCraigie changed the base branch from main to next August 8, 2024 13:32
@RobertCraigie RobertCraigie merged commit cc433c6 into openai:next Aug 8, 2024
2 checks passed
@stainless-app stainless-app bot mentioned this pull request Aug 8, 2024
megamanics pushed a commit to devops-testbed/openai-python that referenced this pull request Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants