remove unsupported prompt_cache_key for anthropic models

This commit is contained in:
Sameer Kankute 2025-11-03 12:35:07 +05:30
parent 396ab80f56
commit 18821a0f46
7 changed files with 164 additions and 1 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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