Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

feat(advisor): call strip_advisor_blocks in chat/completions transform_request

Ishaan Jafferab8d92c14c6f
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py
index 28f543c1cdf..185e1b5de3f 100644
--- a/litellm/llms/anthropic/chat/transformation.py
+++ b/litellm/llms/anthropic/chat/transformation.py
@@ -75,7 +75,12 @@ from litellm.utils import (
token_counter,
)
-from ..common_utils import AnthropicError, AnthropicModelInfo, process_anthropic_headers
+from ..common_utils import (
+ AnthropicError,
+ AnthropicModelInfo,
+ process_anthropic_headers,
+ strip_advisor_blocks_from_messages,
+)
if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
@@ -981,11 +986,11 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
if mcp_servers:
optional_params["mcp_servers"] = mcp_servers
elif param == "tool_choice" or param == "parallel_tool_calls":
- _tool_choice: Optional[
- AnthropicMessagesToolChoice
- ] = self._map_tool_choice(
- tool_choice=non_default_params.get("tool_choice"),
- parallel_tool_use=non_default_params.get("parallel_tool_calls"),
+ _tool_choice: Optional[AnthropicMessagesToolChoice] = (
+ self._map_tool_choice(
+ tool_choice=non_default_params.get("tool_choice"),
+ parallel_tool_use=non_default_params.get("parallel_tool_calls"),
+ )
)
if _tool_choice is not None:
@@ -1083,9 +1088,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
self.map_openai_context_management_to_anthropic(value)
)
if anthropic_context_management is not None:
- optional_params[
- "context_management"
- ] = anthropic_context_management
+ optional_params["context_management"] = (
+ anthropic_context_management
+ )
elif param == "speed" and isinstance(value, str):
# Pass through Anthropic-specific speed parameter for fast mode
optional_params["speed"] = value
@@ -1159,9 +1164,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
text=system_message_block["content"],
)
if "cache_control" in system_message_block:
- anthropic_system_message_content[
- "cache_control"
- ] = system_message_block["cache_control"]
+ anthropic_system_message_content["cache_control"] = (
+ system_message_block["cache_control"]
+ )
anthropic_system_message_list.append(
anthropic_system_message_content
)
@@ -1185,9 +1190,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
)
)
if "cache_control" in _content:
- anthropic_system_message_content[
- "cache_control"
- ] = _content["cache_control"]
+ anthropic_system_message_content["cache_control"] = (
+ _content["cache_control"]
+ )
anthropic_system_message_list.append(
anthropic_system_message_content
@@ -1413,6 +1418,16 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
message="{}\nReceived Messages={}".format(str(e), messages),
) # don't use verbose_logger.exception, if exception is raised
+ ## Auto-strip advisor blocks from history if advisor tool is absent.
+ ## Prevents Anthropic 400: advisor_tool_result in history requires advisor tool.
+ _all_tools = optional_params.get("tools") or []
+ _has_advisor = any(
+ isinstance(t, dict) and t.get("type") == "advisor_20260301"
+ for t in _all_tools
+ )
+ if not _has_advisor:
+ anthropic_messages = strip_advisor_blocks_from_messages(anthropic_messages)
+
## Add code_execution tool if container_upload is in messages
_tools = (
cast(
@@ -1500,9 +1515,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
)
return _message
- def extract_response_content(
- self, completion_response: dict
- ) -> Tuple[
+ def extract_response_content(self, completion_response: dict) -> Tuple[
str,
Optional[List[Any]],
Optional[
@@ -1796,9 +1809,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
code_interpreter_results = self._build_code_interpreter_results(
tool_results, code_by_id, container_id
)
- provider_specific_fields[
- "code_interpreter_results"
- ] = code_interpreter_results
+ provider_specific_fields["code_interpreter_results"] = (
+ code_interpreter_results
+ )
container = completion_response.get("container")
if container is not None: