Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

4.2 KB
Raw Blame
from __future__ import annotations

import httpx

from deeptutor.services.llm.request_compat import (
    error_text,
    is_image_input_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))