fix(anthropic): keep assistant block order and reasoning_text on the /v1/messages Responses bridge

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Moe Khalil 2026-10-27 18:25:22 +00:00
parent a38d6aa5d5
commit 820dd8f4de
4 changed files with 105 additions and 23 deletions

View file

@ -29,6 +29,7 @@ from litellm.types.llms.openai import (
ChatCompletionImageObject,
ChatCompletionReasoningItem,
ChatCompletionReasoningSummaryTextBlock,
ChatCompletionReasoningTextBlock,
ChatCompletionRedactedThinkingBlock,
ChatCompletionResponseMessage,
ChatCompletionTextObject,
@ -2257,6 +2258,8 @@ def reasoning_content_from_thinking_blocks(
ENCRYPTED_REASONING_SIGNATURE_PREFIX: Final = "litellm_encrypted_reasoning:"
REASONING_TEXT_SIGNATURE_PREFIX: Final = "litellm_reasoning_text:"
_RESPONSES_REASONING_SIGNATURE_PREFIXES: Final = (REASONING_TEXT_SIGNATURE_PREFIX, ENCRYPTED_REASONING_SIGNATURE_PREFIX)
def encrypted_reasoning_signature(encrypted_content: str) -> str:
@ -2269,14 +2272,30 @@ def encrypted_reasoning_signature(encrypted_content: str) -> str:
return f"{ENCRYPTED_REASONING_SIGNATURE_PREFIX}{encrypted_content}"
def reasoning_text_signature(encrypted_content: str | None) -> str:
"""The signature of a thinking block holding a reasoning item's raw `reasoning_text`.
Backends that emit `reasoning_text` require it back verbatim, so the prefix tells the
replay to rebuild the item's `content` instead of a `summary`.
"""
return f"{REASONING_TEXT_SIGNATURE_PREFIX}{encrypted_content or ''}"
def _carries_encrypted_reasoning(signature: object) -> bool:
return isinstance(signature, str) and signature.startswith(ENCRYPTED_REASONING_SIGNATURE_PREFIX)
return isinstance(signature, str) and signature.startswith(_RESPONSES_REASONING_SIGNATURE_PREFIXES)
def encrypted_content_from_signature(signature: object) -> str | None:
if not isinstance(signature, str) or not _carries_encrypted_reasoning(signature):
if not isinstance(signature, str):
return None
return signature.removeprefix(ENCRYPTED_REASONING_SIGNATURE_PREFIX) or None
return next(
(
signature.removeprefix(prefix) or None
for prefix in _RESPONSES_REASONING_SIGNATURE_PREFIXES
if signature.startswith(prefix)
),
None,
)
def _encrypted_reasoning_field(block: Mapping[str, object]) -> object:
@ -2293,6 +2312,11 @@ def encrypted_content_of_block(block: Mapping[str, object]) -> str | None:
return encrypted_content_from_signature(_encrypted_reasoning_field(block))
def _carries_reasoning_text(block: Mapping[str, object]) -> bool:
field: Final = _encrypted_reasoning_field(block)
return isinstance(field, str) and field.startswith(REASONING_TEXT_SIGNATURE_PREFIX)
def is_encrypted_reasoning_block(block: object) -> bool:
"""A thinking or redacted_thinking block carrying Responses API encrypted reasoning.
@ -2414,6 +2438,15 @@ def _reasoning_replay_group_key(indexed_block: tuple[int, Mapping[str, object]])
return f"encrypted:{index}" if is_encrypted_reasoning_block(block) else "summary"
def _reasoning_text_item(text: str, encrypted_content: str | None) -> ChatCompletionReasoningItem:
content: Final = [ChatCompletionReasoningTextBlock(type="reasoning_text", text=text)] if text else []
if encrypted_content is None:
return ChatCompletionReasoningItem(type="reasoning", summary=[], content=content)
return ChatCompletionReasoningItem(
type="reasoning", summary=[], content=content, encrypted_content=encrypted_content
)
def _reasoning_item_from_block_group(group: tuple[Mapping[str, object], ...]) -> ChatCompletionReasoningItem | None:
summary: Final[list[ChatCompletionReasoningSummaryTextBlock]] = [ # mutable-ok: API message payload
ChatCompletionReasoningSummaryTextBlock(type="summary_text", text=text)
@ -2421,6 +2454,8 @@ def _reasoning_item_from_block_group(group: tuple[Mapping[str, object], ...]) ->
if (text := _readable_thinking_text(block))
]
encrypted_content: Final = encrypted_content_of_block(group[0])
if _carries_reasoning_text(group[0]):
return _reasoning_text_item(_readable_thinking_text(group[0]), encrypted_content)
if encrypted_content is not None:
return ChatCompletionReasoningItem(type="reasoning", summary=summary, encrypted_content=encrypted_content)
if not summary:

View file

@ -3,7 +3,7 @@
import asyncio
from collections import deque
from collections.abc import AsyncGenerator, Iterator, Mapping
from typing import TYPE_CHECKING, Any, Final
from typing import TYPE_CHECKING, Any, Final, cast
from pydantic import ConfigDict, field_validator
@ -13,6 +13,7 @@ from litellm._uuid import uuid
from litellm.exceptions import MidStreamFallbackError
from litellm.litellm_core_utils.prompt_templates.common_utils import (
encrypted_reasoning_signature,
reasoning_text_signature,
)
from litellm.llms.anthropic.messages.native.utils import (
INCOMPLETE_STREAM_ERROR_MESSAGE,
@ -115,6 +116,7 @@ class AnthropicResponsesStreamWrapper:
response.output_text.delta -> content_block_delta (text_delta)
response.reasoning_summary_part.added -> content_block_delta (thinking_delta separator)
response.reasoning_summary_text.delta -> content_block_delta (thinking_delta)
response.reasoning_text.delta -> content_block_delta (thinking_delta)
response.function_call_arguments.delta -> content_block_delta (input_json_delta)
response.output_item.done -> content_block_delta (signature_delta) + content_block_stop
response.completed -> message_delta + message_stop
@ -187,12 +189,25 @@ class AnthropicResponsesStreamWrapper:
def _field(source: object, name: str) -> object:
return source.get(name) if isinstance(source, dict) else getattr(source, name, None)
@classmethod
def _has_reasoning_text(cls, item: object) -> bool:
content: Final = cls._field(item, "content")
if not isinstance(content, list):
return False
return any(
cls._field(part, "type") == "reasoning_text" and cls._field(part, "text")
for part in cast(list[object], content) # cast-ok: narrowed by isinstance
)
def _close_reasoning_item(self, item: object, item_id: str | None) -> None:
block_idx: Final = self._item_id_to_block_index.get(item_id, -1) if item_id else self._current_block_index
encrypted_content: Final = self._field(item, "encrypted_content")
encrypted: Final = encrypted_content if isinstance(encrypted_content, str) and encrypted_content else None
signature: Final = (
encrypted_reasoning_signature(encrypted_content)
if isinstance(encrypted_content, str) and encrypted_content
reasoning_text_signature(encrypted)
if block_idx >= 0 and self._has_reasoning_text(item)
else encrypted_reasoning_signature(encrypted)
if encrypted is not None
else None
)
if block_idx < 0 and signature is None:
@ -317,7 +332,7 @@ class AnthropicResponsesStreamWrapper:
return
# ---- reasoning summary text delta ----
if event_type == "response.reasoning_summary_text.delta":
if event_type in ("response.reasoning_summary_text.delta", "response.reasoning_text.delta"):
item_id = getattr(event, "item_id", None) or (event.get("item_id") if isinstance(event, dict) else None)
delta = getattr(event, "delta", "") or (event.get("delta", "") if isinstance(event, dict) else "")
block_idx = self._item_id_to_block_index.get(item_id, -1) if item_id else self._current_block_index

View file

@ -15,6 +15,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (
TOOL_RESULT_IMAGE_PLACEHOLDER,
encrypted_reasoning_signature,
responses_reasoning_items_from_thinking_blocks,
reasoning_text_signature,
with_prompt_cache_breakpoint,
)
from litellm.litellm_core_utils.reasoning_effort_utils import (
@ -246,11 +247,21 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
return str(mapping.get("text") or "")
return str(getattr(part, "text", None) or "")
@classmethod
def _reasoning_text_part_text(cls, part: object) -> str:
part_type: Final = (
cast(Mapping[str, object], part).get("type") # cast-ok: content parts are untyped provider json
if isinstance(part, Mapping)
else getattr(part, "type", None)
)
return cls._summary_part_text(part) if part_type == "reasoning_text" else ""
@classmethod
def _thinking_block_from_reasoning_item(
cls,
summary: Iterable[object],
encrypted_content: object,
content: Iterable[object] = (),
) -> dict[str, object] | None: # mutable-ok: API message payload
"""The one Anthropic block for a Responses reasoning item.
@ -259,6 +270,17 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
turn replays the very item OpenAI produced; without it the signature stays empty,
since only Anthropic can sign a thinking block.
"""
reasoning_text: Final = REASONING_SUMMARY_PART_SEPARATOR.join(
part_text for part in content if (part_text := cls._reasoning_text_part_text(part))
)
if reasoning_text:
return AnthropicResponseContentBlockThinking(
type="thinking",
thinking=reasoning_text,
signature=reasoning_text_signature(
encrypted_content if isinstance(encrypted_content, str) and encrypted_content else None
),
).model_dump()
text: Final = REASONING_SUMMARY_PART_SEPARATOR.join(
part_text for part in summary if (part_text := cls._summary_part_text(part))
)
@ -273,9 +295,15 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
@staticmethod
def _assistant_block_group_key(indexed_block: tuple[int, Mapping[str, object]]) -> str:
"""Group a run of consecutive thinking blocks together; keep every other block alone."""
"""Group runs of consecutive thinking or text blocks together; keep every other block alone."""
index, block = indexed_block
return "thinking" if block.get("type") in ("thinking", "redacted_thinking") else f"block:{index}"
match block.get("type"):
case "thinking" | "redacted_thinking":
return "thinking"
case "text":
return "text"
case _:
return f"block:{index}"
@classmethod
def _assistant_group_to_input_items(
@ -286,6 +314,14 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
if btype in ("thinking", "redacted_thinking"):
replayed: Final = responses_reasoning_items_from_thinking_blocks(group)
return tuple(dict(item) for item in replayed)
if btype == "text":
return (
{
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": block.get("text", "")} for block in group],
},
)
if btype == "tool_use":
return (
{
@ -448,19 +484,6 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
for _, group in groupby(enumerate(blocks), key=self._assistant_block_group_key)
for item in self._assistant_group_to_input_items(tuple(block for _, block in group))
)
asst_parts: list[dict[str, object]] = [ # mutable-ok: API message payload
{"type": "output_text", "text": block.get("text", "")}
for block in blocks
if block.get("type") == "text"
]
if asst_parts:
input_items.append(
{
"type": "message",
"role": "assistant",
"content": asst_parts,
}
)
return input_items
@ -741,7 +764,9 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
for item in response.output:
if isinstance(item, ResponseReasoningItem):
reasoning_block = self._thinking_block_from_reasoning_item(item.summary, item.encrypted_content)
reasoning_block = self._thinking_block_from_reasoning_item(
item.summary, item.encrypted_content, getattr(item, "content", None) or ()
)
if reasoning_block is not None:
content.append(reasoning_block)
@ -796,6 +821,7 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
reasoning_block = self._thinking_block_from_reasoning_item(
cast(Iterable[object], item.get("summary") or ()), # cast-ok: untyped provider json
item.get("encrypted_content"),
cast(Iterable[object], item.get("content") or ()), # cast-ok: untyped provider json
)
if reasoning_block is not None:
content.append(reasoning_block)

View file

@ -711,6 +711,11 @@ class ChatCompletionReasoningSummaryTextBlock(TypedDict, total=False):
text: str
class ChatCompletionReasoningTextBlock(TypedDict):
type: ReadOnly[Literal["reasoning_text"]]
text: ReadOnly[str]
class ChatCompletionReasoningItem(TypedDict, total=False):
"""Represents an OpenAI Responses API reasoning item for round-tripping in conversation history."""
@ -718,6 +723,7 @@ class ChatCompletionReasoningItem(TypedDict, total=False):
id: str
encrypted_content: str | None
summary: ReadOnly[list[ChatCompletionReasoningSummaryTextBlock]]
content: ReadOnly[list[ChatCompletionReasoningTextBlock]]
class WebSearchOptionsUserLocationApproximate(TypedDict, total=False):