From 0374f74bb70d13d2b2582ba3b5d184f762e2fc15 Mon Sep 17 00:00:00 2001 From: "Arthur K." Date: Tue, 15 Sep 2026 19:32:42 +0300 Subject: [PATCH] fix(providers): preserve assistant content with tool calls (#5783) * fix(providers/openai-compatible): config option to preserve message content with tool calls * fix(providers): preserve assistant content with tool calls --------- Co-authored-by: chengyongru <2755839590@qq.com> --- nanobot/providers/openai_compat_provider.py | 4 --- tests/providers/test_litellm_kwargs.py | 33 +++++++++++++++++++-- tests/providers/test_mistral_provider.py | 23 ++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/nanobot/providers/openai_compat_provider.py b/nanobot/providers/openai_compat_provider.py index c43b85a7d..2d3de903a 100644 --- a/nanobot/providers/openai_compat_provider.py +++ b/nanobot/providers/openai_compat_provider.py @@ -785,10 +785,6 @@ class OpenAICompatProvider(LLMProvider): tc_clean["function"] = function_clean normalized.append(tc_clean) clean["tool_calls"] = normalized - if clean.get("role") == "assistant": - # Some OpenAI-compatible gateways reject assistant messages - # that mix non-empty content with tool_calls. - clean["content"] = None if "tool_call_id" in clean and clean["tool_call_id"]: clean["tool_call_id"] = map_tool_result_id(clean["tool_call_id"]) if ( diff --git a/tests/providers/test_litellm_kwargs.py b/tests/providers/test_litellm_kwargs.py index c53c2830d..917463f82 100644 --- a/tests/providers/test_litellm_kwargs.py +++ b/tests/providers/test_litellm_kwargs.py @@ -1101,12 +1101,39 @@ def test_openai_compat_preserves_message_level_reasoning_fields() -> None: {"role": "user", "content": "thanks"}, ]) - assert sanitized[1]["content"] is None + assert sanitized[1]["content"] == "done" assert sanitized[1]["reasoning_content"] == "hidden" assert sanitized[1]["extra_content"] == {"debug": True} assert sanitized[1]["tool_calls"][0]["extra_content"] == {"google": {"thought_signature": "sig"}} +def test_openai_compat_replays_tool_call_commentary_to_model() -> None: + spec = find_by_name("openai") + assert spec is not None + with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"): + provider = OpenAICompatProvider(spec=spec) + + kwargs = provider._build_kwargs( + messages=[ + {"role": "user", "content": "check the files"}, + { + "role": "assistant", + "content": "I am checking the profile first.", + "tool_calls": [_tool_call("call_1")], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "done"}, + ], + tools=None, + model="gpt-5", + max_tokens=1024, + temperature=0.7, + reasoning_effort="high", + tool_choice=None, + ) + + assert kwargs["messages"][1]["content"] == "I am checking the profile first." + + def _deepseek_kwargs(messages: list[dict]) -> dict: with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"): provider = OpenAICompatProvider( @@ -1194,7 +1221,7 @@ def test_openai_compat_preserves_tool_call_ids_after_consecutive_assistant_messa ]) assert sanitized[1]["role"] == "assistant" - assert sanitized[1]["content"] is None + assert sanitized[1]["content"] == "我再查一下" assert sanitized[1]["tool_calls"][0]["id"] == "call_function_akxp3wqzn7ph_1" assert sanitized[2]["tool_call_id"] == "call_function_akxp3wqzn7ph_1" @@ -1222,7 +1249,7 @@ def test_mistral_normalizes_tool_call_ids_after_consecutive_assistant_messages() ]) assert sanitized[1]["role"] == "assistant" - assert sanitized[1]["content"] is None + assert sanitized[1]["content"] == "我再查一下" assert sanitized[1]["tool_calls"][0]["id"] == "3ec83c30d" assert sanitized[2]["tool_call_id"] == "3ec83c30d" diff --git a/tests/providers/test_mistral_provider.py b/tests/providers/test_mistral_provider.py index 4f429add7..72658d087 100644 --- a/tests/providers/test_mistral_provider.py +++ b/tests/providers/test_mistral_provider.py @@ -353,3 +353,26 @@ def test_mistral_tool_call_ids_get_normalized() -> None: assert len(assistant_id) == 9 assert assistant_id.isalnum() assert assistant_id == tool_id + + +def test_mistral_preserves_text_with_tool_call_history() -> None: + p = _mistral_provider() + messages = [ + {"role": "user", "content": "check"}, + { + "role": "assistant", + "content": "I will inspect it.", + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "read_file", "arguments": "{}"}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "done"}, + ] + + sanitized = p._sanitize_messages(messages) + + assert sanitized[1]["content"] == "I will inspect it."