from __future__ import annotations import httpx from deeptutor.services.llm.request_compat import ( error_text, is_image_input_unsupported, is_reasoning_effort_tools_incompatible, is_response_format_unsupported, is_stream_options_unsupported, is_tool_schema_unsupported, is_transient_transport_error, without_reasoning_effort, ) 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")) def test_reasoning_effort_tools_incompatibility_does_not_strip_tools() -> None: """Rejecting tools+reasoning_effort must route to dropping reasoning_effort, not tools (#2439).""" exc = ValueError( "400 Function tools with reasoning_effort are not supported for " "gpt-6-luna in /v1/chat/completions." ) assert is_reasoning_effort_tools_incompatible(exc) assert not is_tool_schema_unsupported(exc) assert not is_reasoning_effort_tools_incompatible(ValueError("unsupported parameter: tools")) assert not is_reasoning_effort_tools_incompatible(RuntimeError("rate limit exceeded")) def test_without_reasoning_effort_strips_top_level_and_extra_body() -> None: assert without_reasoning_effort({"model": "gpt-6-luna", "tools": []}) is None top = {"model": "gpt-6-luna", "reasoning_effort": "high", "tools": [{"name": "web_search"}]} stripped_top = without_reasoning_effort(top) assert stripped_top == {"model": "gpt-6-luna", "tools": [{"name": "web_search"}]} assert "reasoning_effort" in top # original dict untouched extra_only = {"model": "m", "extra_body": {"reasoning_effort": "medium"}} assert without_reasoning_effort(extra_only) == {"model": "m"} extra_mixed = {"model": "m", "extra_body": {"reasoning_effort": "medium", "other": 1}} assert without_reasoning_effort(extra_mixed) == {"model": "m", "extra_body": {"other": 1}}