From 448e995143ca2ef35c06a468d223e823076c6dff Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Tue, 4 Nov 2025 18:11:32 +0530 Subject: [PATCH] use generic method to remove the unsupported param --- litellm/llms/anthropic/chat/transformation.py | 4 -- .../anthropic_claude3_transformation.py | 1 - .../anthropic_claude3_transformation.py | 6 +-- .../llms/openai/chat/gpt_transformation.py | 1 + .../anthropic/transformation.py | 1 - .../transformation.py | 29 ++++++++++- litellm/responses/main.py | 3 ++ proxy_server_config.yaml | 8 ++- .../test_anthropic_chat_transformation.py | 42 ++++------------ ...ations_anthropic_claude3_transformation.py | 50 ++++--------------- ...partner_models_anthropic_transformation.py | 43 ++++------------ 11 files changed, 66 insertions(+), 122 deletions(-) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 81a99461c64..adaf8e46d25 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -736,15 +736,11 @@ 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, **optional_params, } - return data def _transform_response_for_json_mode( 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 8bd00760809..9b13d3df08e 100644 --- a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py +++ b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py @@ -80,7 +80,6 @@ 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 087494ca166..80a68901666 100644 --- a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py +++ b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py @@ -129,11 +129,7 @@ class AmazonAnthropicClaudeMessagesConfig( 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) - - # 5. Handle anthropic_beta from user headers + # 4. 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/openai/chat/gpt_transformation.py b/litellm/llms/openai/chat/gpt_transformation.py index 4e553a3da5c..0e2ec36d081 100644 --- a/litellm/llms/openai/chat/gpt_transformation.py +++ b/litellm/llms/openai/chat/gpt_transformation.py @@ -160,6 +160,7 @@ class OpenAIGPTConfig(BaseLLMModelInfo, BaseConfig): "web_search_options", "service_tier", "safety_identifier", + "prompt_cache_key", ] # works across all models model_specific_params = [] 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 4e9802f4771..7ba788e335c 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,7 +68,6 @@ 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/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index e021f4c16d1..2d77cd03457 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -12,6 +12,9 @@ from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging from litellm.responses.litellm_completion_transformation.session_handler import ( ResponsesSessionHandler, ) +from litellm.litellm_core_utils.get_supported_openai_params import ( + get_supported_openai_params, +) from litellm.types.llms.openai import ( AllMessageValues, ChatCompletionImageObject, @@ -94,6 +97,23 @@ class LiteLLMCompletionResponsesConfig: "user", ] + @staticmethod + def filter_unsupported_params( + params: dict, + model: str, + custom_llm_provider: Optional[str] = None, + ) -> None: + """Remove params not supported by the provider.""" + supported_params = get_supported_openai_params( + model=model, custom_llm_provider=custom_llm_provider + ) + if supported_params: + keys_to_remove = [ + key for key in params.keys() if key not in supported_params + ] + for key in keys_to_remove: + params.pop(key, None) + @staticmethod def transform_responses_api_request_to_chat_completion_request( model: str, @@ -118,6 +138,12 @@ class LiteLLMCompletionResponsesConfig: text_param ) + LiteLLMCompletionResponsesConfig.filter_unsupported_params( + params=responses_api_request, + model=model, + custom_llm_provider=custom_llm_provider, + ) + litellm_completion_request: dict = { "messages": LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( input=input, @@ -136,6 +162,7 @@ class LiteLLMCompletionResponsesConfig: "service_tier": kwargs.get("service_tier"), "web_search_options": web_search_options, "response_format": response_format, + "prompt_cache_key": responses_api_request.get("prompt_cache_key"), # litellm specific params "custom_llm_provider": custom_llm_provider, "extra_headers": extra_headers, @@ -157,7 +184,7 @@ class LiteLLMCompletionResponsesConfig: litellm_completion_request = { k: v for k, v in litellm_completion_request.items() if v is not None } - + print(f"litellm_completion_request: {litellm_completion_request.keys()}") return litellm_completion_request @staticmethod diff --git a/litellm/responses/main.py b/litellm/responses/main.py index c297a87077b..6acf55720a7 100644 --- a/litellm/responses/main.py +++ b/litellm/responses/main.py @@ -134,6 +134,7 @@ async def aresponses_api_with_mcp( top_p: Optional[float] = None, truncation: Optional[Literal["auto", "disabled"]] = None, user: Optional[str] = None, + prompt_cache_key: Optional[str] = None, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Optional[Dict[str, Any]] = None, @@ -489,6 +490,7 @@ def responses( user: Optional[str] = None, service_tier: Optional[str] = None, safety_identifier: Optional[str] = None, + prompt_cache_key: Optional[str] = None, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Optional[Dict[str, Any]] = None, @@ -571,6 +573,7 @@ def responses( top_p=top_p, truncation=truncation, user=user, + prompt_cache_key=prompt_cache_key, extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, diff --git a/proxy_server_config.yaml b/proxy_server_config.yaml index 02edb07ebf3..30249f68071 100644 --- a/proxy_server_config.yaml +++ b/proxy_server_config.yaml @@ -94,9 +94,13 @@ model_list: rpm: 1000 model_info: health_check_timeout: 1 - - model_name: "*" + - model_name: "gpt-4o-mini" litellm_params: - model: openai/* + model: openai/gpt-4o-mini + api_key: os.environ/OPENAI_API_KEY + - model_name: claude-sonnet-4 + litellm_params: + model: openai/gpt-4o-mini api_key: os.environ/OPENAI_API_KEY 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 781b2df068d..08ac972ecfb 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 @@ -372,42 +372,18 @@ def test_prompt_cache_key_removed_from_request(): 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 that prompt_cache_key is NOT in Anthropic's supported params + supported_params = config.get_supported_openai_params("claude-3-7-sonnet-20250219") + assert "prompt_cache_key" not in supported_params, ( + "prompt_cache_key should not be in Anthropic's supported params list" ) - # 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 common parameters are present + assert "max_tokens" in supported_params + assert "temperature" in supported_params + assert "tools" in supported_params - # 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") + print("✅ Test passed: prompt_cache_key is not in Anthropic's supported params") 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 f9fea3b168a..a3e316617cd 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 @@ -33,47 +33,15 @@ def test_prompt_cache_key_removed_from_bedrock_request(): """ 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 that prompt_cache_key is NOT in Bedrock Anthropic's supported params + supported_params = config.get_supported_openai_params("anthropic.claude-3-7-sonnet-20250219-v1:0") + assert "prompt_cache_key" not in supported_params, ( + "prompt_cache_key should not be in Bedrock Anthropic's supported params list" ) - # 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 other common parameters are present + assert "max_tokens" in supported_params or "max_completion_tokens" in supported_params + assert "temperature" in supported_params + assert "tools" in supported_params - # 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") + print("✅ Test passed: prompt_cache_key is not in Bedrock Anthropic's supported params") 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 fb325598de2..18d0aa5ac96 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 @@ -45,40 +45,15 @@ def test_prompt_cache_key_removed_from_vertex_ai_request(): """ 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 that prompt_cache_key is NOT in Vertex AI Anthropic's supported params + supported_params = config.get_supported_openai_params("claude-3-7-sonnet-20250219") + assert "prompt_cache_key" not in supported_params, ( + "prompt_cache_key should not be in Vertex AI Anthropic's supported params list" ) - # 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 other common parameters are present + assert "max_tokens" in supported_params or "max_completion_tokens" in supported_params + assert "temperature" in supported_params + assert "tools" in supported_params - # 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") + print("✅ Test passed: prompt_cache_key is not in Vertex AI Anthropic's supported params")