diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 691b46af8da..81a99461c64 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -736,6 +736,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ): optional_params["metadata"] = {"user_id": _litellm_metadata["user_id"]} + if "prompt_cache_key" in optional_params: + optional_params.pop("prompt_cache_key") + data = { "model": model, "messages": anthropic_messages, diff --git a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py index 9b13d3df08e..8bd00760809 100644 --- a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py +++ b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py @@ -80,6 +80,7 @@ class AmazonAnthropicClaudeConfig(AmazonInvokeConfig, AnthropicConfig): _anthropic_request.pop("model", None) _anthropic_request.pop("stream", None) + _anthropic_request.pop("prompt_cache_key", None) if "anthropic_version" not in _anthropic_request: _anthropic_request["anthropic_version"] = self.anthropic_version diff --git a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py index be782d35766..087494ca166 100644 --- a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py +++ b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py @@ -128,8 +128,12 @@ class AmazonAnthropicClaudeMessagesConfig( # 3. `model` is not allowed in request body for bedrock invoke if "model" in anthropic_messages_request: anthropic_messages_request.pop("model", None) + + # 4. `prompt_cache_key` is not allowed in request body for bedrock invoke + if "prompt_cache_key" in anthropic_messages_request: + anthropic_messages_request.pop("prompt_cache_key", None) - # 4. Handle anthropic_beta from user headers + # 5. Handle anthropic_beta from user headers anthropic_beta_list = get_anthropic_beta_from_headers(headers) if anthropic_beta_list: anthropic_messages_request["anthropic_beta"] = anthropic_beta_list diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py index 7ba788e335c..4e9802f4771 100644 --- a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py +++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py @@ -68,6 +68,7 @@ class VertexAIAnthropicConfig(AnthropicConfig): ) data.pop("model", None) # vertex anthropic doesn't accept 'model' parameter + data.pop("prompt_cache_key", None) # vertex anthropic doesn't accept 'prompt_cache_key' parameter return data def transform_response( diff --git a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py index b556b0e5bee..781b2df068d 100644 --- a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py +++ b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py @@ -364,3 +364,50 @@ def test_get_supported_params_thinking(): config = AnthropicConfig() params = config.get_supported_openai_params(model="claude-sonnet-4-20250514") assert "thinking" in params + + +def test_prompt_cache_key_removed_from_request(): + """ + Test that prompt_cache_key is removed from the request before sending to Anthropic. + + This prevents the error: "prompt_cache_key: Extra inputs are not permitted" + + Related issue: https://github.com/BerriAI/litellm/issues/xxxxx + """ + config = AnthropicConfig() + + # Simulate a request with prompt_cache_key + messages = [ + {"role": "user", "content": "Hello, how are you?"} + ] + + optional_params = { + "max_tokens": 100, + "temperature": 0.7, + "prompt_cache_key": "test-cache-key-12345", # This should be removed + } + + litellm_params = {} + headers = {"anthropic-version": "2023-06-01"} + + # Transform the request + result = config.transform_request( + model="claude-3-7-sonnet-20250219", + messages=messages, + optional_params=optional_params, + litellm_params=litellm_params, + headers=headers, + ) + + # Verify prompt_cache_key is NOT in the result + assert "prompt_cache_key" not in result, "prompt_cache_key should be removed from the request" + + # Verify other parameters are still present + assert "max_tokens" in result + assert result["max_tokens"] == 100 + assert "temperature" in result + assert result["temperature"] == 0.7 + assert "model" in result + assert "messages" in result + + print("✅ Test passed: prompt_cache_key successfully removed from Anthropic request") diff --git a/tests/test_litellm/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py b/tests/test_litellm/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py index e6486ae9677..f9fea3b168a 100644 --- a/tests/test_litellm/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py +++ b/tests/test_litellm/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py @@ -20,3 +20,60 @@ def test_get_supported_params_thinking(): model="anthropic.claude-sonnet-4-20250514-v1:0" ) assert "thinking" in params + + +def test_prompt_cache_key_removed_from_bedrock_request(): + """ + Test that prompt_cache_key is removed from Bedrock Anthropic requests. + + This prevents the error: "prompt_cache_key: Extra inputs are not permitted" + when using Claude models on AWS Bedrock. + + Related issue: Bedrock doesn't support prompt_cache_key parameter + """ + config = AmazonAnthropicClaudeConfig() + + # Simulate a request with prompt_cache_key + messages = [ + {"role": "user", "content": "Hello, how are you?"} + ] + + optional_params = { + "max_tokens": 100, + "temperature": 0.7, + "prompt_cache_key": "test-cache-key-12345", # This should be removed + "stream": True, # This should also be removed for Bedrock + } + + litellm_params = {} + headers = {} + + # Transform the request + result = config.transform_request( + model="anthropic.claude-3-7-sonnet-20250219-v1:0", + messages=messages, + optional_params=optional_params, + litellm_params=litellm_params, + headers=headers, + ) + + # Verify prompt_cache_key is NOT in the result + assert "prompt_cache_key" not in result, "prompt_cache_key should be removed from Bedrock request" + + # Verify model is also removed (Bedrock specific) + assert "model" not in result, "model should be removed from Bedrock request" + + # Verify stream is also removed (Bedrock specific) + assert "stream" not in result, "stream should be removed from Bedrock request" + + # Verify anthropic_version is added (Bedrock specific) + assert "anthropic_version" in result + + # Verify other parameters are still present + assert "max_tokens" in result + assert result["max_tokens"] == 100 + assert "temperature" in result + assert result["temperature"] == 0.7 + assert "messages" in result + + print("✅ Test passed: prompt_cache_key successfully removed from Bedrock Anthropic request") diff --git a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py index 4a65b692a49..fb325598de2 100644 --- a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py +++ b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py @@ -32,3 +32,53 @@ def test_get_supported_params_thinking(): config = VertexAIAnthropicConfig() params = config.get_supported_openai_params(model="claude-sonnet-4") assert "thinking" in params + + +def test_prompt_cache_key_removed_from_vertex_ai_request(): + """ + Test that prompt_cache_key is removed from Vertex AI Anthropic requests. + + This prevents the error: "prompt_cache_key: Extra inputs are not permitted" + when using Claude models on Vertex AI. + + Related issue: Vertex AI doesn't support prompt_cache_key parameter + """ + config = VertexAIAnthropicConfig() + + # Simulate a request with prompt_cache_key + messages = [ + {"role": "user", "content": "Hello, how are you?"} + ] + + optional_params = { + "max_tokens": 100, + "temperature": 0.7, + "prompt_cache_key": "test-cache-key-12345", # This should be removed + } + + litellm_params = {} + headers = {"anthropic-version": "vertex-2023-10-16"} + + # Transform the request + result = config.transform_request( + model="claude-3-7-sonnet-20250219", + messages=messages, + optional_params=optional_params, + litellm_params=litellm_params, + headers=headers, + ) + + # Verify prompt_cache_key is NOT in the result + assert "prompt_cache_key" not in result, "prompt_cache_key should be removed from Vertex AI request" + + # Verify model is also removed (Vertex AI specific) + assert "model" not in result, "model should be removed from Vertex AI request" + + # Verify other parameters are still present + assert "max_tokens" in result + assert result["max_tokens"] == 100 + assert "temperature" in result + assert result["temperature"] == 0.7 + assert "messages" in result + + print("✅ Test passed: prompt_cache_key successfully removed from Vertex AI Anthropic request")