feat(credential_accessor.py): support loading in credentials from credential_list

Resolves https://github.com/BerriAI/litellm/issues/9114
This commit is contained in:
Krrish Dholakia 2025-03-10 17:15:58 -07:00
parent 236e68910c
commit e518e3558b
5 changed files with 46 additions and 13 deletions

View file

@ -0,0 +1,15 @@
"""Utils for accessing credentials."""
import litellm
class CredentialAccessor:
@staticmethod
def get_credential_values(credential_name: str) -> dict:
"""Safe accessor for credentials."""
if not litellm.credential_list:
return {}
for credential in litellm.credential_list:
if credential.credential_name == credential_name:
return credential.credential_values.copy()
return {}

View file

@ -2,7 +2,7 @@ model_list:
- model_name: gpt-4o - model_name: gpt-4o
litellm_params: litellm_params:
model: azure/gpt-4o model: azure/gpt-4o
credential_name: default_azure_credential litellm_credential_name: default_azure_credential
credential_list: credential_list:
- credential_name: default_azure_credential - credential_name: default_azure_credential

View file

@ -5370,18 +5370,18 @@ class Router:
client = self.cache.get_cache( client = self.cache.get_cache(
key=cache_key, local_only=True, parent_otel_span=parent_otel_span key=cache_key, local_only=True, parent_otel_span=parent_otel_span
) )
if client is None: # if client is None:
""" # """
Re-initialize the client # Re-initialize the client
""" # """
InitalizeOpenAISDKClient.set_client( # InitalizeOpenAISDKClient.set_client(
litellm_router_instance=self, model=deployment # litellm_router_instance=self, model=deployment
) # )
client = self.cache.get_cache( # client = self.cache.get_cache(
key=cache_key, # key=cache_key,
local_only=True, # local_only=True,
parent_otel_span=parent_otel_span, # parent_otel_span=parent_otel_span,
) # )
return client return client
else: else:
if kwargs.get("stream") is True: if kwargs.get("stream") is True:

View file

@ -1815,6 +1815,7 @@ all_litellm_params = [
"budget_duration", "budget_duration",
"use_in_pass_through", "use_in_pass_through",
"merge_reasoning_content_in_choices", "merge_reasoning_content_in_choices",
"litellm_credential_name",
] + list(StandardCallbackDynamicParams.__annotations__.keys()) ] + list(StandardCallbackDynamicParams.__annotations__.keys())

View file

@ -66,6 +66,7 @@ from litellm.litellm_core_utils.core_helpers import (
map_finish_reason, map_finish_reason,
process_response_headers, process_response_headers,
) )
from litellm.litellm_core_utils.credential_accessor import CredentialAccessor
from litellm.litellm_core_utils.default_encoding import encoding from litellm.litellm_core_utils.default_encoding import encoding
from litellm.litellm_core_utils.exception_mapping_utils import ( from litellm.litellm_core_utils.exception_mapping_utils import (
_get_response_headers, _get_response_headers,
@ -141,6 +142,7 @@ from litellm.types.utils import (
ChatCompletionMessageToolCall, ChatCompletionMessageToolCall,
Choices, Choices,
CostPerToken, CostPerToken,
CredentialItem,
CustomHuggingfaceTokenizer, CustomHuggingfaceTokenizer,
Delta, Delta,
Embedding, Embedding,
@ -455,6 +457,18 @@ def get_applied_guardrails(kwargs: Dict[str, Any]) -> List[str]:
return applied_guardrails return applied_guardrails
def load_credentials_from_list(kwargs: dict):
"""
Updates kwargs with the credentials if credential_name in kwarg
"""
credential_name = kwargs.get("litellm_credential_name")
if credential_name and litellm.credential_list:
credential_accessor = CredentialAccessor.get_credential_values(credential_name)
for key, value in credential_accessor.items():
if key not in kwargs:
kwargs[key] = value
def get_dynamic_callbacks( def get_dynamic_callbacks(
dynamic_callbacks: Optional[List[Union[str, Callable, CustomLogger]]] dynamic_callbacks: Optional[List[Union[str, Callable, CustomLogger]]]
) -> List: ) -> List:
@ -485,6 +499,9 @@ def function_setup( # noqa: PLR0915
## GET APPLIED GUARDRAILS ## GET APPLIED GUARDRAILS
applied_guardrails = get_applied_guardrails(kwargs) applied_guardrails = get_applied_guardrails(kwargs)
## LOAD CREDENTIALS
load_credentials_from_list(kwargs)
## LOGGING SETUP ## LOGGING SETUP
function_id: Optional[str] = kwargs["id"] if "id" in kwargs else None function_id: Optional[str] = kwargs["id"] if "id" in kwargs else None