mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(anthropic): satisfy lint gates on the Responses reasoning_text bridge
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
820dd8f4de
commit
ef098b09e6
6 changed files with 79 additions and 29 deletions
|
|
@ -3,7 +3,7 @@
|
|||
import asyncio
|
||||
from collections import deque
|
||||
from collections.abc import AsyncGenerator, Iterator, Mapping
|
||||
from typing import TYPE_CHECKING, Any, Final, cast
|
||||
from typing import TYPE_CHECKING, Any, Final
|
||||
|
||||
from pydantic import ConfigDict, field_validator
|
||||
|
||||
|
|
@ -139,6 +139,7 @@ class AnthropicResponsesStreamWrapper:
|
|||
self._current_block_index: int = -1
|
||||
# Map item_id -> content_block_index so we can stop the right block later
|
||||
self._item_id_to_block_index: dict[str, int] = {}
|
||||
self._reasoning_text_block_indexes: frozenset[int] = frozenset()
|
||||
# Track open function_call items by item_id so we can emit tool_use start
|
||||
self._pending_tool_ids: dict[str, str] = {} # item_id -> call_id / name accumulator
|
||||
self._sent_message_start = False
|
||||
|
|
@ -189,23 +190,13 @@ 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 = (
|
||||
reasoning_text_signature(encrypted)
|
||||
if block_idx >= 0 and self._has_reasoning_text(item)
|
||||
if block_idx in self._reasoning_text_block_indexes
|
||||
else encrypted_reasoning_signature(encrypted)
|
||||
if encrypted is not None
|
||||
else None
|
||||
|
|
@ -343,6 +334,8 @@ class AnthropicResponsesStreamWrapper:
|
|||
item_id,
|
||||
{"type": "thinking", "thinking": "", "signature": ""},
|
||||
)
|
||||
if event_type == "response.reasoning_text.delta":
|
||||
self._reasoning_text_block_indexes = self._reasoning_text_block_indexes | {block_idx}
|
||||
self._chunk_queue.append(
|
||||
{
|
||||
"type": "content_block_delta",
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (
|
|||
TOOL_RESULT_IMAGE_BOUNDARY,
|
||||
TOOL_RESULT_IMAGE_PLACEHOLDER,
|
||||
encrypted_reasoning_signature,
|
||||
responses_reasoning_items_from_thinking_blocks,
|
||||
reasoning_text_signature,
|
||||
responses_reasoning_items_from_thinking_blocks,
|
||||
with_prompt_cache_breakpoint,
|
||||
)
|
||||
from litellm.litellm_core_utils.reasoning_effort_utils import (
|
||||
|
|
@ -247,14 +247,12 @@ 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 ""
|
||||
@staticmethod
|
||||
def _reasoning_text_part_text(part: object) -> str:
|
||||
if isinstance(part, Mapping):
|
||||
mapping: Final = cast(Mapping[str, object], part) # cast-ok: content parts are untyped provider json
|
||||
return str(mapping.get("text") or "") if mapping.get("type") == "reasoning_text" else ""
|
||||
return str(getattr(part, "text", None) or "") if getattr(part, "type", None) == "reasoning_text" else ""
|
||||
|
||||
@classmethod
|
||||
def _thinking_block_from_reasoning_item(
|
||||
|
|
@ -271,7 +269,7 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
|
|||
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))
|
||||
reasoning_part_text for part in content if (reasoning_part_text := cls._reasoning_text_part_text(part))
|
||||
)
|
||||
if reasoning_text:
|
||||
return AnthropicResponseContentBlockThinking(
|
||||
|
|
|
|||
|
|
@ -48035,6 +48035,13 @@
|
|||
"ChatCompletionReasoningItem": {
|
||||
"description": "Represents an OpenAI Responses API reasoning item for round-tripping in conversation history.",
|
||||
"properties": {
|
||||
"content": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/ChatCompletionReasoningTextBlock"
|
||||
},
|
||||
"title": "Content",
|
||||
"type": "array"
|
||||
},
|
||||
"encrypted_content": {
|
||||
"anyOf": [
|
||||
{
|
||||
|
|
@ -48087,6 +48094,25 @@
|
|||
"title": "ChatCompletionReasoningSummaryTextBlock",
|
||||
"type": "object"
|
||||
},
|
||||
"ChatCompletionReasoningTextBlock": {
|
||||
"properties": {
|
||||
"text": {
|
||||
"title": "Text",
|
||||
"type": "string"
|
||||
},
|
||||
"type": {
|
||||
"const": "reasoning_text",
|
||||
"title": "Type",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"type",
|
||||
"text"
|
||||
],
|
||||
"title": "ChatCompletionReasoningTextBlock",
|
||||
"type": "object"
|
||||
},
|
||||
"ChatCompletionRedactedThinkingBlock": {
|
||||
"properties": {
|
||||
"cache_control": {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import builtins
|
||||
from collections.abc import Iterable, Mapping
|
||||
from collections.abc import Iterable, Mapping, Sequence
|
||||
from enum import Enum
|
||||
from os import PathLike
|
||||
from typing import IO, TYPE_CHECKING, Any, Final, Generic, Literal, Optional, TypeAlias, Union
|
||||
|
|
@ -723,7 +723,7 @@ class ChatCompletionReasoningItem(TypedDict, total=False):
|
|||
id: str
|
||||
encrypted_content: str | None
|
||||
summary: ReadOnly[list[ChatCompletionReasoningSummaryTextBlock]]
|
||||
content: ReadOnly[list[ChatCompletionReasoningTextBlock]]
|
||||
content: ReadOnly[Sequence[ChatCompletionReasoningTextBlock]]
|
||||
|
||||
|
||||
class WebSearchOptionsUserLocationApproximate(TypedDict, total=False):
|
||||
|
|
|
|||
|
|
@ -174,7 +174,11 @@ def test_messages_replays_reasoning_text_with_its_encrypted_content(gateway: Gat
|
|||
"/v1/messages",
|
||||
_messages_request(
|
||||
model,
|
||||
[user_turn, {"role": "assistant", "content": assistant_content}, _tool_results(identity, {"a": "alpha"})],
|
||||
[
|
||||
user_turn,
|
||||
{"role": "assistant", "content": assistant_content},
|
||||
_tool_results(identity, {"a": "alpha"}),
|
||||
],
|
||||
),
|
||||
)
|
||||
assert second.status_code == 200, second.text
|
||||
|
|
@ -196,7 +200,7 @@ def test_messages_stream_emits_reasoning_text_deltas_as_thinking(gateway: Gatewa
|
|||
pieces: Final = ("Let me ", "think ", identity)
|
||||
reasoning_item: Final = _reasoning(identity, "".join(pieces))
|
||||
completed: Final = _response(identity, (reasoning_item, _message(identity, "done")))
|
||||
events: Final = (
|
||||
events: Final[tuple[Mapping[str, object], ...]] = (
|
||||
{"type": "response.created", "response": {**completed, "status": "in_progress", "output": []}},
|
||||
{"type": "response.output_item.added", "output_index": 0, "item": {**reasoning_item, "content": []}},
|
||||
*(
|
||||
|
|
@ -323,11 +327,26 @@ def test_messages_replays_assistant_text_in_its_original_place_among_tool_calls(
|
|||
"role": "assistant",
|
||||
"content": [
|
||||
{"type": "text", "text": "First I will look."},
|
||||
{"type": "tool_use", "id": f"call_{identity}_a", "name": "Bash", "input": {"command": "ls"}},
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": f"call_{identity}_a",
|
||||
"name": "Bash",
|
||||
"input": {"command": "ls"},
|
||||
},
|
||||
{"type": "text", "text": "Then two more."},
|
||||
{"type": "text", "text": "In parallel."},
|
||||
{"type": "tool_use", "id": f"call_{identity}_b", "name": "Bash", "input": {"command": "pwd"}},
|
||||
{"type": "tool_use", "id": f"call_{identity}_c", "name": "Bash", "input": {"command": "id"}},
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": f"call_{identity}_b",
|
||||
"name": "Bash",
|
||||
"input": {"command": "pwd"},
|
||||
},
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": f"call_{identity}_c",
|
||||
"name": "Bash",
|
||||
"input": {"command": "id"},
|
||||
},
|
||||
],
|
||||
},
|
||||
_tool_results(identity, {"a": "a.txt", "b": "/tmp", "c": "uid=0"}),
|
||||
|
|
|
|||
14
ui/litellm-dashboard/src/lib/http/schema.d.ts
generated
vendored
14
ui/litellm-dashboard/src/lib/http/schema.d.ts
generated
vendored
|
|
@ -28897,6 +28897,8 @@ export interface components {
|
|||
* @description Represents an OpenAI Responses API reasoning item for round-tripping in conversation history.
|
||||
*/
|
||||
ChatCompletionReasoningItem: {
|
||||
/** Content */
|
||||
content?: components["schemas"]["ChatCompletionReasoningTextBlock"][];
|
||||
/** Encrypted Content */
|
||||
encrypted_content?: string | null;
|
||||
/** Id */
|
||||
|
|
@ -28923,6 +28925,18 @@ export interface components {
|
|||
} & {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
/** ChatCompletionReasoningTextBlock */
|
||||
ChatCompletionReasoningTextBlock: {
|
||||
/** Text */
|
||||
text: string;
|
||||
/**
|
||||
* Type
|
||||
* @constant
|
||||
*/
|
||||
type: "reasoning_text";
|
||||
} & {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
/** ChatCompletionRedactedThinkingBlock */
|
||||
ChatCompletionRedactedThinkingBlock: {
|
||||
/** Cache Control */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue