Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/crewai/src/crewai/llms/providers/anthropic/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion lib/crewai/src/crewai/llms/providers/azure/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion lib/crewai/src/crewai/llms/providers/gemini/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lib/crewai/src/crewai/llms/providers/openai/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down