Skip to content

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

fix: scope GLM image retries to text-only content validation

Bingxi Zhao (Frank)55f7e9a7a5b9
diff --git a/deeptutor/services/llm/request_compat.py b/deeptutor/services/llm/request_compat.py
index e0b7f3300..d6188dadc 100644
--- a/deeptutor/services/llm/request_compat.py
+++ b/deeptutor/services/llm/request_compat.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import re
+
import httpx
from .exceptions import LLMProviderTransportError, LLMTimeoutError
@@ -184,7 +186,14 @@ def is_tool_schema_unsupported(exc: Exception) -> bool:
def is_image_input_unsupported(exc: Exception) -> bool:
"""Whether a provider or model rejected multimodal message content."""
text = error_text(exc)
- return any(
+ # GLM's dotted-path validation only indicates unsupported image content
+ # when the message content type explicitly permits text and nothing else.
+ text_only_content = re.search(
+ r"messages(?:\[\d+\])?\.content(?:\[\d+\])?\.type"
+ r"[^\n]*取值范围\s*[::]?\s*\[\s*['\"]text['\"]\s*\]",
+ text,
+ )
+ return bool(text_only_content) or any(
marker in text
for marker in (
"image",
@@ -196,11 +205,6 @@ def is_image_input_unsupported(exc: Exception) -> bool:
"expected a string",
"expected string",
"invalid type for 'messages",
- # Zhipu/GLM rejects multimodal blocks with a Chinese message whose
- # path is dotted ("messages.content.type ... 参数非法,取值范围 ['text']"),
- # which none of the English markers above match.
- "content.type",
- "取值范围",
)
)
diff --git a/tests/services/llm/test_request_compat.py b/tests/services/llm/test_request_compat.py
index 4ecf28580..000bbf506 100644
--- a/tests/services/llm/test_request_compat.py
+++ b/tests/services/llm/test_request_compat.py
@@ -91,3 +91,18 @@ def test_logged_error_text_is_bounded() -> None:
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))