diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 8263ea4cf01..3cd19fad1fb 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -1573,6 +1573,12 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) return _message + @staticmethod + def _strip_leaked_think_prefix(text_content: str) -> str: + if "" not in text_content: + return text_content + return text_content.split("", 1)[1].lstrip() + def extract_response_content(self, completion_response: dict) -> Tuple[ str, Optional[List[Any]], @@ -1667,8 +1673,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): if thinking_content is not None: reasoning_content += thinking_content - if "" in text_content: - text_content = text_content.split("", 1)[1].lstrip() + text_content = self._strip_leaked_think_prefix(text_content) return ( text_content, diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index cfe52007c58..8e0f15e54ea 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -1385,12 +1385,12 @@ class LiteLLMAnthropicMessagesAdapter: raw_id = choice.delta.tool_calls[0].id or str(uuid.uuid4()) tool_name = choice.delta.tool_calls[0].function.name or "" base_id = raw_id - thought_sig: Optional[str] = None + thought_sig: str | None = None if THOUGHT_SIGNATURE_SEPARATOR in raw_id: parts = raw_id.split(THOUGHT_SIGNATURE_SEPARATOR, 1) base_id = parts[0] thought_sig = parts[1] if len(parts) > 1 else None - tool_block: Dict[str, Any] = { + tool_block: dict[str, Any] = { "type": "tool_use", "id": base_id, "name": tool_name,