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
fixed deepseek issue
Alishahryar1847916b1d3d8
diff --git a/providers/deepseek/request.py b/providers/deepseek/request.pyindex 06c00fa9..e2a016d8 100644--- a/providers/deepseek/request.py+++ b/providers/deepseek/request.py@@ -82,6 +82,99 @@ def _validate_deepseek_native_request_dict(data: dict[str, Any]) -> None:_walk_block_list_for_unsupported(system, where="system")+def _has_tool_history_blocks(message: Mapping[str, Any]) -> bool:+ role = message.get("role")+ content = message.get("content")+ if not isinstance(content, list):+ return False++ for block in content:+ if not isinstance(block, dict):+ continue+ btype = block.get("type")+ if role == "assistant" and btype == "tool_use":+ return True+ if role == "user" and btype == "tool_result":+ return True+ return False+++def _has_replayable_thinking_before_tool_use(message: Mapping[str, Any]) -> bool:+ if message.get("role") != "assistant":+ return False+ content = message.get("content")+ if not isinstance(content, list):+ return False++ has_thinking = False+ for block in content:+ if not isinstance(block, dict):+ continue+ btype = block.get("type")+ if btype == "thinking" and isinstance(block.get("thinking"), str):+ has_thinking = bool(block["thinking"])+ continue+ if btype == "tool_use":+ return has_thinking+ return False+++def _has_tool_history(data: dict[str, Any]) -> bool:+ for message in data.get("messages") or ():+ if isinstance(message, Mapping) and _has_tool_history_blocks(message):+ return True+ return False+++def _has_replayable_tool_thinking(data: dict[str, Any]) -> bool:+ for message in data.get("messages") or ():+ if isinstance(message, Mapping) and _has_replayable_thinking_before_tool_use(+ message+ ):+ return True+ return False+++def _remove_deepseek_thinking_hints(data: dict[str, Any]) -> None:+ """Remove request hints that can keep DeepSeek in thinking mode after fallback."""+ output_config = data.get("output_config")+ if isinstance(output_config, dict) and "effort" in output_config:+ cleaned_output_config = dict(output_config)+ cleaned_output_config.pop("effort", None)+ if cleaned_output_config:+ data["output_config"] = cleaned_output_config+ else:+ data.pop("output_config", None)++ context_management = data.get("context_management")+ if not isinstance(context_management, dict):+ return+ edits = context_management.get("edits")+ if not isinstance(edits, list):+ return+ filtered_edits = [+ edit+ for edit in edits+ if not (+ isinstance(edit, dict)+ and isinstance(edit.get("type"), str)+ and edit["type"].startswith("clear_thinking_")+ )+ ]+ if len(filtered_edits) == len(edits):+ return+ cleaned_context_management = dict(context_management)+ if filtered_edits:+ cleaned_context_management["edits"] = filtered_edits+ data["context_management"] = cleaned_context_management+ else:+ cleaned_context_management.pop("edits", None)+ if cleaned_context_management:+ data["context_management"] = cleaned_context_management+ else:+ data.pop("context_management", None)++def sanitize_deepseek_messages_for_native(messages: Any, *, thinking_enabled: bool) -> Any:@@ -151,8 +244,39 @@ def build_request_body(request_data: Any, *, thinking_enabled: bool) -> dict:_validate_deepseek_native_request_dict(data)data.pop("extra_body", None)+ has_tool_history = _has_tool_history(data)+ has_replayable_tool_thinking = _has_replayable_tool_thinking(data)+ unsafe_tool_followup = has_tool_history and not has_replayable_tool_thinking+ effective_thinking_enabled = thinking_enabled and not unsafe_tool_followup+ if thinking_enabled:+ if unsafe_tool_followup:+ logger.debug(+ "DEEPSEEK_REQUEST: disabling thinking for tool follow-up without "+ "replayable thinking model={} msgs={} tools={}",+ data.get("model"),+ len(data.get("messages", [])),+ len(data.get("tools", [])),+ )+ _remove_deepseek_thinking_hints(data)+ elif has_tool_history:+ logger.debug(+ "DEEPSEEK_REQUEST: keeping thinking for tool follow-up with "+ "replayable thinking model={} msgs={} tools={}",+ data.get("model"),+ len(data.get("messages", [])),+ len(data.get("tools", [])),+ )+ elif data.get("tools") or data.get("tool_choice"):+ logger.debug(+ "DEEPSEEK_REQUEST: keeping thinking for initial tool request "+ "model={} msgs={} tools={}",+ data.get("model"),+ len(data.get("messages", [])),+ len(data.get("tools", [])),+ )+thinking_cfg = data.pop("thinking", None)- if thinking_enabled and isinstance(thinking_cfg, dict):+ if effective_thinking_enabled and isinstance(thinking_cfg, dict):thinking_payload: dict[str, Any] = {"type": "enabled"}budget_tokens = thinking_cfg.get("budget_tokens")if isinstance(budget_tokens, int):@@ -163,7 +287,7 @@ def build_request_body(request_data: Any, *, thinking_enabled: bool) -> dict:data["messages"] = _strip_reasoning_content_when_native(sanitize_deepseek_messages_for_native(data["messages"],- thinking_enabled=thinking_enabled,+ thinking_enabled=effective_thinking_enabled,))if "max_tokens" not in data or data.get("max_tokens") is None:diff --git a/tests/providers/test_deepseek.py b/tests/providers/test_deepseek.pyindex 27e84953..ef1539cf 100644--- a/tests/providers/test_deepseek.py+++ b/tests/providers/test_deepseek.py@@ -115,6 +115,44 @@ def test_build_request_body_thinking_enabled(deepseek_provider):assert "extra_body" not in body+def test_build_request_body_tool_list_keeps_thinking(deepseek_provider):+ request = MessagesRequest.model_validate(+ {+ "model": "m",+ "messages": [{"role": "user", "content": "x"}],+ "tools": [+ {+ "name": "Read",+ "description": "Read a file",+ "input_schema": {"type": "object", "properties": {}},+ }+ ],+ "thinking": {"type": "enabled", "budget_tokens": 2000},+ }+ )++ body = deepseek_provider._build_request_body(request)++ assert body["thinking"] == {"type": "enabled", "budget_tokens": 2000}+ assert body["tools"][0]["name"] == "Read"+++def test_build_request_body_tool_choice_keeps_thinking(deepseek_provider):+ request = MessagesRequest.model_validate(+ {+ "model": "m",+ "messages": [{"role": "user", "content": "x"}],+ "tool_choice": {"type": "auto"},+ "thinking": {"type": "enabled", "budget_tokens": 2000},+ }+ )++ body = deepseek_provider._build_request_body(request)++ assert body["thinking"] == {"type": "enabled", "budget_tokens": 2000}+ assert body["tool_choice"] == {"type": "auto"}++def test_build_request_body_respects_global_thinking_disable():provider = DeepSeekProvider(ProviderConfig(@@ -183,6 +221,202 @@ def test_strip_redacted_thinking_when_thinking_on(deepseek_provider):assert "text" in types+def test_tool_history_with_replayable_thinking_preserves_thinking(deepseek_provider):+ request = MessagesRequest.model_validate(+ {+ "model": "m",+ "messages": [+ {+ "role": "assistant",+ "content": [+ {+ "type": "thinking",+ "thinking": "hidden",+ "signature": "sig_123",+ },+ {"type": "redacted_thinking", "data": "opaque"},+ {+ "type": "tool_use",+ "id": "t1",+ "name": "Read",+ "input": {"file_path": "x"},+ },+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "t1",+ "content": "ok",+ }+ ],+ },+ ],+ "thinking": {"type": "enabled", "budget_tokens": 2000},+ "context_management": {+ "edits": [{"type": "clear_thinking_20251015", "keep": "all"}]+ },+ "output_config": {"effort": "high"},+ }+ )++ body = deepseek_provider._build_request_body(request)++ assert body["thinking"] == {"type": "enabled", "budget_tokens": 2000}+ assert body["context_management"] == {+ "edits": [{"type": "clear_thinking_20251015", "keep": "all"}]+ }+ assert body["output_config"] == {"effort": "high"}+ assistant_blocks = body["messages"][0]["content"]+ assert [block["type"] for block in assistant_blocks] == ["thinking", "tool_use"]+ assert assistant_blocks[0]["thinking"] == "hidden"+ assert assistant_blocks[0]["signature"] == "sig_123"+ assert assistant_blocks[1]["name"] == "Read"+ assert body["messages"][1]["content"][0]["type"] == "tool_result"+++def test_tool_history_with_unsigned_thinking_preserves_thinking(deepseek_provider):+ request = MessagesRequest.model_validate(+ {+ "model": "m",+ "messages": [+ {+ "role": "assistant",+ "content": [+ {"type": "thinking", "thinking": "plain"},+ {+ "type": "tool_use",+ "id": "t1",+ "name": "Read",+ "input": {"file_path": "x"},+ },+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "t1",+ "content": "ok",+ }+ ],+ },+ ],+ "thinking": {"type": "enabled"},+ }+ )++ body = deepseek_provider._build_request_body(request)++ assert body["thinking"] == {"type": "enabled"}+ assert body["messages"][0]["content"][0] == {+ "type": "thinking",+ "thinking": "plain",+ }+++def test_tool_history_without_thinking_disables_thinking_and_hints(deepseek_provider):+ request = MessagesRequest.model_validate(+ {+ "model": "m",+ "messages": [+ {+ "role": "assistant",+ "content": [+ {+ "type": "tool_use",+ "id": "t1",+ "name": "Read",+ "input": {"file_path": "x"},+ },+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "t1",+ "content": "ok",+ }+ ],+ },+ ],+ "tools": [+ {+ "name": "Read",+ "description": "Read a file",+ "input_schema": {"type": "object", "properties": {}},+ }+ ],+ "tool_choice": {"type": "auto"},+ "thinking": {"type": "enabled", "budget_tokens": 2000},+ "context_management": {+ "edits": [+ {"type": "clear_thinking_20251015", "keep": "all"},+ {"type": "other_edit", "keep": "all"},+ ],+ "other": True,+ },+ "output_config": {"effort": "high", "format": "text"},+ }+ )++ body = deepseek_provider._build_request_body(request)++ assert "thinking" not in body+ assert body["context_management"] == {+ "edits": [{"type": "other_edit", "keep": "all"}],+ "other": True,+ }+ assert body["output_config"] == {"format": "text"}+ assert body["tools"][0]["name"] == "Read"+ assert body["tool_choice"] == {"type": "auto"}+ assert body["messages"][0]["content"][0]["type"] == "tool_use"+ assert body["messages"][1]["content"][0]["type"] == "tool_result"+++def test_tool_history_with_empty_thinking_disables_thinking(deepseek_provider):+ request = MessagesRequest.model_validate(+ {+ "model": "m",+ "messages": [+ {+ "role": "assistant",+ "content": [+ {"type": "thinking", "thinking": ""},+ {+ "type": "tool_use",+ "id": "t1",+ "name": "Read",+ "input": {"file_path": "x"},+ },+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "t1",+ "content": "ok",+ }+ ],+ },+ ],+ "thinking": {"type": "enabled"},+ }+ )++ body = deepseek_provider._build_request_body(request)++ assert "thinking" not in body+ assert [block["type"] for block in body["messages"][0]["content"]] == ["tool_use"]++def test_thinking_off_strips_thinking_history():provider = DeepSeekProvider(ProviderConfig(