Skip to content
Snippets Groups Projects
Unverified Commit d5b2e951 authored by Andre Wisplinghoff's avatar Andre Wisplinghoff Committed by GitHub
Browse files

Fix "Invalid value for 'content': expected a string, got null." openai error...

Fix "Invalid value for 'content': expected a string, got null." openai error in case of empty assistant messages (#17921)
parent 57c696ad
No related branches found
No related tags found
No related merge requests found
...@@ -323,11 +323,16 @@ def to_openai_message_dict( ...@@ -323,11 +323,16 @@ def to_openai_message_dict(
raise ValueError(msg) raise ValueError(msg)
# NOTE: Sending a null value (None) for Tool Message to OpenAI will cause error # NOTE: Sending a null value (None) for Tool Message to OpenAI will cause error
# It's only Allowed to send None if it's an Assistant Message # It's only Allowed to send None if it's an Assistant Message and either a function call or tool calls were performed
# Reference: https://platform.openai.com/docs/api-reference/chat/create # Reference: https://platform.openai.com/docs/api-reference/chat/create
content_txt = ( content_txt = (
None None
if content_txt == "" and message.role == MessageRole.ASSISTANT if content_txt == ""
and message.role == MessageRole.ASSISTANT
and (
"function_call" in message.additional_kwargs
or "tool_calls" in message.additional_kwargs
)
else content_txt else content_txt
) )
......
...@@ -29,7 +29,7 @@ exclude = ["**/BUILD"] ...@@ -29,7 +29,7 @@ exclude = ["**/BUILD"]
license = "MIT" license = "MIT"
name = "llama-index-llms-openai" name = "llama-index-llms-openai"
readme = "README.md" readme = "README.md"
version = "0.3.20" version = "0.3.21"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = ">=3.9,<4.0" python = ">=3.9,<4.0"
......
...@@ -156,6 +156,22 @@ def test_to_openai_message_dicts_basic_string() -> None: ...@@ -156,6 +156,22 @@ def test_to_openai_message_dicts_basic_string() -> None:
] ]
def test_to_openai_message_dicts_empty_content() -> None:
"""If neither `tool_calls` nor `function_call` is set, content must not be set to None,
see: https://platform.openai.com/docs/api-reference/chat/create"""
chat_messages = [
ChatMessage(role="user", content="test question"),
ChatMessage(role="assistant", content=""),
]
openai_messages = to_openai_message_dicts(
chat_messages,
)
assert openai_messages == [
{"role": "user", "content": "test question"},
{"role": "assistant", "content": ""},
]
def test_to_openai_message_dicts_function_calling( def test_to_openai_message_dicts_function_calling(
chat_messages_with_function_calling: List[ChatMessage], chat_messages_with_function_calling: List[ChatMessage],
openai_message_dicts_with_function_calling: List[ChatCompletionMessageParam], openai_message_dicts_with_function_calling: List[ChatCompletionMessageParam],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment