diff --git a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py index 28122d4dbc..f814a93d92 100644 --- a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py +++ b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py @@ -256,6 +256,8 @@ def _get_client_params(self, include_http_client: bool = True) -> dict[str, Any] self.api_key = os.getenv("ANTHROPIC_API_KEY") if self.api_key is None: raise ValueError("ANTHROPIC_API_KEY is required") + if isinstance(self.api_key, str): + self.api_key = self.api_key.strip() client_params = { "api_key": self.api_key, diff --git a/lib/crewai/src/crewai/llms/providers/azure/completion.py b/lib/crewai/src/crewai/llms/providers/azure/completion.py index d357939bb6..48603d18ed 100644 --- a/lib/crewai/src/crewai/llms/providers/azure/completion.py +++ b/lib/crewai/src/crewai/llms/providers/azure/completion.py @@ -118,7 +118,11 @@ def _normalize_azure_fields(cls, data: Any) -> Any: "Interceptors are currently supported for OpenAI and Anthropic providers only." ) - data["api_key"] = data.get("api_key") or os.getenv("AZURE_API_KEY") + api_key = data.get("api_key") or os.getenv("AZURE_API_KEY") + if isinstance(api_key, str): + api_key = api_key.strip() + data["api_key"] = api_key + data["endpoint"] = ( data.get("endpoint") or os.getenv("AZURE_ENDPOINT") @@ -268,6 +272,8 @@ def _make_client_kwargs(self) -> dict[str, Any]: # module import before deployment env vars were injected). if not self.api_key: self.api_key = os.getenv("AZURE_API_KEY") + if isinstance(self.api_key, str): + self.api_key = self.api_key.strip() if not self.endpoint: endpoint = ( os.getenv("AZURE_ENDPOINT") diff --git a/lib/crewai/src/crewai/llms/providers/gemini/completion.py b/lib/crewai/src/crewai/llms/providers/gemini/completion.py index 8914b6b266..c07bb1193d 100644 --- a/lib/crewai/src/crewai/llms/providers/gemini/completion.py +++ b/lib/crewai/src/crewai/llms/providers/gemini/completion.py @@ -79,11 +79,15 @@ def _normalize_gemini_fields(cls, data: Any) -> Any: seqs = [seqs] data["stop"] = seqs - data["api_key"] = ( + api_key = ( data.get("api_key") or os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY") ) + if isinstance(api_key, str): + api_key = api_key.strip() + data["api_key"] = api_key + data["project"] = data.get("project") or os.getenv("GOOGLE_CLOUD_PROJECT") data["location"] = ( data.get("location") or os.getenv("GOOGLE_CLOUD_LOCATION") or "us-central1" @@ -133,6 +137,8 @@ def _get_sync_client(self) -> Any: self.api_key = os.getenv("GOOGLE_API_KEY") or os.getenv( "GEMINI_API_KEY" ) + if isinstance(self.api_key, str): + self.api_key = self.api_key.strip() if not self.project: self.project = os.getenv("GOOGLE_CLOUD_PROJECT") self._client = self._initialize_client(self.use_vertexai) diff --git a/lib/crewai/src/crewai/llms/providers/openai/completion.py b/lib/crewai/src/crewai/llms/providers/openai/completion.py index 0adcd82d6a..86de963d47 100644 --- a/lib/crewai/src/crewai/llms/providers/openai/completion.py +++ b/lib/crewai/src/crewai/llms/providers/openai/completion.py @@ -246,7 +246,10 @@ def _normalize_openai_fields(cls, data: Any) -> Any: return data if not data.get("provider"): data["provider"] = "openai" - data["api_key"] = data.get("api_key") or os.getenv("OPENAI_API_KEY") + api_key = data.get("api_key") or os.getenv("OPENAI_API_KEY") + if isinstance(api_key, str): + api_key = api_key.strip() + data["api_key"] = api_key if "api_base" not in data: data["api_base"] = None model = data.get("model", "gpt-4o") @@ -363,6 +366,8 @@ def _get_client_params(self) -> dict[str, Any]: self.api_key = os.getenv("OPENAI_API_KEY") if self.api_key is None: raise ValueError("OPENAI_API_KEY is required") + if isinstance(self.api_key, str): + self.api_key = self.api_key.strip() base_params = { "api_key": self.api_key, diff --git a/lib/crewai/src/crewai/llms/providers/openai_compatible/completion.py b/lib/crewai/src/crewai/llms/providers/openai_compatible/completion.py index da4cfd03db..d88ad3c429 100644 --- a/lib/crewai/src/crewai/llms/providers/openai_compatible/completion.py +++ b/lib/crewai/src/crewai/llms/providers/openai_compatible/completion.py @@ -184,11 +184,11 @@ def _resolve_api_key( ValueError: If API key is required but not found. """ if api_key: - return api_key + return api_key.strip() env_key = os.getenv(config.api_key_env) if env_key: - return env_key + return env_key.strip() if config.api_key_required: raise ValueError(