From d0adb59a8aac637bcae14d7be090aec8251e6fa8 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:26:54 -0700 Subject: [PATCH] fix(completion): keep the assistant fill for a role-less function_call turn --- litellm/utils.py | 7 ++++--- tests/unit/test_utils.py | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index 4d326995a19..299d43f0367 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -9127,14 +9127,15 @@ def validate_and_fix_openai_messages( """ Ensures all messages are valid OpenAI chat completion messages. - Only an assistant turn carries tool_calls, so one sent without a role gets "assistant" (issue #8661). + Only an assistant turn carries tool_calls or function_call, so one sent without a role gets "assistant" + (issue #8661). """ new_messages: Final = [] for message in messages: if message.get("tool_calls"): message["tool_calls"] = jsonify_tools(tools=message["tool_calls"]) - if not message.get("role"): - message["role"] = "assistant" + if not message.get("role") and (message.get("tool_calls") or message.get("function_call")): + message["role"] = "assistant" convert_msg_to_dict = cast( # cast-ok: message dicts satisfy the TypedDict shape AllMessageValues, diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 20738d78b45..adf49a11894 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -8291,10 +8291,21 @@ def test_completion_message_without_role_is_a_400(messages: list[dict[str, str | @pytest.mark.usefixtures("_vcr_outcome_gate", "setup_and_teardown") -def test_completion_role_less_tool_call_turn_is_sent_as_assistant(): +@pytest.mark.parametrize( + "call_turn", + [ + { + "content": "", + "tool_calls": [ + {"id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": "{}"}} + ], + }, + {"content": None, "function_call": {"name": "get_weather", "arguments": "{}"}}, + ], +) +def test_completion_role_less_tool_call_turn_is_sent_as_assistant(call_turn: dict[str, object]): """Only an assistant turn carries tool calls, so one replayed without a role still goes upstream as assistant.""" - tool_call: Final = {"id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": "{}"}} - messages: Final = [{"role": "user", "content": "Weather in Paris?"}, {"content": "", "tool_calls": [tool_call]}] + messages: Final = [{"role": "user", "content": "Weather in Paris?"}, call_turn] assert validate_and_fix_openai_messages(messages)[1]["role"] == "assistant"