use generic method to remove the unsupported param

This commit is contained in:
Sameer Kankute 2025-11-04 18:11:32 +05:30
parent 18821a0f46
commit 448e995143
11 changed files with 66 additions and 122 deletions

View file

@ -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(

View file

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

View file

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

View file

@ -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 = []

View file

@ -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(

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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