Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
Forward vision attachments for DeepSeek vision models (#1498)
wocessade4c7cced57ce6
diff --git a/src/free_claude_code/providers/deepseek/compat.py b/src/free_claude_code/providers/deepseek/compat.pyindex d941c06d..f62503ff 100644--- a/src/free_claude_code/providers/deepseek/compat.py+++ b/src/free_claude_code/providers/deepseek/compat.py@@ -39,12 +39,25 @@ _UNSUPPORTED_MESSAGE_BLOCK_TYPES = frozenset(})_STRIPPABLE_MESSAGE_BLOCK_TYPES = frozenset({"image", "document"})+_FORWARDABLE_ATTACHMENT_BLOCK_TYPES = frozenset({"image"})_OMITTED_ATTACHMENT_TEXT = ("[attachment omitted: DeepSeek does not support image or document inputs]")_OMITTED_ATTACHMENT_BLOCK = {"type": "text", "text": _OMITTED_ATTACHMENT_TEXT}+def _is_vision_capable_model(model: str | None) -> bool:+ """True when the DeepSeek model accepts OpenAI image parts.++ DeepSeek's vision models (e.g. ``deepseek-v4-flash-vision-exp``) support+ image inputs natively. Document blocks are still stripped because the+ shared OpenAI conversion path has no document parts.+ """+ if not model:+ return False+ return "vision" in str(model).rsplit("/", 1)[-1].lower()++def build_deepseek_request_body(request_data: MessagesRequest, *, reasoning: ReasoningPolicy) -> dict:@@ -55,10 +68,13 @@ def build_deepseek_request_body(len(request_data.messages),)+ vision_capable = _is_vision_capable_model(request_data.model)data = dump_messages_request(request_data)if "messages" in data:- data["messages"] = _strip_unsupported_attachment_blocks(data["messages"])- _validate_deepseek_request_dict(data)+ data["messages"] = _strip_unsupported_attachment_blocks(+ data["messages"], allow_attachments=vision_capable+ )+ _validate_deepseek_request_dict(data, allow_attachments=vision_capable)_downgrade_forced_tool_choice(data)has_tool_history = _has_tool_history(data)@@ -155,7 +171,9 @@ def sanitize_deepseek_messages_for_openai(messages: Any) -> Any:return sanitized-def _strip_unsupported_attachment_blocks(messages: Any) -> Any:+def _strip_unsupported_attachment_blocks(+ messages: Any, *, allow_attachments: bool = False+) -> Any:if not isinstance(messages, list):return messages@@ -179,6 +197,12 @@ def _strip_unsupported_attachment_blocks(messages: Any) -> Any:if isinstance(block, dict):btype = block.get("type")if btype in _STRIPPABLE_MESSAGE_BLOCK_TYPES:+ if (+ allow_attachments+ and btype in _FORWARDABLE_ATTACHMENT_BLOCK_TYPES+ ):+ new_content.append(block)+ continuetop_level_dropped[btype] = top_level_dropped.get(btype, 0) + 1message_dropped_attachment = Truecontinue@@ -192,6 +216,12 @@ def _strip_unsupported_attachment_blocks(messages: Any) -> Any:and sub.get("type") in _STRIPPABLE_MESSAGE_BLOCK_TYPES):sub_type = sub["type"]+ if (+ allow_attachments+ and sub_type in _FORWARDABLE_ATTACHMENT_BLOCK_TYPES+ ):+ filtered_inner.append(sub)+ continuenested_dropped[sub_type] = (nested_dropped.get(sub_type, 0) + 1)@@ -234,7 +264,9 @@ def _is_server_listed_tool(tool: Mapping[str, Any]) -> bool:return False-def _walk_block_list_for_unsupported(blocks: Any, *, where: str) -> None:+def _walk_block_list_for_unsupported(+ blocks: Any, *, where: str, allow_attachments: bool = False+) -> None:if not isinstance(blocks, list):returnfor block in blocks:@@ -242,16 +274,22 @@ def _walk_block_list_for_unsupported(blocks: Any, *, where: str) -> None:continuebtype = block.get("type")if btype in _UNSUPPORTED_MESSAGE_BLOCK_TYPES:+ if allow_attachments and btype in _FORWARDABLE_ATTACHMENT_BLOCK_TYPES:+ continueraise InvalidRequestError(f"DeepSeek native does not support {btype!r} blocks ({where}).")if btype == "tool_result" and "content" in block:_walk_block_list_for_unsupported(- block["content"], where=f"{where} (tool_result content)"+ block["content"],+ where=f"{where} (tool_result content)",+ allow_attachments=allow_attachments,)-def _validate_deepseek_request_dict(data: dict[str, Any]) -> None:+def _validate_deepseek_request_dict(+ data: dict[str, Any], *, allow_attachments: bool = False+) -> None:mcp = data.get("mcp_servers")if mcp:raise InvalidRequestError("DeepSeek does not support mcp_servers on requests.")@@ -270,11 +308,17 @@ def _validate_deepseek_request_dict(data: dict[str, Any]) -> None:continuecontent = message.get("content")if isinstance(content, list):- _walk_block_list_for_unsupported(content, where=f"messages[{i}].content")+ _walk_block_list_for_unsupported(+ content,+ where=f"messages[{i}].content",+ allow_attachments=allow_attachments,+ )system = data.get("system")if isinstance(system, list):- _walk_block_list_for_unsupported(system, where="system")+ _walk_block_list_for_unsupported(+ system, where="system", allow_attachments=allow_attachments+ )def _has_tool_history_blocks(message: Mapping[str, Any]) -> bool:diff --git a/tests/providers/test_deepseek.py b/tests/providers/test_deepseek.pyindex 7748a926..89e7ac20 100644--- a/tests/providers/test_deepseek.py+++ b/tests/providers/test_deepseek.py@@ -10,6 +10,7 @@ from free_claude_code.application.errors import InvalidRequestErrorfrom free_claude_code.config.constants import ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENSfrom free_claude_code.config.provider_catalog import DEEPSEEK_DEFAULT_BASEfrom free_claude_code.core.anthropic.models import (+ ContentBlockDocument,ContentBlockImage,Message,MessagesRequest,@@ -716,6 +717,106 @@ def test_preflight_strips_user_image():assert "image or document inputs" in content.lower()+def test_vision_model_forwards_user_image():+ """Vision-capable DeepSeek models receive image blocks as OpenAI image_url parts."""+ request = MessagesRequest(+ model="deepseek-v4-flash-vision-exp",+ messages=[+ Message(+ role="user",+ content=[+ ContentBlockImage(+ type="image",+ source={+ "type": "base64",+ "media_type": "image/png",+ "data": "YQ==",+ },+ ),+ {"type": "text", "text": "Describe this image"},+ ],+ )+ ],+ )+ provider = DeepSeekProvider(+ make_provider_config(+ api_key="k",+ base_url=DEEPSEEK_DEFAULT_BASE,+ rate_limit=1,+ rate_window=1,+ ),+ admission=immediate_admission(),+ )+ # Must not raise on preflight (no InvalidRequestError for image blocks).+ provider.preflight_stream(request, reasoning=REASONING_ON)+ body = provider._build_request_body(request, reasoning=reasoning_for(request))+ content = body["messages"][0]["content"]+ assert isinstance(content, list)+ image_parts = [+ part+ for part in content+ if isinstance(part, dict) and part.get("type") == "image_url"+ ]+ assert len(image_parts) == 1+ assert image_parts[0]["image_url"]["url"] == "data:image/png;base64,YQ=="+ text_parts = [+ part+ for part in content+ if isinstance(part, dict) and part.get("type") == "text"+ ]+ assert text_parts[0]["text"] == "Describe this image"+++def test_vision_model_strips_user_document():+ """Vision models still omit documents; conversion has no document parts."""+ request = MessagesRequest(+ model="deepseek-v4-flash-vision-exp",+ messages=[+ Message(+ role="user",+ content=[+ ContentBlockDocument(+ type="document",+ source={+ "type": "base64",+ "media_type": "application/pdf",+ "data": "YQ==",+ },+ )+ ],+ )+ ],+ )+ provider = DeepSeekProvider(+ make_provider_config(+ api_key="k",+ base_url=DEEPSEEK_DEFAULT_BASE,+ rate_limit=1,+ rate_window=1,+ ),+ admission=immediate_admission(),+ )+ provider.preflight_stream(request, reasoning=REASONING_ON)+ body = provider._build_request_body(request, reasoning=reasoning_for(request))+ content = body["messages"][0]["content"]+ assert content+ if isinstance(content, str):+ lowered = content.lower()+ else:+ texts = [+ part.get("text", "")+ for part in content+ if isinstance(part, dict) and part.get("type") == "text"+ ]+ lowered = "\n".join(texts).lower()+ assert not any(+ isinstance(part, dict) and part.get("type") == "image_url"+ for part in content+ )+ assert "attachment omitted" in lowered+ assert "image or document inputs" in lowered++def test_preflight_rejects_mcp_servers():request = MessagesRequest(model="m",