diff --git a/libs/community/langchain_community/chat_models/litellm.py b/libs/community/langchain_community/chat_models/litellm.py index d6c9557339857..4888390c5a1e0 100644 --- a/libs/community/langchain_community/chat_models/litellm.py +++ b/libs/community/langchain_community/chat_models/litellm.py @@ -225,6 +225,7 @@ class ChatLiteLLM(BaseChatModel): replicate_api_key: Optional[str] = None cohere_api_key: Optional[str] = None openrouter_api_key: Optional[str] = None + api_key: Optional[str] = None streaming: bool = False api_base: Optional[str] = None organization: Optional[str] = None @@ -271,6 +272,21 @@ def _client_params(self) -> Dict[str, Any]: if self.model_name is not None: set_model_value = self.model_name self.client.api_base = self.api_base + self.client.api_key = self.api_key + for named_api_key in [ + "openai_api_key", + "azure_api_key", + "anthropic_api_key", + "replicate_api_key", + "cohere_api_key", + "openrouter_api_key", + ]: + if api_key_value := getattr(self, named_api_key): + setattr( + self.client, + named_api_key.replace("_api_key", "_key"), + api_key_value, + ) self.client.organization = self.organization creds: Dict[str, Any] = { "model": set_model_value,