openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
21.98k stars 3.03k forks source link

ChatCompletionSystemMessageParam.name is not optional but the description says it should be #1670

Closed fgmacedo closed 3 weeks ago

fgmacedo commented 3 weeks ago

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

Describe the bug

The class param ChatCompletionSystemMessageParam.name is described as being optional, in fact, the API works without issues if we don't pass this param. But the type is not marked as optional. (Same issue with ChatCompletionUserMessageParam.name and ChatCompletionAssistantMessageParam.name).

Instead of being declared as

class ChatCompletionSystemMessageParam(TypedDict, total=False):
    content: Required[Union[str, Iterable[ChatCompletionContentPartTextParam]]]
    """The contents of the system message."""

    role: Required[Literal["system"]]
    """The role of the messages author, in this case `system`."""

    name: str
    """An optional name for the participant.

    Provides the model information to differentiate between participants of the same
    role.
    """

It should be declared as:

class ChatCompletionSystemMessageParam(TypedDict, total=False):
    content: Required[Union[str, Iterable[ChatCompletionContentPartTextParam]]]
    """The contents of the system message."""

    role: Required[Literal["system"]]
    """The role of the messages author, in this case `system`."""

    name:  Optional[str]
    """An optional name for the participant.

    Provides the model information to differentiate between participants of the same
    role.
    """

To Reproduce

Given this test file:

# test.py
import openai

client = openai.OpenAI()

response = client.beta.chat.completions.parse(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are an assistant."},  # <-  mypy claims this line has an error
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's up?"},
            ],
        },
    ],
)

Run:

mypy .

Output:

test.py:8: error: Type of TypedDict is ambiguous, none of ("ChatCompletionSystemMessageParam", "ChatCompletionUserMessageParam", "ChatCompletionAssistantMessageParam") matches cleanly  [misc]

Code snippets

No response

OS

ubuntu

Python version

Python 3.12.2

Library version

openai 1.42.0

RobertCraigie commented 3 weeks ago

What mypy version are you using? I can't reproduce on 1.11.1.

Regardless, this is not a bug in the library. The total=False keyword argument in the class definition means that all fields that don't use the Required type can be omitted.

Please report a bug with mypy if this continues to fail for you.

fgmacedo commented 3 weeks ago

Hi @RobertCraigie , thanks for your fast reply. I'm using the same mypy version. Ok, now I see the total=False... I don't know what's going on to make mypy sad. I'll skip the linter for this line. Best regards!