Skip to content

client.beta.chat.completions.parse returns tool_calls with empty array, which is invalid for message history #2061

Closed
@in-op

Description

@in-op

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

When using client.beta.chat.completions.parse, and with no tools involved, the ParsedChatCompletionMessage response will include a tool_calls with the empty array [] instead of None. This is invalid when including that in the message history you pass to subsequent calls to client.beta.chat.completions.parse or client.chat.completions.create, and will blow up with:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid 'messages[1].tool_calls': empty array. Expected an array with minimum length 1, but got an empty array instead.", 'type': 'invalid_request_error', 'param': 'messages[1].tool_calls', 'code': 'empty_array'}}

To Reproduce

Run any client.beta.chat.completions.parse call and append that message to a message history of a subsequent request.

Code snippets

import openai

client = openai.OpenAI()

messages = [
    {"role": "user", "content": "What color is the sky?"},
]

completion = client.beta.chat.completions.parse(
    model="gpt-4o-mini",
    messages=messages,
)

print(completion.choices[0].message)
messages.append(completion.choices[0].message)
messages.append(
    {"role": "user", "content": "What color is the ocean?"},
)

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
)

print(completion.choices[0].message)

OS

macOS

Python version

Python 3.12.5

Library version

openai==1.60.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions