Skip to content

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

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

Closed
1 task done
in-op opened this issue Jan 28, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@in-op
Copy link

in-op commented Jan 28, 2025

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

@in-op in-op added the bug Something isn't working label Jan 28, 2025
@RobertCraigie
Copy link
Collaborator

Thanks for the report and including a reproduction script! This will be fixed in the next release #2095

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

No branches or pull requests

2 participants