Files
DeepTutor/tests/services/llm/test_request_compat.py
Rudy Celekli 81557468c2 fix(llm): detect response_format value rejections that omit the param name
is_response_format_unsupported gates every marker behind an explicit
"response_format" mention. But its own docstring lists DashScope's
rejection — "'json_object' is not supported by this model" — which names
the value, not the parameter, so the guard returned False and the
graceful drop-response_format-and-retry never fired; the JSON call hard-
failed instead.

Let the value-specific markers (json_object, json_schema, the
response_format.type messages) stand on their own, and keep the generic
"unsupported"/"not valid" markers gated behind the response_format
mention so an unrelated unsupported-parameter error (e.g. stream_options)
still does not strip response_format.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-01 09:25:44 -05:00

131 lines
5.3 KiB
Python

from __future__ import annotations
import httpx
from deeptutor.services.llm.request_compat import (
error_text,
is_image_input_unsupported,
is_response_format_unsupported,
is_stream_options_unsupported,
is_tool_schema_unsupported,
is_transient_transport_error,
)
class _Response:
text = "Unsupported parameter: stream_options"
class _ProviderError(Exception):
response = _Response()
def test_error_text_prefers_provider_response_body() -> None:
assert error_text(_ProviderError("generic message")) == (
"unsupported parameter: stream_options"
)
def test_request_compatibility_classifiers_match_known_provider_errors() -> None:
assert is_stream_options_unsupported(ValueError("unknown parameter: stream_options"))
assert is_tool_schema_unsupported(ValueError("function_declaration is unsupported"))
assert is_tool_schema_unsupported(ValueError("unsupported parameter: tools"))
assert is_tool_schema_unsupported(ValueError("unknown parameter: tools"))
assert is_tool_schema_unsupported(
ValueError("404_NOT_FOUND: function schema endpoint is unavailable")
)
assert is_image_input_unsupported(ValueError("content must be a string"))
def test_request_compatibility_classifiers_ignore_unrelated_errors() -> None:
error = RuntimeError("rate limit exceeded")
assert not is_stream_options_unsupported(error)
assert not is_tool_schema_unsupported(error)
assert not is_image_input_unsupported(error)
def test_tool_schema_classifier_ignores_errors_that_only_mention_tool() -> None:
"""Bare ``tool`` must not strip schemas — that silently disabled tools (#708)."""
# A temperature / max_tokens rejection that happens to echo the tools list.
echoed = ValueError("unsupported parameter: temperature. request included tools=[web_search]")
assert not is_tool_schema_unsupported(echoed)
assert not is_tool_schema_unsupported(RuntimeError("tool call timed out"))
assert not is_tool_schema_unsupported(ValueError("failed while invoking tool web_search"))
assert not is_tool_schema_unsupported(
ValueError("404_NOT_FOUND: model endpoint /v1/models/missing was not found")
)
def test_transient_transport_classifier_walks_wrapped_causes() -> None:
try:
try:
raise httpx.ReadError("peer closed the stream")
except httpx.ReadError as cause:
raise RuntimeError("provider request failed") from cause
except RuntimeError as error:
assert is_transient_transport_error(error)
def test_transient_transport_classifier_excludes_provider_rejections() -> None:
assert not is_transient_transport_error(RuntimeError("401 invalid API key"))
assert not is_transient_transport_error(RuntimeError("429 rate limit exceeded"))
def test_logged_error_text_is_bounded() -> None:
"""A provider that echoes the request back must not fill the log with it.
The log line exists so a user can attach it to a bug report, so an
unbounded body would put the request — tool schemas, sometimes the
messages — into a file headed for a public issue.
"""
from deeptutor.services.llm.request_compat import (
_MAX_LOGGED_ERROR_CHARS,
logged_error_text,
)
short = ValueError("unsupported parameter: tools")
assert logged_error_text(short) == "unsupported parameter: tools"
huge = ValueError("x" * (_MAX_LOGGED_ERROR_CHARS + 500))
bounded = logged_error_text(huge)
assert bounded.startswith("x" * 50)
assert len(bounded) < _MAX_LOGGED_ERROR_CHARS + 40
assert bounded.endswith("(+500 chars)")
def test_glm_text_only_content_rejection_is_recognized() -> None:
for path in ("messages.content.type", "messages[0].content[1].type"):
assert is_image_input_unsupported(ValueError(f"{path} 参数非法,取值范围 ['text']"))
def test_glm_unrelated_parameter_ranges_do_not_trigger_image_retry() -> None:
for message in (
"temperature 参数非法,取值范围 [0, 1]",
"content.type 参数非法,取值范围 ['text']",
"messages.content.type 参数非法,取值范围 ['text', 'audio']",
"messages.content.type is unavailable",
):
assert not is_image_input_unsupported(ValueError(message))
def test_response_format_classifier_matches_value_specific_rejections() -> None:
"""Providers that reject a response_format *value* without naming the
parameter must still be detected so the drop-and-retry fires. DashScope
answers ``'json_object' is not supported by this model`` (#docstring)."""
assert is_response_format_unsupported(
ValueError("'json_object' is not supported by this model")
)
assert is_response_format_unsupported(
ValueError("'response_format.type' must be 'json_schema' or 'text'")
)
assert is_response_format_unsupported(ValueError("response_format is not supported"))
def test_response_format_classifier_ignores_unrelated_unsupported_params() -> None:
"""A generic unsupported-parameter error that never mentions response_format
must not strip it (e.g. stream_options)."""
assert not is_response_format_unsupported(ValueError("stream_options is not supported"))
assert not is_response_format_unsupported(ValueError("unknown parameter: temperature"))
assert not is_response_format_unsupported(RuntimeError("rate limit exceeded"))