Skip to content

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]

Raise clearer error on Anthropic Unified route + allow setting `new_key` on `/key/regenerate`

Krish Dholakiae8d7537b5770
diff --git a/litellm/llms/base_llm/anthropic_messages/transformation.py b/litellm/llms/base_llm/anthropic_messages/transformation.py
index 5bf16eb3cf0..70a29e01005 100644
--- a/litellm/llms/base_llm/anthropic_messages/transformation.py
+++ b/litellm/llms/base_llm/anthropic_messages/transformation.py
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
-from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, List, Optional, Tuple
+from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, List, Optional, Tuple, Union
import httpx
@@ -10,6 +10,7 @@ from litellm.types.router import GenericLiteLLMParams
if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.llms.base_llm.chat.transformation import BaseLLMException
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -109,3 +110,10 @@ class BaseAnthropicMessagesConfig(ABC):
litellm_logging_obj: LiteLLMLoggingObj,
) -> AsyncIterator:
raise NotImplementedError("Subclasses must implement this method")
+
+ def get_error_class(
+ self, error_message: str, status_code: int, headers: Union[dict, httpx.Headers]
+ ) -> "BaseLLMException":
+ return BaseLLMException(
+ message=error_message, status_code=status_code, headers=headers
+ )
diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py
index 132063cc59d..bc3e293a452 100644
--- a/litellm/llms/custom_httpx/llm_http_handler.py
+++ b/litellm/llms/custom_httpx/llm_http_handler.py
@@ -1333,14 +1333,19 @@ class BaseLLMHTTPHandler:
},
)
- response = await async_httpx_client.post(
- url=request_url,
- headers=headers,
- data=signed_json_body or json.dumps(request_body),
- stream=stream or False,
- logging_obj=logging_obj,
- )
- response.raise_for_status()
+ try:
+ response = await async_httpx_client.post(
+ url=request_url,
+ headers=headers,
+ data=signed_json_body or json.dumps(request_body),
+ stream=stream or False,
+ logging_obj=logging_obj,
+ )
+ response.raise_for_status()
+ except Exception as e:
+ raise self._handle_error(
+ e=e, provider_config=anthropic_messages_provider_config
+ )
# used for logging + cost tracking
logging_obj.model_call_details["httpx_response"] = response
@@ -2352,7 +2357,13 @@ class BaseLLMHTTPHandler:
self,
e: Exception,
provider_config: Union[
- BaseConfig, BaseRerankConfig, BaseResponsesAPIConfig, BaseImageEditConfig, BaseVectorStoreConfig, BaseGoogleGenAIGenerateContentConfig
+ BaseConfig,
+ BaseRerankConfig,
+ BaseResponsesAPIConfig,
+ BaseImageEditConfig,
+ BaseVectorStoreConfig,
+ BaseGoogleGenAIGenerateContentConfig,
+ BaseAnthropicMessagesConfig,
],
):
status_code = getattr(e, "status_code", 500)
@@ -2451,7 +2462,10 @@ class BaseLLMHTTPHandler:
_is_async: bool = False,
fake_stream: bool = False,
litellm_metadata: Optional[Dict[str, Any]] = None,
- ) -> Union[ImageResponse, Coroutine[Any, Any, ImageResponse],]:
+ ) -> Union[
+ ImageResponse,
+ Coroutine[Any, Any, ImageResponse],
+ ]:
"""
Handles image edit requests.
@@ -2649,8 +2663,7 @@ class BaseLLMHTTPHandler:
async_httpx_client = client
headers = vector_store_provider_config.validate_environment(
- headers=extra_headers or {},
- litellm_params=litellm_params
+ headers=extra_headers or {}, litellm_params=litellm_params
)
if extra_headers:
@@ -2661,11 +2674,13 @@ class BaseLLMHTTPHandler:
litellm_params=dict(litellm_params),
)
- url, request_body = vector_store_provider_config.transform_search_vector_store_request(
- vector_store_id=vector_store_id,
- query=query,
- vector_store_search_optional_params=vector_store_search_optional_params,
- api_base=api_base,
+ url, request_body = (
+ vector_store_provider_config.transform_search_vector_store_request(
+ vector_store_id=vector_store_id,
+ query=query,
+ vector_store_search_optional_params=vector_store_search_optional_params,
+ api_base=api_base,
+ )
)
logging_obj.pre_call(
@@ -2679,7 +2694,9 @@ class BaseLLMHTTPHandler:
)
try:
- response = await async_httpx_client.post(url=url, headers=headers, json=request_body, timeout=timeout)
+ response = await async_httpx_client.post(
+ url=url, headers=headers, json=request_body, timeout=timeout
+ )
except Exception as e:
raise self._handle_error(e=e, provider_config=vector_store_provider_config)
@@ -2701,7 +2718,9 @@ class BaseLLMHTTPHandler:
timeout: Optional[Union[float, httpx.Timeout]] = None,
client: Optional[Union[HTTPHandler, AsyncHTTPHandler]] = None,
_is_async: bool = False,
- ) -> Union[VectorStoreSearchResponse, Coroutine[Any, Any, VectorStoreSearchResponse]]:
+ ) -> Union[
+ VectorStoreSearchResponse, Coroutine[Any, Any, VectorStoreSearchResponse]
+ ]:
if _is_async:
return self.async_vector_store_search_handler(
vector_store_id=vector_store_id,
@@ -2725,8 +2744,7 @@ class BaseLLMHTTPHandler:
sync_httpx_client = client
headers = vector_store_provider_config.validate_environment(
- headers=extra_headers or {},
- litellm_params=litellm_params
+ headers=extra_headers or {}, litellm_params=litellm_params
)
if extra_headers:
@@ -2737,11 +2755,13 @@ class BaseLLMHTTPHandler:
litellm_params=dict(litellm_params),
)
- url, request_body = vector_store_provider_config.transform_search_vector_store_request(
- vector_store_id=vector_store_id,
- query=query,
- vector_store_search_optional_params=vector_store_search_optional_params,
- api_base=api_base,
+ url, request_body = (
+ vector_store_provider_config.transform_search_vector_store_request(
+ vector_store_id=vector_store_id,
+ query=query,
+ vector_store_search_optional_params=vector_store_search_optional_params,
+ api_base=api_base,
+ )
)
logging_obj.pre_call(
@@ -2755,7 +2775,9 @@ class BaseLLMHTTPHandler:
)
try:
- response = sync_httpx_client.post(url=url, headers=headers, json=request_body)
+ response = sync_httpx_client.post(
+ url=url, headers=headers, json=request_body
+ )
except Exception as e:
raise self._handle_error(e=e, provider_config=vector_store_provider_config)
@@ -2785,8 +2807,7 @@ class BaseLLMHTTPHandler:
async_httpx_client = client
headers = vector_store_provider_config.validate_environment(
- headers=extra_headers or {},
- litellm_params=litellm_params
+ headers=extra_headers or {}, litellm_params=litellm_params
)
if extra_headers:
@@ -2797,9 +2818,11 @@ class BaseLLMHTTPHandler:
litellm_params=dict(litellm_params),
)
- url, request_body = vector_store_provider_config.transform_create_vector_store_request(
- vector_store_create_optional_params=vector_store_create_optional_params,
- api_base=api_base,
+ url, request_body = (
+ vector_store_provider_config.transform_create_vector_store_request(
+ vector_store_create_optional_params=vector_store_create_optional_params,
+ api_base=api_base,
+ )
)
logging_obj.pre_call(
@@ -2813,7 +2836,9 @@ class BaseLLMHTTPHandler:
)
try:
- response = await async_httpx_client.post(url=url, headers=headers, json=request_body, timeout=timeout)
+ response = await async_httpx_client.post(
+ url=url, headers=headers, json=request_body, timeout=timeout
+ )
except Exception as e:
raise self._handle_error(e=e, provider_config=vector_store_provider_config)
@@ -2833,7 +2858,9 @@ class BaseLLMHTTPHandler:
timeout: Optional[Union[float, httpx.Timeout]] = None,
client: Optional[Union[HTTPHandler, AsyncHTTPHandler]] = None,
_is_async: bool = False,
- ) -> Union[VectorStoreCreateResponse, Coroutine[Any, Any, VectorStoreCreateResponse]]:
+ ) -> Union[
+ VectorStoreCreateResponse, Coroutine[Any, Any, VectorStoreCreateResponse]
+ ]:
if _is_async:
return self.async_vector_store_create_handler(
vector_store_create_optional_params=vector_store_create_optional_params,
@@ -2855,8 +2882,7 @@ class BaseLLMHTTPHandler:
sync_httpx_client = client
headers = vector_store_provider_config.validate_environment(
- headers=extra_headers or {},
- litellm_params=litellm_params
+ headers=extra_headers or {}, litellm_params=litellm_params
)
if extra_headers:
@@ -2867,9 +2893,11 @@ class BaseLLMHTTPHandler:
litellm_params=dict(litellm_params),
)
- url, request_body = vector_store_provider_config.transform_create_vector_store_request(
- vector_store_create_optional_params=vector_store_create_optional_params,
- api_base=api_base,
+ url, request_body = (
+ vector_store_provider_config.transform_create_vector_store_request(
+ vector_store_create_optional_params=vector_store_create_optional_params,
+ api_base=api_base,
+ )
)
logging_obj.pre_call(
@@ -2883,14 +2911,16 @@ class BaseLLMHTTPHandler:
)
try:
- response = sync_httpx_client.post(url=url, headers=headers, json=request_body)
+ response = sync_httpx_client.post(
+ url=url, headers=headers, json=request_body
+ )
except Exception as e:
raise self._handle_error(e=e, provider_config=vector_store_provider_config)
return vector_store_provider_config.transform_create_vector_store_response(
response=response,
)
-
+
#####################################################################
################ Google GenAI GENERATE CONTENT HANDLER ###########################
#####################################################################
@@ -2918,7 +2948,7 @@ class BaseLLMHTTPHandler:
from litellm.google_genai.streaming_iterator import (
GoogleGenAIGenerateContentStreamingIterator,
)
-
+
if _is_async:
return self.async_generate_content_handler(
model=model,
@@ -2944,11 +2974,13 @@ class BaseLLMHTTPHandler:
sync_httpx_client = client
# Get headers and URL from the provider config
- headers, api_base = generate_content_provider_config.sync_get_auth_token_and_url(
- api_base=litellm_params.api_base,
- model=model,
- litellm_params=dict(litellm_params),
- stream=stream,
+ headers, api_base = (
+ generate_content_provider_config.sync_get_auth_token_and_url(
+ api_base=litellm_params.api_base,
+ model=model,
+ litellm_params=dict(litellm_params),
+ stream=stream,
+ )
)
if extra_headers:
@@ -3046,11 +3078,13 @@ class BaseLLMHTTPHandler:
async_httpx_client = client
# Get headers and URL from the provider config
- headers, api_base = await generate_content_provider_config.get_auth_token_and_url(
- model=model,
- litellm_params=dict(litellm_params),
- stream=stream,
- api_base=litellm_params.api_base,
+ headers, api_base = (
+ await generate_content_provider_config.get_auth_token_and_url(
+ model=model,
+ litellm_params=dict(litellm_params),
+ stream=stream,
+ api_base=litellm_params.api_base,
+ )
)
if extra_headers:
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index 63937297aa9..db0959a1d43 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -351,13 +351,11 @@ class LiteLLMRoutes(enum.Enum):
"/v1beta/models/{model_name}:countTokens",
"/v1beta/models/{model_name}:generateContent",
"/v1beta/models/{model_name}:streamGenerateContent",
-
"/models/{model_name}:countTokens",
"/models/{model_name}:generateContent",
"/models/{model_name}:streamGenerateContent",
]
-
apply_guardrail_routes = [
"/guardrails/apply_guardrail",
]
@@ -789,6 +787,7 @@ class UpdateKeyRequest(KeyRequestBase):
class RegenerateKeyRequest(GenerateKeyRequest):
# This needs to be different from UpdateKeyRequest, because "key" is optional for this
key: Optional[str] = None
+ new_key: Optional[str] = None
duration: Optional[str] = None
spend: Optional[float] = None
metadata: Optional[dict] = None
diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py
index 0b1c7f523c0..9215060dfed 100644
--- a/litellm/proxy/management_endpoints/key_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py
@@ -696,6 +696,7 @@ async def prepare_key_update_data(
):
data_json: dict = data.model_dump(exclude_unset=True)
data_json.pop("key", None)
+ data_json.pop("new_key", None)
non_default_values = {}
for k, v in data_json.items():
if k in LiteLLM_ManagementEndpoint_MetadataFields:
@@ -1834,6 +1835,21 @@ async def _rotate_master_key(
)
+def get_new_token(data: Optional[RegenerateKeyRequest]) -> str:
+ if data and data.new_key is not None:
+ new_token = data.new_key
+ if not data.new_key.startswith("sk-"):
+ raise HTTPException(
+ status_code=status.HTTP_400_BAD_REQUEST,
+ detail={
+ "error": "New key must start with 'sk-'. This is to distinguish a key hash (used by litellm for logging / internal logic) from the actual key."
+ },
+ )
+ else:
+ new_token = f"sk-{secrets.token_urlsafe(LENGTH_OF_LITELLM_GENERATED_KEY)}"
+ return new_token
+
+
@router.post(
"/key/{key:path}/regenerate",
tags=["key management"],
@@ -1860,6 +1876,9 @@ async def regenerate_key_fn(
Parameters:
- key: str (path parameter) - The key to regenerate
- data: Optional[RegenerateKeyRequest] - Request body containing optional parameters to update
+ - key: Optional[str] - The key to regenerate.
+ - new_master_key: Optional[str] - The new master key to use, if key is the master key.
+ - new_key: Optional[str] - The new key to use, if key is not the master key. If both set, new_master_key will be used.
- key_alias: Optional[str] - User-friendly key alias
- user_id: Optional[str] - User ID associated with key
- team_id: Optional[str] - Team ID associated with key
@@ -1983,7 +2002,8 @@ async def regenerate_key_fn(
verbose_proxy_logger.debug("key_in_db: %s", _key_in_db)
- new_token = f"sk-{secrets.token_urlsafe(LENGTH_OF_LITELLM_GENERATED_KEY)}"
+ new_token = get_new_token(data=data)
+
new_token_hash = hash_token(new_token)
new_token_key_name = f"sk-...{new_token[-4:]}"
diff --git a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
index ddecb94ab64..54909999038 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
@@ -459,3 +459,36 @@ async def test_key_update_object_permissions_missing_permission_record(monkeypat
# Verify upsert was called to create new record
mock_prisma_client.db.litellm_objectpermissiontable.upsert.assert_called_once()
+
+
+def test_get_new_token_with_valid_key():
+ """Test get_new_token function when provided with a valid key that starts with 'sk-'"""
+ from litellm.proxy._types import RegenerateKeyRequest
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ get_new_token,
+ )
+
+ # Test with valid new_key
+ data = RegenerateKeyRequest(new_key="sk-test123456789")
+ result = get_new_token(data)
+
+ assert result == "sk-test123456789"
+
+
+def test_get_new_token_with_invalid_key():
+ """Test get_new_token function when provided with an invalid key that doesn't start with 'sk-'"""
+ from fastapi import HTTPException
+
+ from litellm.proxy._types import RegenerateKeyRequest
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ get_new_token,
+ )
+
+ # Test with invalid new_key (doesn't start with 'sk-')
+ data = RegenerateKeyRequest(new_key="invalid-key-123")
+
+ with pytest.raises(HTTPException) as exc_info:
+ get_new_token(data)
+
+ assert exc_info.value.status_code == 400
+ assert "New key must start with 'sk-'" in str(exc_info.value.detail)