From 7bf38c6312b19551331c3b698c1b6631f552db78 Mon Sep 17 00:00:00 2001 From: Gennaro Malafronte <8798987+malafronte@users.noreply.github.com> Date: Thu, 23 Apr 2026 00:16:00 +0200 Subject: [PATCH] fix(anthropic): strip leaked think tags from messages --- litellm/llms/anthropic/chat/transformation.py | 3 ++ .../adapters/transformation.py | 8 +++-- .../messages/handler.py | 34 ++++++++++--------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 31b9bad3395..8263ea4cf01 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -1667,6 +1667,9 @@ 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() + return ( text_content, citations, diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index 23aed56e65f..e3c62dc9ea7 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -1266,10 +1266,14 @@ class LiteLLMAnthropicMessagesAdapter: ) # Handle text content - if choice.message.content is not None: + text_content = choice.message.content + if isinstance(text_content, str) and "" in text_content: + text_content = text_content.split("", 1)[1].lstrip() + + if text_content: new_content.append( AnthropicResponseContentBlockText( - type="text", text=choice.message.content + type="text", text=text_content ).model_dump() ) # Handle tool calls (in parallel to text content) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py index 0c59e812e0b..583f0e8b01b 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py @@ -454,20 +454,22 @@ def anthropic_messages_handler( "display": "summarized", } - return base_llm_http_handler.anthropic_messages_handler( - model=model, - messages=messages, - anthropic_messages_provider_config=anthropic_messages_provider_config, - anthropic_messages_optional_request_params=dict( - anthropic_messages_optional_request_params - ), - _is_async=is_async, - client=client, - custom_llm_provider=custom_llm_provider, - litellm_params=litellm_params, - logging_obj=litellm_logging_obj, - api_key=api_key, - api_base=api_base, - stream=stream, - kwargs=kwargs, + return _sanitize_think_tag_text_blocks( + base_llm_http_handler.anthropic_messages_handler( + model=model, + messages=messages, + anthropic_messages_provider_config=anthropic_messages_provider_config, + anthropic_messages_optional_request_params=dict( + anthropic_messages_optional_request_params + ), + _is_async=is_async, + client=client, + custom_llm_provider=custom_llm_provider, + litellm_params=litellm_params, + logging_obj=litellm_logging_obj, + api_key=api_key, + api_base=api_base, + stream=stream, + kwargs=kwargs, + ) )