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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Add compaction type block in the output
Sameer Kankute039b37fac148
diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.pyindex 00695cbfb5b..7c8e2ebeaff 100644--- a/litellm/litellm_core_utils/core_helpers.py+++ b/litellm/litellm_core_utils/core_helpers.py@@ -94,8 +94,8 @@ def map_finish_reason(return "length"elif finish_reason == "tool_use": # anthropicreturn "tool_calls"- elif finish_reason == "content_filtered":- return "content_filter"+ elif finish_reason == "compaction":+ return "length"return finish_reasondiff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.pyindex 61616c7f1b4..5cbf014f8fb 100644--- a/litellm/llms/anthropic/chat/transformation.py+++ b/litellm/llms/anthropic/chat/transformation.py@@ -1028,9 +1028,37 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):if beta_value not in existing_values:headers["anthropic-beta"] = f"{existing_beta}, {beta_value}"- def _ensure_context_management_beta_header(self, headers: dict) -> None:- beta_value = ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value- self._ensure_beta_header(headers, beta_value)+ def _ensure_context_management_beta_header(+ self, headers: dict, context_management: dict+ ) -> None:+ """+ Add appropriate beta headers based on context_management edits.+ - If any edit has type "compact_20260112", add compact-2026-01-12 header+ - For all other edits, add context-management-2025-06-27 header+ """+ edits = context_management.get("edits", [])++ has_compact = False+ has_other = False++ for edit in edits:+ edit_type = edit.get("type", "")+ if edit_type == "compact_20260112":+ has_compact = True+ else:+ has_other = True++ # Add compact header if any compact edits exist+ if has_compact:+ self._ensure_beta_header(+ headers, ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_01_12.value+ )++ # Add context management header if any other edits exist+ if has_other:+ self._ensure_beta_header(+ headers, ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value+ )def update_headers_with_optional_anthropic_beta(self, headers: dict, optional_params: dict@@ -1058,7 +1086,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):headers, ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value)if optional_params.get("context_management") is not None:- self._ensure_context_management_beta_header(headers)+ self._ensure_context_management_beta_header(+ headers, optional_params["context_management"]+ )if optional_params.get("output_format") is not None:self._ensure_beta_header(headers, ANTHROPIC_BETA_HEADER_VALUES.STRUCTURED_OUTPUT_2025_09_25.value@@ -1227,6 +1257,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):List[ChatCompletionToolCallChunk],Optional[List[Any]],Optional[List[Any]],+ Optional[List[Any]],]:text_content = ""citations: Optional[List[Any]] = None@@ -1239,6 +1270,8 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):tool_calls: List[ChatCompletionToolCallChunk] = []web_search_results: Optional[List[Any]] = Nonetool_results: Optional[List[Any]] = None+ context_management: Optional[List[Any]] = None+ compaction_blocks: Optional[List[Any]] = Nonefor idx, content in enumerate(completion_response["content"]):if content["type"] == "text":text_content += content["text"]@@ -1280,6 +1313,12 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):thinking_blocks.append(cast(ChatCompletionRedactedThinkingBlock, content))++ ## COMPACTION+ elif content["type"] == "compaction":+ if compaction_blocks is None:+ compaction_blocks = []+ compaction_blocks.append(content)## CITATIONSif content.get("citations") is not None:@@ -1301,7 +1340,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):if thinking_content is not None:reasoning_content += thinking_content- return text_content, citations, thinking_blocks, reasoning_content, tool_calls, web_search_results, tool_results+ return text_content, citations, thinking_blocks, reasoning_content, tool_calls, web_search_results, tool_results, compaction_blocksdef calculate_usage(self,@@ -1444,6 +1483,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):tool_calls,web_search_results,tool_results,+ compaction_blocks,) = self.extract_response_content(completion_response=completion_response)if (@@ -1471,6 +1511,8 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):provider_specific_fields["tool_results"] = tool_resultsif container is not None:provider_specific_fields["container"] = container+ if compaction_blocks is not None:+ provider_specific_fields["compaction_blocks"] = compaction_blocks_message = litellm.Message(tool_calls=tool_calls,@@ -1479,6 +1521,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):thinking_blocks=thinking_blocks,reasoning_content=reasoning_content,)+ _message.provider_specific_fields = provider_specific_fields## HANDLE JSON MODE - anthropic returns single function calljson_mode_message = self._transform_response_for_json_mode(@@ -1509,18 +1552,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):model_response.created = int(time.time())model_response.model = completion_response["model"]- context_management_response = completion_response.get("context_management")- if context_management_response is not None:- _hidden_params["context_management"] = context_management_response- try:- model_response.__dict__["context_management"] = (- context_management_response- )- except Exception:- pass-model_response._hidden_params = _hidden_params-return model_responsedef get_prefix_prompt(self, messages: List[AllMessageValues]) -> Optional[str]:diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.pyindex 62e775d4faa..85d419ccc7d 100644--- a/litellm/types/llms/anthropic.py+++ b/litellm/types/llms/anthropic.py@@ -633,6 +633,7 @@ class ANTHROPIC_BETA_HEADER_VALUES(str, Enum):WEB_FETCH_2025_09_10 = "web-fetch-2025-09-10"WEB_SEARCH_2025_03_05 = "web-search-2025-03-05"CONTEXT_MANAGEMENT_2025_06_27 = "context-management-2025-06-27"+ COMPACT_2026_01_12 = "compact-2026-01-12"STRUCTURED_OUTPUT_2025_09_25 = "structured-outputs-2025-11-13"ADVANCED_TOOL_USE_2025_11_20 = "advanced-tool-use-2025-11-20"