From 9e8e80f7ab1b3bdf0af59f32e9b29d40be9f9386 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sun, 1 Nov 2026 09:23:46 -0500 Subject: [PATCH] fix(llm): make the tool-format-error regex marker actually match MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _is_tool_format_error classifies a 400 so a non-streaming tool call can fall back to streaming (e.g. DashScope Coding Plan rejecting malformed tool arguments). It tested every marker with `kw in text`, but one marker, "invalid.*parameter.*function", is a regex: as a substring it only matches text that literally contains ".*", which no error body does, so that marker was dead and the "invalid ... parameter ... function" shape it was added for never triggered the fallback — the tool turn hard-failed instead. Search that marker with re.search while keeping the two literal markers as substring checks. Co-Authored-By: Claude Opus 4.8 --- .../provider_core/openai_compat_provider.py | 12 ++++------ .../test_openai_compat_responses_fallback.py | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/deeptutor/services/llm/provider_core/openai_compat_provider.py b/deeptutor/services/llm/provider_core/openai_compat_provider.py index b29bbb333..e38eb6db4 100644 --- a/deeptutor/services/llm/provider_core/openai_compat_provider.py +++ b/deeptutor/services/llm/provider_core/openai_compat_provider.py @@ -1031,13 +1031,11 @@ class OpenAICompatProvider(LLMProvider): well-formed response. """ text = str(getattr(e, "body", None) or getattr(e, "message", None) or e).lower() - return any( - kw in text - for kw in ( - "function.arguments", - "must be in json format", - "invalid.*parameter.*function", - ) + return ( + any(kw in text for kw in ("function.arguments", "must be in json format")) + # Regex marker — must be searched, not membership-tested, or the + # ``.*`` is taken literally and the marker never matches. + or re.search(r"invalid.*parameter.*function", text) is not None ) async def chat( diff --git a/tests/services/llm/test_openai_compat_responses_fallback.py b/tests/services/llm/test_openai_compat_responses_fallback.py index deb75d4e1..83270e983 100644 --- a/tests/services/llm/test_openai_compat_responses_fallback.py +++ b/tests/services/llm/test_openai_compat_responses_fallback.py @@ -111,3 +111,26 @@ class TestResponsesReasoningReplayFallback: assert result.content == "fallback answer" assert calls == {"responses": 1, "chat": 1} + + +class TestToolFormatErrorClassification: + """Non-streaming tool calls fall back to streaming only when the 400 is a + tool-argument-format error. The ``invalid.*parameter.*function`` marker is a + regex and must be searched, not membership-tested.""" + + @pytest.mark.parametrize( + "message", + [ + "error: function.arguments must be valid JSON", + "Arguments must be in JSON format", + "Invalid request: parameter 'tools[0].function' is invalid", + "invalid value for parameter in function definition", + ], + ) + def test_tool_format_errors_are_detected(self, message: str) -> None: + err = _FakeHTTPError(400, message) + assert OpenAICompatProvider._is_tool_format_error(err) is True + + def test_unrelated_error_is_not_a_tool_format_error(self) -> None: + err = _FakeHTTPError(500, "internal server error") + assert OpenAICompatProvider._is_tool_format_error(err) is False