Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/dev-guides/14-model-capability-runtime-degradation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

1. **唯一调用路径** — 所有 Agent 通过 `Client.get_completion()` 或 `Client.get_completion_with_tools()` 调用 LLM,两者内部均走 `litellm.completion()`。
2. **`drop_params=True`** — LiteLLM 自动丢弃模型不支持的参数(如 `reasoning_effort`、`parallel_tool_calls`),不会报错。
3. **图片降级** — 如果模型不支持图片,`_is_image_deserialize_error()` 捕获异常后自动剥离图片重试。
3. **图片降级** — 如果模型不支持图片,`_is_image_deserialize_error()` 捕获异常后自动剥离图片重试;对于信息不明确的上游请求失败,仅在原请求确实包含图片时降级。
4. **无前端预检查** — 前端始终允许用户上传图片;后端自动处理。

## Client API
Expand Down
30 changes: 26 additions & 4 deletions py-src/data_formulator/agents/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,32 @@ def _strip_images_from_messages(self, messages):
sanitized_messages.append(msg)
return sanitized_messages

def _is_image_deserialize_error(self, error_text: str) -> bool:
def _messages_contain_images(self, messages) -> bool:
"""Return whether messages contain an image_url content block."""
return any(
isinstance(msg, dict)
and isinstance(msg.get("content"), list)
and any(
isinstance(item, dict) and item.get("type") == "image_url"
for item in msg["content"]
)
for msg in messages
)

def _is_image_deserialize_error(self, error_text: str, has_images: bool = False) -> bool:
"""Detect provider errors caused by image blocks on text-only models."""
lowered = error_text.lower()
return ("image_url" in lowered and "expected `text`" in lowered) or "unknown variant `image_url`" in lowered
return (
("image_url" in lowered and "expected `text`" in lowered)
or "unknown variant `image_url`" in lowered
or (
has_images
and (
"upstream request failed" in lowered
or ("image" in lowered and ("not support" in lowered or "unsupported" in lowered))
)
)
)

def _is_reasoning_effort_error(self, error_text: str) -> bool:
"""Detect provider errors caused by an unsupported ``reasoning_effort``
Expand Down Expand Up @@ -393,7 +415,7 @@ def get_completion(self, messages, stream=False, reasoning_effort="low",
if self._is_reasoning_effort_error(err):
params.pop("reasoning_effort", None)
return self._dispatch(messages=messages, stream=stream, params=params)
if self._is_image_deserialize_error(err):
if self._is_image_deserialize_error(err, self._messages_contain_images(messages)):
sanitized = self._strip_images_from_messages(messages)
return self._dispatch(messages=sanitized, stream=stream, params=params)
raise
Expand All @@ -416,7 +438,7 @@ def get_completion_with_tools(self, messages, tools, stream=False,
params.pop("reasoning_effort", None)
return self._dispatch(messages=messages, stream=stream,
params=params, tools=tools, extra=kwargs)
if self._is_image_deserialize_error(err):
if self._is_image_deserialize_error(err, self._messages_contain_images(messages)):
sanitized = self._strip_images_from_messages(messages)
return self._dispatch(messages=sanitized, stream=stream,
params=params, tools=tools, extra=kwargs)
Expand Down
38 changes: 38 additions & 0 deletions tests/backend/agents/test_client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Model name prefixing for gemini / anthropic / ollama
- Ollama api_base normalisation (trailing /api stripping)
- Image block stripping helpers (_strip_image_blocks, _strip_images_from_messages)
- Image request detection (_messages_contain_images)
- Image deserialise error detection (_is_image_deserialize_error)
- Client.from_config constructor
"""
Expand Down Expand Up @@ -203,6 +204,43 @@ def test_partial_match_image_url_without_expected(self):
"""'image_url' alone without 'expected `text`' should NOT match."""
assert self.client._is_image_deserialize_error("received image_url block") is False

def test_upstream_failure_with_images_detected(self):
err = "400: Error from provider (OpenCode Go): Upstream request failed"
assert self.client._is_image_deserialize_error(err, has_images=True) is True

def test_upstream_failure_without_images_not_detected(self):
err = "400: Error from provider (OpenCode Go): Upstream request failed"
assert self.client._is_image_deserialize_error(err, has_images=False) is False

def test_unsupported_image_message_with_images_detected(self):
err = "The selected model does not support image inputs"
assert self.client._is_image_deserialize_error(err, has_images=True) is True


# ---------------------------------------------------------------------------
# _messages_contain_images
# ---------------------------------------------------------------------------

class TestMessagesContainImages:
def setup_method(self):
self.client = Client("openai", "gpt-4o", api_key="k")

def test_multimodal_message_detected(self):
messages = [
{"role": "user", "content": [
{"type": "text", "text": "Describe this"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}},
]},
]
assert self.client._messages_contain_images(messages) is True

def test_text_only_messages_not_detected(self):
messages = [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": [{"type": "text", "text": "Describe this"}]},
]
assert self.client._messages_contain_images(messages) is False


# ---------------------------------------------------------------------------
# Client.from_config
Expand Down