fix(deepseek): fill reasoning_content on anthropic-compatible endpoint

DeepSeek's Anthropic-compatible /v1/messages endpoint rejects multi-turn
thinking conversations whose assistant history is missing
`reasoning_content`:

  The `reasoning_content` in the thinking mode must be passed back to the API.

The OpenAI-compat path already backfills the field
(`DeepSeekChatConfig._fill_reasoning_content`), but the Anthropic-compat
config did not, so the two endpoints behaved differently for the same model.

This adds `_fill_reasoning_content` and `_thinking_mode_active` to
`DeepSeekAnthropicMessagesConfig`, mirroring the chat path:

- promote `reasoning_content` from `provider_specific_fields` when present
- otherwise inject a single-space placeholder and log a warning
- guard on `thinking.type == "enabled"` only, so non-thinking requests are
  untouched

The guard intentionally drops the `supports_reasoning` capability check the
chat path uses: the Anthropic-compat endpoint accepts the native Anthropic
message shape, where thinking is always opt-in, so any request that turns
thinking on must be backfilled.
This commit is contained in:
GavinZ 2026-10-26 12:43:59 +08:00
parent dddbaa803d
commit 2a1af50daf

View file

@ -8,6 +8,7 @@ from typing import Final
from pydantic import TypeAdapter
import litellm
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.url_utils import join_api_base
from litellm.llms.anthropic.pass_through.messages.transformation import (
AnthropicMessagesConfig,
@ -122,6 +123,65 @@ class DeepSeekAnthropicMessagesConfig(AnthropicMessagesConfig):
return [_sanitized_tool(tool) for tool in _TOOL_LIST.validate_python(tools)]
def _fill_reasoning_content(self, messages: list[dict]) -> list[dict]: # mutable-ok: anthropic wire payload
"""
DeepSeek thinking mode requires `reasoning_content` to be passed back on
every assistant message in multi-turn conversations. If it is missing,
the anthropic-compatible API returns:
"The reasoning_content in the thinking mode must be passed back to the API."
This mirrors `DeepSeekChatConfig._fill_reasoning_content`
(litellm/llms/deepseek/chat/transformation.py) for the anthropic-compat
endpoint — both endpoints enforce the same requirement, but only the
OpenAI-compat path filled the field.
For each assistant message that is missing `reasoning_content`:
1. Promote it from `provider_specific_fields["reasoning_content"]` if
present (LiteLLM stores provider-specific response fields there).
2. Otherwise inject a single space — the minimum value the API accepts.
"""
result: Final[list[dict]] = [] # mutable-ok: append-built mirror of chat path
for msg in messages:
if isinstance(msg, dict) and msg.get("role") == "assistant" and not msg.get("reasoning_content"):
patched = dict(msg)
provider_fields = patched.get("provider_specific_fields") or {}
stored = provider_fields.get("reasoning_content")
if stored:
patched["reasoning_content"] = stored
cleaned = dict(provider_fields)
cleaned.pop("reasoning_content", None)
patched["provider_specific_fields"] = cleaned
else:
verbose_logger.warning(
"DeepSeek thinking mode: assistant message is missing "
"`reasoning_content` and none was saved in "
"`provider_specific_fields`. A single-space placeholder "
"is being injected to satisfy API validation, but the "
"model will receive a blank reasoning chain for this turn, "
"which may silently degrade multi-turn response quality. "
"Preserve `reasoning_content` from the original assistant "
"response when building multi-turn conversation history."
)
patched["reasoning_content"] = " "
result.append(patched)
else:
result.append(msg)
return result
def _thinking_mode_active(self, model: str, optional_params: dict) -> bool: # mutable-ok: read-only caller params
"""
Returns True only when the request explicitly carries
`thinking: {"type": "enabled"}` — the same opt-in signal the
upstream DeepSeek anthropic-compat endpoint enforces.
Unlike the chat-side `DeepSeekChatConfig._thinking_mode_active`,
we do NOT gate on `supports_reasoning(model, custom_llm_provider)`.
The anthropic-compat endpoint accepts the native anthropic message
shape (where `thinking` is always opt-in), so any request that
actually turns thinking on is one we must backfill for.
"""
return (optional_params.get("thinking") or {}).get("type") == "enabled"
def transform_anthropic_messages_request(
self,
model: str,
@ -139,4 +199,11 @@ class DeepSeekAnthropicMessagesConfig(AnthropicMessagesConfig):
)
if "tools" in anthropic_messages_request:
anthropic_messages_request["tools"] = self._sanitize_tools_for_deepseek(anthropic_messages_request["tools"])
if (
self._thinking_mode_active(model=model, optional_params=anthropic_messages_optional_request_params)
and "messages" in anthropic_messages_request
):
anthropic_messages_request["messages"] = self._fill_reasoning_content(
anthropic_messages_request["messages"]
)
return anthropic_messages_request