Skip to content

ChatCompletion does not handle functions = None #538

Description

@badrul1

Describe the bug

Firstly I noticed how you have integrated the chat_completion_request function into the Python OpenAI package instead of writing it out in code. I have used prior version and the new version but am finding the newer integrated version giving me issues.

Specificaly it seems that it does not handle the case where functions = None very well. There does not appear to be a way to tell 'ChatCompletion' to not run any functions other than giving it a fake function and then setting the function_call flag to 'none'.

Example error:
error_code=None error_message="None is not of type 'array' - 'functions'" error_param=None error_type=invalid_request_error message='OpenAI API error received' stream_error=False

It would be good if it could proces requests when functions is set to none and function_call is set to 'none'.

To Reproduce

Pass in functions=Non and function_call='none' to the code below.

Code snippets

def chat_completion_request(messages, functions=None, function_call="auto", model=GPT_MODEL):
    if functions is None: 
        function_call ="none"
   
    r= openai.ChatCompletion.create(
        model=GPT_MODEL,
        messages=messages,
        functions=functions,
        function_call=function_call,  # auto is default, but we'll be explicit
    )
    return r

OS

Windows 11

Python version

Latest

Library version

Latest

Activity

  1. badrul1 commented on Jul 13, 2023

    @badrul1
    Author

    Realised I was forgetting to return the result with a .json() at the end.

  2. jeremy-fisher-nd commented on Jul 25, 2023

    @jeremy-fisher-nd

    It looks like if you dig all the way to the API request, the params (i.e., all keyword arguments except api_key, api_base, api_type, request_id, api_version and organization) dictionary is serialized to json here: https://github.com/openai/openai-python/blob/b82a3f7e4c462a8a10fa445193301a3cefef9a4a/openai/api_requestor.py#L553C37-L553C37

    That's probably going to result in different behavior if you specify functions=None vs specifying nothing at all.

  3. RobertCraigie commented on Nov 6, 2023

    @RobertCraigie
    Contributor

    This is "fixed" in the v1 release as we've moved to an explicit None vs omitted model where we have an internal sentinel value to signify if an argument was given or not.

    If you try to pass functions=None you'll get a static type error and the API will raise an error at runtime.

  4. added a commit that references this issue on Apr 27, 2025
    616d8e7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed in v1Issues addressed by the v1 beta

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions