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

Fix "Invalid value for 'content': expected a string, got null." openai error in case of empty assistant messages #17921

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

andrewisplinghoff
Copy link
Contributor

@andrewisplinghoff andrewisplinghoff commented Feb 25, 2025

Description

This is a fix for OpenAI errors such as:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value for 'content': expected a string, got null.", 'type': 'invalid_request_error', 'param': 'messages.[24].content', 'code': None}}

They happen when there are messages in the chat history from the assistant that do not contain text, but are also not function or tool calls. E.g. in our application, that can happen if the user cancels a request and therefore there was no response received.

According to docs at https://platform.openai.com/docs/api-reference/chat/create, if neither tool_calls nor function_call is set, content must not be set to None.

Please check if maybe additional checks are required additionally to the pure check for existance of 'function_call' / 'tool_calls' keys in message.additional_kwargs being performed right now - should we maybe also check that those are not None?

Version Bump?

Did I bump the version in the pyproject.toml file of the package I am updating? (Except for the llama-index-core package)

  • Yes

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Your pull-request will likely not be merged unless it is covered by some form of impactful unit testing.

  • I added new unit tests to cover this change

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant