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

ChatCompletionAssistantMessageParam is incorrectly typed #1433

Closed
1 task done
willbakst opened this issue May 21, 2024 · 5 comments · Fixed by openai/openai-openapi#269
Closed
1 task done

ChatCompletionAssistantMessageParam is incorrectly typed #1433

willbakst opened this issue May 21, 2024 · 5 comments · Fixed by openai/openai-openapi#269
Labels
bug Something isn't working

Comments

@willbakst
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

The ChatCompletionAssistantMessageParam has the incorrect types for the function_call, name, and tool_calls fields. All of these fields should be marked Optional[...] but they are not.

While this doesn't break the type itself, downstream it prevents the use of pydantic with these types.

To Reproduce

Run the code in the snippet below.

pydantic will try to validate ChatCompletionAssistantMessageParam and fail because function_call is None (as returned by the API). I would expect either (1) that the returned message does not include the function_call field like it excludes the name field or (2) for the ChatCompletionAssistantMessageParam type to allow for function_call to be None in it's type definition.

Code snippets

from openai.types.chat import ChatCompletionAssistantMessageParam
from pydantic import BaseModel

class MyModel(BaseModel):
    history: list[ChatCompletionAssistantMessageParam]

history = [
    {
        "content": None,
        "role": "assistant",
        "function_call": None,
        "tool_calls": [
            {
                "id": "id",
                "function": {
                    "arguments": '{"location":"Tokyo, Japan"}',
                    "name": "GetCurrentWeather",
                },
                "type": "function",
            }
        ],
    },
]
my_model = MyModel(history=history)
print(my_model)

OS

14.2.1

Python version

3.9.16

Library version

1.30.1

@rattrayalex
Copy link
Collaborator

Sorry, I believe this is working as intended. You aren't supposed to send "function_call": null in the API, so the type 'funcion_call': None is not allowed.

Perhaps @RobertCraigie has an idea on how to better handle the pydantic issue you mention.

@rattrayalex rattrayalex closed this as not planned Won't fix, can't repro, duplicate, stale May 26, 2024
@willbakst
Copy link
Author

The main reason I brought this up is that the documentation shows inserting response.choices[0].message back into the messages array (line 57 of the code example), and that returned message has 'function_call': None, so either the type is incorrect and that value should be allowed or the return value shouldn't have function_call in it at all.

Of course, the downstream effect here is that when using Pydantic it fails with a ValidationError unless I manually remove function_call since None isn't a valid option.

@rattrayalex
Copy link
Collaborator

rattrayalex commented May 28, 2024

Gotcha, thanks. Okay so I think the backend has actually changed recently to allow this to be null, I'll reflect that in the OAS so the type is fixed as you suggest.

On the Pydantic side, you may want to use our static ChatCompletionMessage.construct(…) helper to skip the validation – does that help?

@willbakst
Copy link
Author

For me the issue is that I have my own Pydantic model with the message as a field, so the validation fails on my own object (and I want to validation to make sure I'm sending the right message structure).

@david-mykoai
Copy link

The tool_calls parameter is also marked as optional in the OpenAI API reference. Could this be fixed as well?
In fact, passing in an empty array no longer works for me, passing in None is the only way I could get the API to not return an error.

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

Successfully merging a pull request may close this issue.

3 participants