fix(anthropic): strip leaked think tags from messages

This commit is contained in:
Gennaro Malafronte 2026-04-23 00:16:00 +02:00
parent 2dae356850
commit 7bf38c6312
3 changed files with 27 additions and 18 deletions

View file

@ -1667,6 +1667,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
if thinking_content is not None:
reasoning_content += thinking_content
if "</think>" in text_content:
text_content = text_content.split("</think>", 1)[1].lstrip()
return (
text_content,
citations,

View file

@ -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 "</think>" in text_content:
text_content = text_content.split("</think>", 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)

View file

@ -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,
)
)