Skip to content

Bug caused by incorrect usage of Pydantic and Typing Extensions #1704

@wuyouMaster

Description

@wuyouMaster

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

You define the JSONSchema class in the path openai.types.shared_params.response_format_json_schema.py as follow

class JSONSchema(TypedDict, total=False):
    name: Required[str]
    """The name of the response format.

    Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length
    of 64.
    """

    description: str
    """
    A description of what the response format is for, used by the model to determine
    how to respond in the format.
    """

    schema: Dict[str, object]
    """The schema for the response format, described as a JSON Schema object."""

    strict: Optional[bool]
    """Whether to enable strict schema adherence when generating the output.

    If set to true, the model will always follow the exact schema defined in the
    `schema` field. Only a subset of JSON Schema is supported when `strict` is
    `true`. To learn more, read the
    [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
    """

Then, you used it in each class where you defined the format of the response body, as follows.

ResponseFormat: TypeAlias = Union[ResponseFormatText, ResponseFormatJSONObject, ResponseFormatJSONSchema]

class CompletionCreateParamsBase(TypedDict, total=False):
       .
       .
       .
      response_format: ResponseFormat

However, the field named 'schema' is a built-in field in Pydantic. When you convert this model to Pydantic, it triggers the following error.
NameError: Field name "schema" shadows a BaseModel attribute; use a different field name with "alias='schema'
This bug exists in version 1.40 and later versions.

To Reproduce

from openai.types.chat.completion_create_params import CompletionCreateParamsNonStreaming
from pydantic.v1 import create_model_from_typedict
2. ref = create_model_from_typedict(CompletionCreateParamsNonStreaming)
3. Then error occurred

Code snippets

No response

OS

any

Python version

any

Library version

openai v1.40.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions