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

beta.chat.completions.parse is not working with response_format json object or json schema after upgrading to 1.55.3 #1914

Open
1 task done
SunkadGit opened this issue Dec 1, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@SunkadGit
Copy link

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

Getting Type error
TypeError: No method for generating JsonSchema for core_schema.type='invalid' (expected: GenerateJsonSchema.invalid_schema)

To Reproduce

Use beta.chat.completions.parse with pydantic object in response_format

Code snippets

No response

OS

windows

Python version

3.11

Library version

1.55.3

@SunkadGit SunkadGit added the bug Something isn't working label Dec 1, 2024
@RobertCraigie
Copy link
Collaborator

Can you share the BaseModel class you're using?

@paulhyuk
Copy link

paulhyuk commented Dec 9, 2024

Doesn't work for me either on v1.57.1. The following code (not including imports):

class Response(BaseModel):
    operations: List[Dict]
    explanation: str

completion = client.beta.chat.completions.parse(
    model="gpt-4o-2024-08-06",
    messages=[
        {"role": "system", "content": "You are a data transformation expert that provides precise, actionable JSON responses."},
        {"role": "user", "content": prompt}
    ],
    response_format=Response,
)
response = completion.choices[0].message.parsed

Returns the following error:

    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'Response': In context=(), 'required' is required to be supplied and to be an array including every key in properties. Extra required key 'operations' supplied.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}

@RobertCraigie
Copy link
Collaborator

@paulhyuk unfortunately arbitrary properties aren't supported in structured outputs yet https://platform.openai.com/docs/guides/structured-outputs#additionalproperties-false-must-always-be-set-in-objects, so you'll need to give a different definition for opeerations: List[Dict]

@paulhyuk
Copy link

paulhyuk commented Dec 9, 2024

Thanks @RobertCraigie -- does the API support Unions for the structured output? For example the below doesn't seem to work as a response_format

class OperationResponse(BaseModel):
    operation: str
    parameters: Rename|Drop|Create|FillNa|Datetime

@SunkadGit
Copy link
Author

SunkadGit commented Dec 10, 2024

For me it worked when downgraded openai library to 1.51.0

This is the Basemodel:
from pydantic import BaseModel
from typing import List

class FieldExtraction(BaseModel):
fieldname: str
fieldvalue: str

class Classification(BaseModel):
page_number: int
classification: str
similarity: float
all_similarities: List[float]
extraction: List[FieldExtraction]
employeeID: str
emailID: str

class Classifications(BaseModel):
classifications: List[Classification]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants