diff --git a/llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/utils.py b/llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/utils.py index febd688a2d5e55a5a010ab93b634774f1211c885..7fde73a16c8dd931521a5d298d4bcea8cca5a828 100644 --- a/llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/utils.py +++ b/llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/utils.py @@ -323,11 +323,16 @@ def to_openai_message_dict( raise ValueError(msg) # 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 content_txt = ( 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 ) diff --git a/llama-index-integrations/llms/llama-index-llms-openai/pyproject.toml b/llama-index-integrations/llms/llama-index-llms-openai/pyproject.toml index 9024a5658ccfcb4e3ac1dc98d595dd6655e0b7af..572492a362bbda80f69768d38288640524d9a0d5 100644 --- a/llama-index-integrations/llms/llama-index-llms-openai/pyproject.toml +++ b/llama-index-integrations/llms/llama-index-llms-openai/pyproject.toml @@ -29,7 +29,7 @@ exclude = ["**/BUILD"] license = "MIT" name = "llama-index-llms-openai" readme = "README.md" -version = "0.3.20" +version = "0.3.21" [tool.poetry.dependencies] python = ">=3.9,<4.0" diff --git a/llama-index-integrations/llms/llama-index-llms-openai/tests/test_openai_utils.py b/llama-index-integrations/llms/llama-index-llms-openai/tests/test_openai_utils.py index c255b7e617ebaada22f52201c8876254c6cab5a8..a9a114212821ca62816d5d054a9780381bacaa4a 100644 --- a/llama-index-integrations/llms/llama-index-llms-openai/tests/test_openai_utils.py +++ b/llama-index-integrations/llms/llama-index-llms-openai/tests/test_openai_utils.py @@ -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( chat_messages_with_function_calling: List[ChatMessage], openai_message_dicts_with_function_calling: List[ChatCompletionMessageParam],