LastNoob commited on
Commit
a4c84e7
·
1 Parent(s): fa2e8f2

feat(deepseek): use native Anthropic Messages transport

Browse files

- Point DeepSeek at api.deepseek.com/anthropic with x-api-key headers
- Native request builder, DeepSeek-specific thinking/block sanitization
- Drop deepseek from OpenAI-chat server-tool preflight; update tests and docs
- Default smoke model deepseek-v4-pro; re-export dump_raw_messages_request

.env.example CHANGED
@@ -6,7 +6,7 @@ NVIDIA_NIM_API_KEY=""
6
  OPENROUTER_API_KEY=""
7
 
8
 
9
- # DeepSeek Config
10
  DEEPSEEK_API_KEY=""
11
 
12
 
 
6
  OPENROUTER_API_KEY=""
7
 
8
 
9
+ # DeepSeek Config (uses native Anthropic Messages at api.deepseek.com/anthropic)
10
  DEEPSEEK_API_KEY=""
11
 
12
 
README.md CHANGED
@@ -12,7 +12,7 @@
12
  [![Code style: Ruff](https://img.shields.io/badge/code%20formatting-ruff-f5a623.svg?style=for-the-badge)](https://github.com/astral-sh/ruff)
13
  [![Logging: Loguru](https://img.shields.io/badge/logging-loguru-4ecdc4.svg?style=for-the-badge)](https://github.com/Delgan/loguru)
14
 
15
- A lightweight proxy that routes Claude Code's Anthropic API calls to **NVIDIA NIM** (40 req/min free), **OpenRouter** (hundreds of models), **DeepSeek** (direct API), **LM Studio** (fully local), **llama.cpp** (local with Anthropic endpoints), or **Ollama** (fully local, native Anthropic Messages).
16
 
17
  [Quick Start](#quick-start) · [Providers](#providers) · [Discord Bot](#discord-bot) · [Configuration](#configuration) · [Development](#development) · [Contributing](#contributing)
18
 
@@ -375,7 +375,7 @@ The proxy also exposes Claude-compatible probe routes: `GET /v1/models`, `POST /
375
  | -------------- | ------------ | ---------- | ------------------------------------ |
376
  | **NVIDIA NIM** | Free | 40 req/min | Daily driver, generous free tier |
377
  | **OpenRouter** | Free / Paid | Varies | Model variety, fallback options |
378
- | **DeepSeek** | Usage-based | Varies | Direct access to DeepSeek chat/reasoner |
379
  | **LM Studio** | Free (local) | Unlimited | Privacy, offline use, no rate limits |
380
  | **llama.cpp** | Free (local) | Unlimited | Lightweight local inference engine |
381
  | **Ollama** | Free (local) | Unlimited | Easy local LLM runtime, native Anthropic API |
@@ -386,7 +386,7 @@ Models use a prefix format: `provider_prefix/model/name`. An invalid prefix caus
386
  | ---------- | ----------------- | -------------------- | ----------------------------- |
387
  | NVIDIA NIM | `nvidia_nim/...` | `NVIDIA_NIM_API_KEY` | `integrate.api.nvidia.com/v1` |
388
  | OpenRouter | `open_router/...` | `OPENROUTER_API_KEY` | `openrouter.ai/api/v1` |
389
- | DeepSeek | `deepseek/...` | `DEEPSEEK_API_KEY` | `api.deepseek.com` |
390
  | LM Studio | `lmstudio/...` | (none) | `localhost:1234/v1` |
391
  | llama.cpp | `llamacpp/...` | (none) | `localhost:8080/v1` |
392
  | Ollama | `ollama/...` | (none) | `localhost:11434` |
@@ -423,10 +423,12 @@ Browse: [openrouter.ai/models](https://openrouter.ai/models) · [Free models](ht
423
  <details>
424
  <summary><b>DeepSeek models</b></summary>
425
 
426
- DeepSeek currently exposes the direct API models:
 
 
427
 
428
- - `deepseek/deepseek-chat`
429
- - `deepseek/deepseek-reasoner`
430
 
431
  Browse: [api-docs.deepseek.com](https://api-docs.deepseek.com)
432
 
 
12
  [![Code style: Ruff](https://img.shields.io/badge/code%20formatting-ruff-f5a623.svg?style=for-the-badge)](https://github.com/astral-sh/ruff)
13
  [![Logging: Loguru](https://img.shields.io/badge/logging-loguru-4ecdc4.svg?style=for-the-badge)](https://github.com/Delgan/loguru)
14
 
15
+ A lightweight proxy that routes Claude Code's Anthropic API calls to **NVIDIA NIM** (40 req/min free), **OpenRouter** (hundreds of models), **DeepSeek** (direct Anthropic-compatible API), **LM Studio** (fully local), **llama.cpp** (local with Anthropic endpoints), or **Ollama** (fully local, native Anthropic Messages).
16
 
17
  [Quick Start](#quick-start) · [Providers](#providers) · [Discord Bot](#discord-bot) · [Configuration](#configuration) · [Development](#development) · [Contributing](#contributing)
18
 
 
375
  | -------------- | ------------ | ---------- | ------------------------------------ |
376
  | **NVIDIA NIM** | Free | 40 req/min | Daily driver, generous free tier |
377
  | **OpenRouter** | Free / Paid | Varies | Model variety, fallback options |
378
+ | **DeepSeek** | Usage-based | Varies | Native Anthropic Messages on DeepSeek's API |
379
  | **LM Studio** | Free (local) | Unlimited | Privacy, offline use, no rate limits |
380
  | **llama.cpp** | Free (local) | Unlimited | Lightweight local inference engine |
381
  | **Ollama** | Free (local) | Unlimited | Easy local LLM runtime, native Anthropic API |
 
386
  | ---------- | ----------------- | -------------------- | ----------------------------- |
387
  | NVIDIA NIM | `nvidia_nim/...` | `NVIDIA_NIM_API_KEY` | `integrate.api.nvidia.com/v1` |
388
  | OpenRouter | `open_router/...` | `OPENROUTER_API_KEY` | `openrouter.ai/api/v1` |
389
+ | DeepSeek | `deepseek/...` | `DEEPSEEK_API_KEY` | `api.deepseek.com/anthropic` |
390
  | LM Studio | `lmstudio/...` | (none) | `localhost:1234/v1` |
391
  | llama.cpp | `llamacpp/...` | (none) | `localhost:8080/v1` |
392
  | Ollama | `ollama/...` | (none) | `localhost:11434` |
 
423
  <details>
424
  <summary><b>DeepSeek models</b></summary>
425
 
426
+ The `deepseek` provider uses DeepSeek's **Anthropic-compatible** `POST /v1/messages` entrypoint
427
+ (HTTP base `https://api.deepseek.com/anthropic`), not the OpenAI `chat/completions` API. Some
428
+ Anthropic request features are unsupported; see the DeepSeek API docs for limits.
429
 
430
+ - `deepseek/deepseek-v4-pro` / `deepseek/deepseek-v4-flash` (recommended for smokes and tools+thinking)
431
+ - `deepseek/deepseek-chat` / `deepseek/deepseek-reasoner` (older model ids may still be available)
432
 
433
  Browse: [api-docs.deepseek.com](https://api-docs.deepseek.com)
434
 
api/services.py CHANGED
@@ -33,7 +33,7 @@ TokenCounter = Callable[[list[Any], str | list[Any] | None, list[Any] | None], i
33
  ProviderGetter = Callable[[str], BaseProvider]
34
 
35
  # Providers that use ``/chat/completions`` + Anthropic-to-OpenAI conversion (not native Messages).
36
- _OPENAI_CHAT_UPSTREAM_IDS = frozenset({"nvidia_nim", "deepseek"})
37
 
38
 
39
  def anthropic_sse_streaming_response(
 
33
  ProviderGetter = Callable[[str], BaseProvider]
34
 
35
  # Providers that use ``/chat/completions`` + Anthropic-to-OpenAI conversion (not native Messages).
36
+ _OPENAI_CHAT_UPSTREAM_IDS = frozenset({"nvidia_nim"})
37
 
38
 
39
  def anthropic_sse_streaming_response(
api/web_tools/request.py CHANGED
@@ -79,7 +79,7 @@ def openai_chat_upstream_server_tool_error(
79
  )
80
  if not forced and has_listed_anthropic_server_tools(request):
81
  return (
82
- "OpenAI Chat upstreams (NVIDIA NIM, DeepSeek) cannot use listed Anthropic server tools "
83
  "(web_search / web_fetch) without the local web server tool handler. Use a native "
84
  "Anthropic transport, set ENABLE_WEB_SERVER_TOOLS=true and force the tool with "
85
  "tool_choice, or remove these tools from the request."
 
79
  )
80
  if not forced and has_listed_anthropic_server_tools(request):
81
  return (
82
+ "OpenAI Chat upstreams (NVIDIA NIM) cannot use listed Anthropic server tools "
83
  "(web_search / web_fetch) without the local web server tool handler. Use a native "
84
  "Anthropic transport, set ENABLE_WEB_SERVER_TOOLS=true and force the tool with "
85
  "tool_choice, or remove these tools from the request."
config/provider_catalog.py CHANGED
@@ -13,7 +13,10 @@ TransportType = Literal["openai_chat", "anthropic_messages"]
13
 
14
  # Default upstream base URLs (also re-exported via :mod:`providers.defaults`)
15
  NVIDIA_NIM_DEFAULT_BASE = "https://integrate.api.nvidia.com/v1"
16
- DEEPSEEK_DEFAULT_BASE = "https://api.deepseek.com"
 
 
 
17
  OPENROUTER_DEFAULT_BASE = "https://openrouter.ai/api/v1"
18
  LMSTUDIO_DEFAULT_BASE = "http://localhost:1234/v1"
19
  LLAMACPP_DEFAULT_BASE = "http://localhost:8080/v1"
@@ -59,12 +62,12 @@ PROVIDER_CATALOG: dict[str, ProviderDescriptor] = {
59
  ),
60
  "deepseek": ProviderDescriptor(
61
  provider_id="deepseek",
62
- transport_type="openai_chat",
63
  credential_env="DEEPSEEK_API_KEY",
64
  credential_url="https://platform.deepseek.com/api_keys",
65
  credential_attr="deepseek_api_key",
66
- default_base_url=DEEPSEEK_DEFAULT_BASE,
67
- capabilities=("chat", "streaming", "thinking"),
68
  ),
69
  "lmstudio": ProviderDescriptor(
70
  provider_id="lmstudio",
 
13
 
14
  # Default upstream base URLs (also re-exported via :mod:`providers.defaults`)
15
  NVIDIA_NIM_DEFAULT_BASE = "https://integrate.api.nvidia.com/v1"
16
+ # DeepSeek Anthropic-compatible Messages API (not OpenAI ``/v1`` chat completions).
17
+ DEEPSEEK_ANTHROPIC_DEFAULT_BASE = "https://api.deepseek.com/anthropic"
18
+ # Historical export name: DeepSeek upstream is the native Anthropic path above.
19
+ DEEPSEEK_DEFAULT_BASE = DEEPSEEK_ANTHROPIC_DEFAULT_BASE
20
  OPENROUTER_DEFAULT_BASE = "https://openrouter.ai/api/v1"
21
  LMSTUDIO_DEFAULT_BASE = "http://localhost:1234/v1"
22
  LLAMACPP_DEFAULT_BASE = "http://localhost:8080/v1"
 
62
  ),
63
  "deepseek": ProviderDescriptor(
64
  provider_id="deepseek",
65
+ transport_type="anthropic_messages",
66
  credential_env="DEEPSEEK_API_KEY",
67
  credential_url="https://platform.deepseek.com/api_keys",
68
  credential_attr="deepseek_api_key",
69
+ default_base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
70
+ capabilities=("chat", "streaming", "tools", "thinking", "native_anthropic"),
71
  ),
72
  "lmstudio": ProviderDescriptor(
73
  provider_id="lmstudio",
core/anthropic/native_messages_request.py CHANGED
@@ -109,6 +109,11 @@ def _dump_request_fields(request_data: Any) -> dict[str, Any]:
109
  return dumped
110
 
111
 
 
 
 
 
 
112
  def sanitize_native_messages_thinking_policy(
113
  messages: Any, *, thinking_enabled: bool
114
  ) -> Any:
 
109
  return dumped
110
 
111
 
112
+ def dump_raw_messages_request(request_data: Any) -> dict[str, Any]:
113
+ """Public JSON-ready dict of Anthropic public request fields (for native adapters)."""
114
+ return _dump_request_fields(request_data)
115
+
116
+
117
  def sanitize_native_messages_thinking_policy(
118
  messages: Any, *, thinking_enabled: bool
119
  ) -> Any:
providers/deepseek/__init__.py CHANGED
@@ -1,7 +1,11 @@
1
  """DeepSeek provider exports."""
2
 
3
- from providers.defaults import DEEPSEEK_DEFAULT_BASE
4
 
5
  from .client import DeepSeekProvider
6
 
7
- __all__ = ["DEEPSEEK_DEFAULT_BASE", "DeepSeekProvider"]
 
 
 
 
 
1
  """DeepSeek provider exports."""
2
 
3
+ from providers.defaults import DEEPSEEK_ANTHROPIC_DEFAULT_BASE, DEEPSEEK_DEFAULT_BASE
4
 
5
  from .client import DeepSeekProvider
6
 
7
+ __all__ = [
8
+ "DEEPSEEK_ANTHROPIC_DEFAULT_BASE",
9
+ "DEEPSEEK_DEFAULT_BASE",
10
+ "DeepSeekProvider",
11
+ ]
providers/deepseek/client.py CHANGED
@@ -1,30 +1,37 @@
1
- """DeepSeek provider implementation."""
 
 
2
 
3
  from typing import Any
4
 
 
5
  from providers.base import ProviderConfig
6
- from providers.defaults import DEEPSEEK_DEFAULT_BASE
7
- from providers.openai_compat import OpenAIChatTransport
8
 
9
  from .request import build_request_body
10
 
11
 
12
- class DeepSeekProvider(OpenAIChatTransport):
13
- """DeepSeek provider using OpenAI-compatible chat completions."""
14
 
15
  def __init__(self, config: ProviderConfig):
16
  super().__init__(
17
  config,
18
  provider_name="DEEPSEEK",
19
- base_url=config.base_url or DEEPSEEK_DEFAULT_BASE,
20
- api_key=config.api_key,
21
  )
22
 
23
  def _build_request_body(
24
  self, request: Any, thinking_enabled: bool | None = None
25
  ) -> dict:
26
- """Internal helper for tests and shared building."""
27
  return build_request_body(
28
  request,
29
  thinking_enabled=self._is_thinking_enabled(request, thinking_enabled),
30
  )
 
 
 
 
 
 
 
 
1
+ """DeepSeek provider implementation (native Anthropic-compatible Messages)."""
2
+
3
+ from __future__ import annotations
4
 
5
  from typing import Any
6
 
7
+ from providers.anthropic_messages import AnthropicMessagesTransport
8
  from providers.base import ProviderConfig
9
+ from providers.defaults import DEEPSEEK_ANTHROPIC_DEFAULT_BASE
 
10
 
11
  from .request import build_request_body
12
 
13
 
14
+ class DeepSeekProvider(AnthropicMessagesTransport):
15
+ """DeepSeek using ``https://api.deepseek.com/anthropic`` (Anthropic Messages API)."""
16
 
17
  def __init__(self, config: ProviderConfig):
18
  super().__init__(
19
  config,
20
  provider_name="DEEPSEEK",
21
+ default_base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
 
22
  )
23
 
24
  def _build_request_body(
25
  self, request: Any, thinking_enabled: bool | None = None
26
  ) -> dict:
 
27
  return build_request_body(
28
  request,
29
  thinking_enabled=self._is_thinking_enabled(request, thinking_enabled),
30
  )
31
+
32
+ def _request_headers(self) -> dict[str, str]:
33
+ return {
34
+ "Accept": "text/event-stream",
35
+ "Content-Type": "application/json",
36
+ "x-api-key": self._api_key,
37
+ }
providers/deepseek/request.py CHANGED
@@ -1,46 +1,180 @@
1
- """Request builder for DeepSeek provider."""
2
 
 
 
 
3
  from typing import Any
4
 
5
  from loguru import logger
6
 
7
- from core.anthropic import ReasoningReplayMode, build_base_request_body
8
- from core.anthropic.conversion import OpenAIConversionError
9
  from providers.exceptions import InvalidRequestError
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def build_request_body(request_data: Any, *, thinking_enabled: bool) -> dict:
13
- """Build OpenAI-format request body from Anthropic request for DeepSeek."""
14
  logger.debug(
15
- "DEEPSEEK_REQUEST: conversion start model={} msgs={}",
16
  getattr(request_data, "model", "?"),
17
  len(getattr(request_data, "messages", [])),
18
  )
19
- try:
20
- body = build_base_request_body(
21
- request_data,
22
- reasoning_replay=ReasoningReplayMode.REASONING_CONTENT
23
- if thinking_enabled
24
- else ReasoningReplayMode.DISABLED,
25
- )
26
- except OpenAIConversionError as exc:
27
- raise InvalidRequestError(str(exc)) from exc
28
 
29
- extra_body: dict[str, Any] = {}
30
- request_extra = getattr(request_data, "extra_body", None)
31
- if request_extra:
32
- extra_body.update(request_extra)
33
 
34
- if thinking_enabled and body.get("model") != "deepseek-reasoner":
35
- extra_body.setdefault("thinking", {"type": "enabled"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- if extra_body:
38
- body["extra_body"] = extra_body
39
 
40
  logger.debug(
41
- "DEEPSEEK_REQUEST: conversion done model={} msgs={} tools={}",
42
- body.get("model"),
43
- len(body.get("messages", [])),
44
- len(body.get("tools", [])),
45
  )
46
- return body
 
1
+ """Request builder and DeepSeek native Anthropic compatibility sanitizer."""
2
 
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Mapping
6
  from typing import Any
7
 
8
  from loguru import logger
9
 
10
+ from config.constants import ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS
11
+ from core.anthropic.native_messages_request import dump_raw_messages_request
12
  from providers.exceptions import InvalidRequestError
13
 
14
+ # Block types not supported on DeepSeek partial Anthropic-compatible API.
15
+ _UNSUPPORTED_MESSAGE_BLOCK_TYPES = frozenset(
16
+ {
17
+ "image",
18
+ "document",
19
+ "server_tool_use",
20
+ "web_search_tool_result",
21
+ "web_fetch_tool_result",
22
+ }
23
+ )
24
+
25
+
26
+ def _is_server_listed_tool(tool: Mapping[str, Any]) -> bool:
27
+ """True for Anthropic web_search / web_fetch-style tool definitions (listed tools)."""
28
+ name = (tool.get("name") or "").strip()
29
+ if name in ("web_search", "web_fetch"):
30
+ return True
31
+ typ = tool.get("type")
32
+ if isinstance(typ, str):
33
+ return typ.startswith("web_search") or typ.startswith("web_fetch")
34
+ return False
35
+
36
+
37
+ def _walk_block_list_for_unsupported(blocks: Any, *, where: str) -> None:
38
+ if not isinstance(blocks, list):
39
+ return
40
+ for block in blocks:
41
+ if not isinstance(block, dict):
42
+ continue
43
+ btype = block.get("type")
44
+ if btype in _UNSUPPORTED_MESSAGE_BLOCK_TYPES:
45
+ raise InvalidRequestError(
46
+ f"DeepSeek native does not support {btype!r} blocks ({where})."
47
+ )
48
+ if btype == "tool_result" and "content" in block:
49
+ _walk_block_list_for_unsupported(
50
+ block["content"], where=f"{where} (tool_result content)"
51
+ )
52
+
53
+
54
+ def _validate_deepseek_native_request_dict(data: dict[str, Any]) -> None:
55
+ mcp = data.get("mcp_servers")
56
+ if mcp:
57
+ raise InvalidRequestError(
58
+ "DeepSeek native does not support mcp_servers on requests."
59
+ )
60
+
61
+ for tool in data.get("tools") or ():
62
+ if not isinstance(tool, dict):
63
+ continue
64
+ if _is_server_listed_tool(tool):
65
+ raise InvalidRequestError(
66
+ "DeepSeek native does not support listed Anthropic server tools "
67
+ "(web_search / web_fetch). Remove them or use a different provider."
68
+ )
69
+
70
+ for i, message in enumerate(data.get("messages") or ()):
71
+ if not isinstance(message, dict):
72
+ continue
73
+ c = message.get("content")
74
+ if isinstance(c, list):
75
+ _walk_block_list_for_unsupported(c, where=f"messages[{i}].content")
76
+ if isinstance(c, str) and "<think>" in c:
77
+ # Unusual, but block encoded redacted content — treat as unsafe for DeepSeek.
78
+ pass
79
+
80
+ system = data.get("system")
81
+ if isinstance(system, list):
82
+ _walk_block_list_for_unsupported(system, where="system")
83
+
84
+
85
+ def sanitize_deepseek_messages_for_native(
86
+ messages: Any, *, thinking_enabled: bool
87
+ ) -> Any:
88
+ """Filter assistant content for DeepSeek: unsigned ``thinking`` is allowed; no ``redacted_thinking``."""
89
+ if not isinstance(messages, list):
90
+ return messages
91
+
92
+ sanitized: list[Any] = []
93
+ for message in messages:
94
+ if not isinstance(message, dict):
95
+ sanitized.append(message)
96
+ continue
97
+ if message.get("role") != "assistant":
98
+ sanitized.append(message)
99
+ continue
100
+ content = message.get("content")
101
+ if not isinstance(content, list):
102
+ sanitized.append(message)
103
+ continue
104
+
105
+ if not thinking_enabled:
106
+ filtered = [
107
+ block
108
+ for block in content
109
+ if not (
110
+ isinstance(block, dict)
111
+ and block.get("type") in ("thinking", "redacted_thinking")
112
+ )
113
+ ]
114
+ else:
115
+ filtered = [
116
+ block
117
+ for block in content
118
+ if not (
119
+ isinstance(block, dict) and block.get("type") == "redacted_thinking"
120
+ )
121
+ ]
122
+ new_msg = dict(message)
123
+ new_msg["content"] = filtered or ""
124
+ sanitized.append(new_msg)
125
+ return sanitized
126
+
127
+
128
+ def _strip_reasoning_content_when_native(messages: Any) -> Any:
129
+ """``reasoning_content`` is OpenAI-helper metadata; not part of native Anthropic body."""
130
+ if not isinstance(messages, list):
131
+ return messages
132
+ out: list[Any] = []
133
+ for m in messages:
134
+ if not isinstance(m, dict):
135
+ out.append(m)
136
+ continue
137
+ msg = {k: v for k, v in m.items() if k != "reasoning_content"}
138
+ out.append(msg)
139
+ return out
140
+
141
 
142
  def build_request_body(request_data: Any, *, thinking_enabled: bool) -> dict:
143
+ """Build a DeepSeek ``/v1/messages`` JSON body (Anthropic format)."""
144
  logger.debug(
145
+ "DEEPSEEK_REQUEST: native build model={} msgs={}",
146
  getattr(request_data, "model", "?"),
147
  len(getattr(request_data, "messages", [])),
148
  )
 
 
 
 
 
 
 
 
 
149
 
150
+ data = dump_raw_messages_request(request_data)
151
+ _validate_deepseek_native_request_dict(data)
152
+ data.pop("extra_body", None)
 
153
 
154
+ thinking_cfg = data.pop("thinking", None)
155
+ if thinking_enabled and isinstance(thinking_cfg, dict):
156
+ thinking_payload: dict[str, Any] = {"type": "enabled"}
157
+ budget_tokens = thinking_cfg.get("budget_tokens")
158
+ if isinstance(budget_tokens, int):
159
+ thinking_payload["budget_tokens"] = budget_tokens
160
+ data["thinking"] = thinking_payload
161
+
162
+ if "messages" in data:
163
+ data["messages"] = _strip_reasoning_content_when_native(
164
+ sanitize_deepseek_messages_for_native(
165
+ data["messages"],
166
+ thinking_enabled=thinking_enabled,
167
+ )
168
+ )
169
+ if "max_tokens" not in data or data.get("max_tokens") is None:
170
+ data["max_tokens"] = ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS
171
 
172
+ data["stream"] = True
 
173
 
174
  logger.debug(
175
+ "DEEPSEEK_REQUEST: build done model={} msgs={} tools={}",
176
+ data.get("model"),
177
+ len(data.get("messages", [])),
178
+ len(data.get("tools", [])),
179
  )
180
+ return data
providers/defaults.py CHANGED
@@ -1,6 +1,7 @@
1
  """Re-exports default upstream base URLs from the config provider catalog."""
2
 
3
  from config.provider_catalog import (
 
4
  DEEPSEEK_DEFAULT_BASE,
5
  LLAMACPP_DEFAULT_BASE,
6
  LMSTUDIO_DEFAULT_BASE,
@@ -10,6 +11,7 @@ from config.provider_catalog import (
10
  )
11
 
12
  __all__ = (
 
13
  "DEEPSEEK_DEFAULT_BASE",
14
  "LLAMACPP_DEFAULT_BASE",
15
  "LMSTUDIO_DEFAULT_BASE",
 
1
  """Re-exports default upstream base URLs from the config provider catalog."""
2
 
3
  from config.provider_catalog import (
4
+ DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
5
  DEEPSEEK_DEFAULT_BASE,
6
  LLAMACPP_DEFAULT_BASE,
7
  LMSTUDIO_DEFAULT_BASE,
 
11
  )
12
 
13
  __all__ = (
14
+ "DEEPSEEK_ANTHROPIC_DEFAULT_BASE",
15
  "DEEPSEEK_DEFAULT_BASE",
16
  "LLAMACPP_DEFAULT_BASE",
17
  "LMSTUDIO_DEFAULT_BASE",
providers/openai_compat.py CHANGED
@@ -1,6 +1,6 @@
1
- """OpenAI-style chat base for :class:`OpenAIChatTransport` (NIM, DeepSeek, etc.).
2
 
3
- ``AnthropicMessagesTransport``-based providers (OpenRouter, LM Studio, …) live
4
  in separate modules; do not list them as subclasses of this class.
5
  """
6
 
@@ -56,7 +56,7 @@ def _iter_heuristic_tool_use_sse(
56
 
57
 
58
  class OpenAIChatTransport(BaseProvider):
59
- """Base for OpenAI-compatible ``/chat/completions`` adapters (NIM, DeepSeek, …)."""
60
 
61
  def __init__(
62
  self,
 
1
+ """OpenAI-style chat base for :class:`OpenAIChatTransport` (NIM, etc.).
2
 
3
+ ``AnthropicMessagesTransport``-based providers (OpenRouter, LM Studio, DeepSeek, …) live
4
  in separate modules; do not list them as subclasses of this class.
5
  """
6
 
 
56
 
57
 
58
  class OpenAIChatTransport(BaseProvider):
59
+ """Base for OpenAI-compatible ``/chat/completions`` adapters (NIM, …)."""
60
 
61
  def __init__(
62
  self,
smoke/README.md CHANGED
@@ -34,7 +34,7 @@ uv run pytest smoke -n 0 -s --tb=short
34
  Provider product E2E runs once per configured provider, independent of `MODEL`,
35
  `MODEL_OPUS`, `MODEL_SONNET`, and `MODEL_HAIKU`. Defaults come from the provider
36
  catalog/docs and can be overridden with `FCC_SMOKE_MODEL_<PROVIDER>`, for example
37
- `FCC_SMOKE_MODEL_DEEPSEEK=deepseek-reasoner`. If no provider smoke model is
38
  configured, live product smoke fails as `missing_env` unless you explicitly set
39
  `FCC_ALLOW_NO_PROVIDER_SMOKE=1`.
40
 
 
34
  Provider product E2E runs once per configured provider, independent of `MODEL`,
35
  `MODEL_OPUS`, `MODEL_SONNET`, and `MODEL_HAIKU`. Defaults come from the provider
36
  catalog/docs and can be overridden with `FCC_SMOKE_MODEL_<PROVIDER>`, for example
37
+ `FCC_SMOKE_MODEL_DEEPSEEK=deepseek-v4-pro` (or `deepseek-v4-flash`). If no provider smoke model is
38
  configured, live product smoke fails as `missing_env` unless you explicitly set
39
  `FCC_ALLOW_NO_PROVIDER_SMOKE=1`.
40
 
smoke/lib/config.py CHANGED
@@ -40,7 +40,7 @@ SECRET_KEY_PARTS = ("KEY", "TOKEN", "SECRET", "WEBHOOK", "AUTH")
40
  PROVIDER_SMOKE_DEFAULT_MODELS: dict[str, str] = {
41
  "nvidia_nim": "nvidia_nim/z-ai/glm4.7",
42
  "open_router": "open_router/stepfun/step-3.5-flash:free",
43
- "deepseek": "deepseek/deepseek-chat",
44
  "lmstudio": "lmstudio/local-model",
45
  "llamacpp": "llamacpp/local-model",
46
  "ollama": "ollama/llama3.1",
 
40
  PROVIDER_SMOKE_DEFAULT_MODELS: dict[str, str] = {
41
  "nvidia_nim": "nvidia_nim/z-ai/glm4.7",
42
  "open_router": "open_router/stepfun/step-3.5-flash:free",
43
+ "deepseek": "deepseek/deepseek-v4-pro",
44
  "lmstudio": "lmstudio/local-model",
45
  "llamacpp": "llamacpp/local-model",
46
  "ollama": "ollama/llama3.1",
tests/api/test_dependencies.py CHANGED
@@ -154,7 +154,7 @@ async def test_get_provider_deepseek():
154
  provider = get_provider()
155
 
156
  assert isinstance(provider, DeepSeekProvider)
157
- assert provider._base_url == "https://api.deepseek.com"
158
  assert provider._api_key == "test_deepseek_key"
159
  assert provider._config.enable_thinking is True
160
 
@@ -170,7 +170,7 @@ async def test_get_provider_deepseek_uses_fixed_base_url():
170
  provider = get_provider()
171
 
172
  assert isinstance(provider, DeepSeekProvider)
173
- assert provider._base_url == "https://api.deepseek.com"
174
 
175
 
176
  @pytest.mark.asyncio
 
154
  provider = get_provider()
155
 
156
  assert isinstance(provider, DeepSeekProvider)
157
+ assert provider._base_url == "https://api.deepseek.com/anthropic"
158
  assert provider._api_key == "test_deepseek_key"
159
  assert provider._config.enable_thinking is True
160
 
 
170
  provider = get_provider()
171
 
172
  assert isinstance(provider, DeepSeekProvider)
173
+ assert provider._base_url == "https://api.deepseek.com/anthropic"
174
 
175
 
176
  @pytest.mark.asyncio
tests/api/test_web_server_tools.py CHANGED
@@ -584,7 +584,7 @@ def test_service_rejects_listed_server_tools_on_openai_chat() -> None:
584
  service = ClaudeProxyService(
585
  settings,
586
  provider_getter=lambda _: MagicMock(),
587
- model_router=FixedProviderModelRouter(settings, "deepseek"),
588
  )
589
  request = MessagesRequest(
590
  model="m",
 
584
  service = ClaudeProxyService(
585
  settings,
586
  provider_getter=lambda _: MagicMock(),
587
+ model_router=FixedProviderModelRouter(settings, "nvidia_nim"),
588
  )
589
  request = MessagesRequest(
590
  model="m",
tests/providers/test_deepseek.py CHANGED
@@ -1,50 +1,31 @@
1
- """Tests for DeepSeek provider."""
2
 
 
3
  from unittest.mock import AsyncMock, MagicMock, patch
4
 
5
  import pytest
6
 
7
- from api.models.anthropic import ContentBlockImage, Message, MessagesRequest
 
 
 
 
 
 
8
  from providers.base import ProviderConfig
9
- from providers.deepseek import DEEPSEEK_DEFAULT_BASE, DeepSeekProvider
 
 
 
 
10
  from providers.exceptions import InvalidRequestError
11
 
12
 
13
- class MockMessage:
14
- def __init__(self, role, content, reasoning_content=None):
15
- self.role = role
16
- self.content = content
17
- self.reasoning_content = reasoning_content
18
-
19
-
20
- class MockBlock:
21
- def __init__(self, **kwargs):
22
- for key, value in kwargs.items():
23
- setattr(self, key, value)
24
-
25
-
26
- class MockRequest:
27
- def __init__(self, **kwargs):
28
- self.model = "deepseek-chat"
29
- self.messages = [MockMessage("user", "Hello")]
30
- self.max_tokens = 100
31
- self.temperature = 0.5
32
- self.top_p = 0.9
33
- self.system = "System prompt"
34
- self.stop_sequences = None
35
- self.tools = []
36
- self.extra_body = {}
37
- self.thinking = MagicMock()
38
- self.thinking.enabled = True
39
- for key, value in kwargs.items():
40
- setattr(self, key, value)
41
-
42
-
43
  @pytest.fixture
44
  def deepseek_config():
45
  return ProviderConfig(
46
  api_key="test_deepseek_key",
47
- base_url=DEEPSEEK_DEFAULT_BASE,
48
  rate_limit=10,
49
  rate_window=60,
50
  enable_thinking=True,
@@ -53,15 +34,18 @@ def deepseek_config():
53
 
54
  @pytest.fixture(autouse=True)
55
  def mock_rate_limiter():
56
- """Mock the global rate limiter to prevent waiting."""
57
- with patch("providers.openai_compat.GlobalRateLimiter") as mock:
 
 
 
58
  instance = mock.get_scoped_instance.return_value
59
- instance.wait_if_blocked = AsyncMock(return_value=False)
60
 
61
  async def _passthrough(fn, *args, **kwargs):
62
  return await fn(*args, **kwargs)
63
 
64
  instance.execute_with_retry = AsyncMock(side_effect=_passthrough)
 
65
  yield instance
66
 
67
 
@@ -70,240 +54,202 @@ def deepseek_provider(deepseek_config):
70
  return DeepSeekProvider(deepseek_config)
71
 
72
 
73
- def test_init(deepseek_config):
74
- """Test provider initialization."""
75
- with patch("providers.openai_compat.AsyncOpenAI") as mock_openai:
76
- provider = DeepSeekProvider(deepseek_config)
77
- assert provider._api_key == "test_deepseek_key"
78
- assert provider._base_url == DEEPSEEK_DEFAULT_BASE
79
- mock_openai.assert_called_once()
80
-
81
-
82
- def test_build_request_body_enables_thinking_for_chat_model(deepseek_provider):
83
- """Thinking-enabled requests add DeepSeek's thinking payload for chat model."""
84
- req = MockRequest(model="deepseek-chat")
85
- body = deepseek_provider._build_request_body(req)
86
-
87
- assert body["model"] == "deepseek-chat"
88
- assert body["extra_body"]["thinking"] == {"type": "enabled"}
89
- assert body["messages"][0]["role"] == "system"
90
-
91
-
92
- def test_build_request_body_global_disable_blocks_request_thinking():
93
- """Global disable suppresses provider-side thinking even if the request enables it."""
94
- provider = DeepSeekProvider(
95
- ProviderConfig(
96
- api_key="test_deepseek_key",
97
- base_url=DEEPSEEK_DEFAULT_BASE,
98
- rate_limit=10,
99
- rate_window=60,
100
- enable_thinking=False,
101
- )
102
- )
103
- req = MockRequest(model="deepseek-chat")
104
- body = provider._build_request_body(req)
105
-
106
- assert "extra_body" not in body or "thinking" not in body["extra_body"]
107
-
108
 
109
- def test_build_request_body_request_disable_blocks_global_thinking(deepseek_provider):
110
- """Request-level disable suppresses provider-side thinking when global is enabled."""
111
- req = MockRequest(model="deepseek-chat")
112
- req.thinking.enabled = False
113
- body = deepseek_provider._build_request_body(req)
114
-
115
- assert "extra_body" not in body or "thinking" not in body["extra_body"]
116
 
 
 
 
 
 
 
117
 
118
- def test_build_request_body_reasoner_skips_thinking_extra(deepseek_provider):
119
- """deepseek-reasoner does not need an extra thinking payload."""
120
- req = MockRequest(model="deepseek-reasoner")
121
- body = deepseek_provider._build_request_body(req)
122
 
123
- assert body["model"] == "deepseek-reasoner"
124
- assert "extra_body" not in body or "thinking" not in body["extra_body"]
 
 
 
125
 
126
 
127
- def test_build_request_body_preserves_caller_thinking_override(deepseek_provider):
128
- """Caller-provided thinking payload should not be overwritten."""
129
- req = MockRequest(
130
- model="deepseek-chat",
131
- extra_body={"thinking": {"type": "manual"}},
 
132
  )
133
- body = deepseek_provider._build_request_body(req)
134
-
135
- assert body["extra_body"]["thinking"] == {"type": "manual"}
136
-
137
-
138
- def test_build_request_body_preserves_reasoning_content(deepseek_provider):
139
- """Thinking blocks are mirrored into reasoning_content for continuation."""
140
- req = MockRequest(
141
- system=None,
142
- messages=[
143
- MockMessage(
144
- "assistant",
145
- [
146
- MockBlock(type="thinking", thinking="First think"),
147
- MockBlock(type="text", text="Then answer"),
148
- ],
149
- )
150
- ],
151
  )
152
-
153
- body = deepseek_provider._build_request_body(req)
154
-
155
- assistant = body["messages"][0]
156
- assert assistant["reasoning_content"] == "First think"
157
- assert "<think>" not in assistant["content"]
158
- assert assistant["content"] == "Then answer"
159
 
160
 
161
- def test_build_request_body_preserves_top_level_reasoning_content(deepseek_provider):
162
- """OpenAI-compatible assistant reasoning_content survives history replay."""
163
- req = MockRequest(
164
- system=None,
165
- messages=[
166
- MockMessage(
167
- "assistant",
168
- "Then answer",
169
- reasoning_content="First think",
170
- )
171
- ],
172
  )
 
 
173
 
174
- body = deepseek_provider._build_request_body(req)
175
 
176
- assistant = body["messages"][0]
177
- assert assistant["reasoning_content"] == "First think"
178
- assert assistant["content"] == "Then answer"
179
-
180
-
181
- def test_build_request_body_replays_reasoning_content_with_tool_calls(
182
- deepseek_provider,
183
- ):
184
- """Thinking plus tool_use history is replayed in DeepSeek's top-level shape."""
185
- req = MockRequest(
186
- system=None,
187
- messages=[
188
- MockMessage(
189
- "assistant",
190
- [
191
- MockBlock(type="thinking", thinking="Need the tool."),
192
- MockBlock(
193
- type="tool_use",
194
- id="call_reasoning",
195
- name="echo",
196
- input={"value": "x"},
197
- ),
198
- ],
199
- )
200
- ],
201
  )
 
 
 
202
 
203
- body = deepseek_provider._build_request_body(req)
204
-
205
- assistant = body["messages"][0]
206
- assert assistant["content"] == ""
207
- assert assistant["reasoning_content"] == "Need the tool."
208
- assert assistant["tool_calls"][0]["id"] == "call_reasoning"
209
- assert "<think>" not in assistant["content"]
210
 
211
-
212
- def test_build_request_body_disabled_thinking_omits_reasoning_and_thinking_tags():
213
- """Resolved thinking policy must strip assistant thinking from OpenAI history."""
214
  provider = DeepSeekProvider(
215
  ProviderConfig(
216
- api_key="test_deepseek_key",
217
- base_url=DEEPSEEK_DEFAULT_BASE,
218
- rate_limit=10,
219
- rate_window=60,
220
  enable_thinking=False,
221
  )
222
  )
223
- req = MockRequest(
224
- system=None,
225
- model="deepseek-chat",
226
- messages=[
227
- MockMessage(
228
- "assistant",
229
- [
230
- MockBlock(type="thinking", thinking="secret"),
231
- MockBlock(type="text", text="hi"),
232
- ],
233
- )
234
- ],
235
  )
236
-
237
- body = provider._build_request_body(req)
238
-
239
- assistant = body["messages"][0]
240
- assert "reasoning_content" not in assistant
241
- assert "secret" not in assistant["content"]
242
- assert assistant["content"] == "hi"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
 
244
 
245
- def test_build_request_body_disabled_thinking_omits_redacted_blocks():
246
- """redacted_thinking is not sent as OpenAI text when thinking is disabled."""
247
  provider = DeepSeekProvider(
248
  ProviderConfig(
249
- api_key="test_deepseek_key",
250
- base_url=DEEPSEEK_DEFAULT_BASE,
251
- rate_limit=10,
252
- rate_window=60,
253
  enable_thinking=False,
254
  )
255
  )
256
- req = MockRequest(
257
- system=None,
258
- model="deepseek-chat",
259
- messages=[
260
- MockMessage(
261
- "assistant",
262
- [
263
- MockBlock(type="redacted_thinking", data="opaque-xyz"),
264
- MockBlock(type="text", text="hi"),
265
- ],
266
- )
267
- ],
 
268
  )
269
- body = provider._build_request_body(req)
270
- assert "opaque-xyz" not in body["messages"][0]["content"]
271
-
272
-
273
- @pytest.mark.asyncio
274
- async def test_stream_response_reasoning_content(deepseek_provider):
275
- """reasoning_content deltas are emitted as thinking blocks."""
276
- req = MockRequest()
277
-
278
- mock_chunk = MagicMock()
279
- mock_chunk.choices = [
280
- MagicMock(
281
- delta=MagicMock(content=None, reasoning_content="Thinking..."),
282
- finish_reason="stop",
283
- )
284
- ]
285
- mock_chunk.usage = MagicMock(completion_tokens=2)
286
-
287
- async def mock_stream():
288
- yield mock_chunk
289
-
290
- with patch.object(
291
- deepseek_provider._client.chat.completions, "create", new_callable=AsyncMock
292
- ) as mock_create:
293
- mock_create.return_value = mock_stream()
294
-
295
- events = [event async for event in deepseek_provider.stream_response(req)]
296
-
297
- assert any(
298
- '"thinking_delta"' in event and "Thinking..." in event for event in events
299
- )
 
 
 
 
 
 
 
300
 
301
 
302
- def test_preflight_stream_rejects_unsupported_user_image_for_openai_conversion():
303
- """Eager preflight: image block fails before a stream would be opened."""
304
  request = MessagesRequest(
305
- model="deepseek/deepseek-chat",
306
- max_tokens=100,
307
  messages=[
308
  Message(
309
  role="user",
@@ -323,11 +269,139 @@ def test_preflight_stream_rejects_unsupported_user_image_for_openai_conversion()
323
  provider = DeepSeekProvider(
324
  ProviderConfig(
325
  api_key="k",
326
- base_url=DEEPSEEK_DEFAULT_BASE,
327
- rate_limit=10,
328
- rate_window=60,
329
  )
330
  )
331
- with pytest.raises(InvalidRequestError) as exc:
332
  provider.preflight_stream(request, thinking_enabled=True)
333
- assert "image" in str(exc.value).lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for DeepSeek native Anthropic Messages provider."""
2
 
3
+ from contextlib import asynccontextmanager
4
  from unittest.mock import AsyncMock, MagicMock, patch
5
 
6
  import pytest
7
 
8
+ from api.models.anthropic import (
9
+ ContentBlockImage,
10
+ Message,
11
+ MessagesRequest,
12
+ Tool,
13
+ )
14
+ from config.constants import ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS
15
  from providers.base import ProviderConfig
16
+ from providers.deepseek import (
17
+ DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
18
+ DEEPSEEK_DEFAULT_BASE,
19
+ DeepSeekProvider,
20
+ )
21
  from providers.exceptions import InvalidRequestError
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  @pytest.fixture
25
  def deepseek_config():
26
  return ProviderConfig(
27
  api_key="test_deepseek_key",
28
+ base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
29
  rate_limit=10,
30
  rate_window=60,
31
  enable_thinking=True,
 
34
 
35
  @pytest.fixture(autouse=True)
36
  def mock_rate_limiter():
37
+ @asynccontextmanager
38
+ async def _slot():
39
+ yield
40
+
41
+ with patch("providers.anthropic_messages.GlobalRateLimiter") as mock:
42
  instance = mock.get_scoped_instance.return_value
 
43
 
44
  async def _passthrough(fn, *args, **kwargs):
45
  return await fn(*args, **kwargs)
46
 
47
  instance.execute_with_retry = AsyncMock(side_effect=_passthrough)
48
+ instance.concurrency_slot.side_effect = _slot
49
  yield instance
50
 
51
 
 
54
  return DeepSeekProvider(deepseek_config)
55
 
56
 
57
+ def test_default_base_url_alias():
58
+ assert DEEPSEEK_DEFAULT_BASE == DEEPSEEK_ANTHROPIC_DEFAULT_BASE
59
+ assert DEEPSEEK_ANTHROPIC_DEFAULT_BASE == "https://api.deepseek.com/anthropic"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
 
 
 
 
 
 
 
61
 
62
+ def test_init(deepseek_config):
63
+ with patch("httpx.AsyncClient") as mock_client:
64
+ provider = DeepSeekProvider(deepseek_config)
65
+ assert provider._api_key == "test_deepseek_key"
66
+ assert provider._base_url == "https://api.deepseek.com/anthropic"
67
+ assert mock_client.called
68
 
 
 
 
 
69
 
70
+ def test_request_headers_includes_x_api_key(deepseek_provider):
71
+ h = deepseek_provider._request_headers()
72
+ assert h["x-api-key"] == "test_deepseek_key"
73
+ assert h["Content-Type"] == "application/json"
74
+ assert h["Accept"] == "text/event-stream"
75
 
76
 
77
+ def test_build_request_body_native_shape(deepseek_provider):
78
+ request = MessagesRequest(
79
+ model="deepseek-v4-pro",
80
+ max_tokens=100,
81
+ messages=[Message(role="user", content="Hello")],
82
+ system="S",
83
  )
84
+ body = deepseek_provider._build_request_body(request)
85
+ assert body["model"] == "deepseek-v4-pro"
86
+ assert body["stream"] is True
87
+ assert body["messages"][0]["role"] == "user"
88
+ assert body["messages"][0]["content"] in (
89
+ "Hello",
90
+ [{"type": "text", "text": "Hello"}],
 
 
 
 
 
 
 
 
 
 
 
91
  )
92
+ assert body["system"] == "S"
93
+ assert body["max_tokens"] == 100
 
 
 
 
 
94
 
95
 
96
+ def test_build_request_body_default_max_tokens(deepseek_provider):
97
+ request = MessagesRequest(
98
+ model="m",
99
+ messages=[Message(role="user", content="x")],
 
 
 
 
 
 
 
100
  )
101
+ body = deepseek_provider._build_request_body(request)
102
+ assert body["max_tokens"] == ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS
103
 
 
104
 
105
+ def test_build_request_body_thinking_enabled(deepseek_provider):
106
+ request = MessagesRequest.model_validate(
107
+ {
108
+ "model": "m",
109
+ "messages": [{"role": "user", "content": "x"}],
110
+ "thinking": {"type": "enabled", "budget_tokens": 2000},
111
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  )
113
+ body = deepseek_provider._build_request_body(request)
114
+ assert body["thinking"] == {"type": "enabled", "budget_tokens": 2000}
115
+ assert "extra_body" not in body
116
 
 
 
 
 
 
 
 
117
 
118
+ def test_build_request_body_respects_global_thinking_disable():
 
 
119
  provider = DeepSeekProvider(
120
  ProviderConfig(
121
+ api_key="k",
122
+ base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
123
+ rate_limit=1,
124
+ rate_window=1,
125
  enable_thinking=False,
126
  )
127
  )
128
+ request = MessagesRequest.model_validate(
129
+ {
130
+ "model": "m",
131
+ "messages": [{"role": "user", "content": "x"}],
132
+ "thinking": {"type": "enabled", "budget_tokens": 1},
133
+ }
 
 
 
 
 
 
134
  )
135
+ body = provider._build_request_body(request)
136
+ assert "thinking" not in body
137
+
138
+
139
+ def test_preserve_unsigned_thinking_when_thinking_on(deepseek_provider):
140
+ request = MessagesRequest.model_validate(
141
+ {
142
+ "model": "m",
143
+ "messages": [
144
+ {
145
+ "role": "assistant",
146
+ "content": [
147
+ {
148
+ "type": "thinking",
149
+ "thinking": "plain",
150
+ "signature": None,
151
+ },
152
+ {"type": "text", "text": "out"},
153
+ ],
154
+ }
155
+ ],
156
+ }
157
+ )
158
+ body = deepseek_provider._build_request_body(request)
159
+ blocks = body["messages"][0]["content"]
160
+ assert len(blocks) == 2
161
+ assert blocks[0]["type"] == "thinking"
162
+ assert blocks[0]["thinking"] == "plain"
163
+
164
+
165
+ def test_strip_redacted_thinking_when_thinking_on(deepseek_provider):
166
+ request = MessagesRequest.model_validate(
167
+ {
168
+ "model": "m",
169
+ "messages": [
170
+ {
171
+ "role": "assistant",
172
+ "content": [
173
+ {"type": "redacted_thinking", "data": "opaque"},
174
+ {"type": "text", "text": "out"},
175
+ ],
176
+ }
177
+ ],
178
+ }
179
+ )
180
+ body = deepseek_provider._build_request_body(request)
181
+ types = {b["type"] for b in body["messages"][0]["content"]}
182
+ assert "redacted_thinking" not in types
183
+ assert "text" in types
184
 
185
 
186
+ def test_thinking_off_strips_thinking_history():
 
187
  provider = DeepSeekProvider(
188
  ProviderConfig(
189
+ api_key="k",
190
+ base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
191
+ rate_limit=1,
192
+ rate_window=1,
193
  enable_thinking=False,
194
  )
195
  )
196
+ request = MessagesRequest.model_validate(
197
+ {
198
+ "model": "m",
199
+ "messages": [
200
+ {
201
+ "role": "assistant",
202
+ "content": [
203
+ {"type": "thinking", "thinking": "sec"},
204
+ {"type": "text", "text": "hi"},
205
+ ],
206
+ }
207
+ ],
208
+ }
209
  )
210
+ body = provider._build_request_body(request)
211
+ for b in body["messages"][0]["content"]:
212
+ assert b["type"] != "thinking"
213
+ assert "sec" not in str(body["messages"])
214
+
215
+
216
+ def test_passthrough_tool_use_and_result(deepseek_provider):
217
+ request = MessagesRequest.model_validate(
218
+ {
219
+ "model": "m",
220
+ "messages": [
221
+ {
222
+ "role": "assistant",
223
+ "content": [
224
+ {
225
+ "type": "tool_use",
226
+ "id": "t1",
227
+ "name": "n",
228
+ "input": {"a": 1},
229
+ }
230
+ ],
231
+ },
232
+ {
233
+ "role": "user",
234
+ "content": [
235
+ {
236
+ "type": "tool_result",
237
+ "tool_use_id": "t1",
238
+ "content": "ok",
239
+ }
240
+ ],
241
+ },
242
+ ],
243
+ }
244
+ )
245
+ body = deepseek_provider._build_request_body(request)
246
+ assert body["messages"][0]["content"][0]["type"] == "tool_use"
247
+ assert body["messages"][1]["content"][0]["type"] == "tool_result"
248
 
249
 
250
+ def test_preflight_rejects_user_image():
 
251
  request = MessagesRequest(
252
+ model="m",
 
253
  messages=[
254
  Message(
255
  role="user",
 
269
  provider = DeepSeekProvider(
270
  ProviderConfig(
271
  api_key="k",
272
+ base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
273
+ rate_limit=1,
274
+ rate_window=1,
275
  )
276
  )
277
+ with pytest.raises(InvalidRequestError, match="image"):
278
  provider.preflight_stream(request, thinking_enabled=True)
279
+
280
+
281
+ def test_preflight_rejects_mcp_servers():
282
+ request = MessagesRequest(
283
+ model="m",
284
+ messages=[Message(role="user", content="x")],
285
+ mcp_servers=[{"type": "url", "url": "https://x"}],
286
+ )
287
+ provider = DeepSeekProvider(
288
+ ProviderConfig(
289
+ api_key="k",
290
+ base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
291
+ rate_limit=1,
292
+ rate_window=1,
293
+ )
294
+ )
295
+ with pytest.raises(InvalidRequestError, match="mcp_servers"):
296
+ provider.preflight_stream(request)
297
+
298
+
299
+ def test_preflight_rejects_listed_server_tools_in_tools_list():
300
+ request = MessagesRequest(
301
+ model="m",
302
+ messages=[Message(role="user", content="x")],
303
+ tools=[Tool(name="web_search", type="web_search_20250305", input_schema={})],
304
+ )
305
+ provider = DeepSeekProvider(
306
+ ProviderConfig(
307
+ api_key="k",
308
+ base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
309
+ rate_limit=1,
310
+ rate_window=1,
311
+ )
312
+ )
313
+ with pytest.raises(InvalidRequestError, match="web_search"):
314
+ provider.preflight_stream(request)
315
+
316
+
317
+ def test_preflight_rejects_server_tool_result_blocks():
318
+ request = MessagesRequest.model_validate(
319
+ {
320
+ "model": "m",
321
+ "messages": [
322
+ {
323
+ "role": "assistant",
324
+ "content": [
325
+ {
326
+ "type": "server_tool_use",
327
+ "id": "s1",
328
+ "name": "web_search",
329
+ "input": {"q": "a"},
330
+ },
331
+ {
332
+ "type": "web_search_tool_result",
333
+ "tool_use_id": "s1",
334
+ "content": [],
335
+ },
336
+ ],
337
+ }
338
+ ],
339
+ }
340
+ )
341
+ provider = DeepSeekProvider(
342
+ ProviderConfig(
343
+ api_key="k",
344
+ base_url=DEEPSEEK_ANTHROPIC_DEFAULT_BASE,
345
+ rate_limit=1,
346
+ rate_window=1,
347
+ )
348
+ )
349
+ with pytest.raises(InvalidRequestError, match=r"web_search_tool_result|server"):
350
+ provider.preflight_stream(request)
351
+
352
+
353
+ def test_strip_reasoning_content_not_forwarded(deepseek_provider):
354
+ # ``reasoning_content`` is for OpenAI helpers only, not in native body.
355
+ request = MessagesRequest(
356
+ model="m",
357
+ messages=[
358
+ Message(
359
+ role="assistant",
360
+ content="hi",
361
+ reasoning_content="r",
362
+ )
363
+ ],
364
+ )
365
+ body = deepseek_provider._build_request_body(request)
366
+ assert "reasoning_content" not in body["messages"][0]
367
+
368
+
369
+ @pytest.mark.asyncio
370
+ async def test_stream_uses_post_messages_path(deepseek_provider):
371
+ request = MessagesRequest(
372
+ model="m",
373
+ messages=[Message(role="user", content="hi")],
374
+ )
375
+ called: dict[str, str] = {}
376
+
377
+ async def fake_send(request, *args, **kwargs):
378
+ called["path"] = request.url.path
379
+ mock_resp = MagicMock()
380
+ mock_resp.status_code = 200
381
+ mock_resp.is_closed = False
382
+ mock_resp.raise_for_status = lambda: None
383
+
384
+ async def aiter():
385
+ if False: # pragma: no cover
386
+ yield ""
387
+
388
+ mock_resp.aiter_lines = aiter
389
+ mock_resp.aclose = AsyncMock()
390
+ return mock_resp
391
+
392
+ deepseek_provider._client.send = fake_send
393
+ _ = [x async for x in deepseek_provider.stream_response(request, request_id="r1")]
394
+
395
+ assert called["path"] == "/anthropic/messages"
396
+
397
+
398
+ def test_drops_extra_body_from_canonical_request(deepseek_provider):
399
+ raw = {
400
+ "model": "m",
401
+ "max_tokens": 3,
402
+ "messages": [{"role": "user", "content": "x"}],
403
+ "extra_body": {"note": 1},
404
+ }
405
+ r = MessagesRequest.model_validate(raw)
406
+ body = deepseek_provider._build_request_body(r)
407
+ assert "extra_body" not in body