diff --git a/lib/crewai/src/crewai/llms/providers/gemini/completion.py b/lib/crewai/src/crewai/llms/providers/gemini/completion.py index c092f539d..1607b6395 100644 --- a/lib/crewai/src/crewai/llms/providers/gemini/completion.py +++ b/lib/crewai/src/crewai/llms/providers/gemini/completion.py @@ -130,6 +130,14 @@ class GeminiCompletion(BaseLLM): def _get_sync_client(self) -> Any: if self._client is None: + # Re-read env vars so a deferred build can pick up credentials + # that weren't set at instantiation time. + if not self.api_key: + self.api_key = os.getenv("GOOGLE_API_KEY") or os.getenv( + "GEMINI_API_KEY" + ) + if not self.project: + self.project = os.getenv("GOOGLE_CLOUD_PROJECT") self._client = self._initialize_client(self.use_vertexai) return self._client diff --git a/lib/crewai/tests/llms/google/test_google.py b/lib/crewai/tests/llms/google/test_google.py index 25b755dae..f6e94f89e 100644 --- a/lib/crewai/tests/llms/google/test_google.py +++ b/lib/crewai/tests/llms/google/test_google.py @@ -64,6 +64,23 @@ def test_gemini_completion_module_is_imported(): assert hasattr(completion_mod, 'GeminiCompletion') +def test_gemini_lazy_build_reads_env_vars_set_after_construction(): + """When `LLM(model="gemini/...")` is constructed before env vars are set, + the lazy client builder must re-read `GOOGLE_API_KEY` / `GEMINI_API_KEY` + so the LLM works once credentials become available.""" + from crewai.llms.providers.gemini.completion import GeminiCompletion + + with patch.dict(os.environ, {}, clear=True): + llm = GeminiCompletion(model="gemini-1.5-pro") + assert llm.api_key is None + assert llm._client is None + + with patch.dict(os.environ, {"GEMINI_API_KEY": "late-key"}, clear=True): + client = llm._get_sync_client() + assert client is not None + assert llm.api_key == "late-key" + + def test_native_gemini_raises_error_when_initialization_fails(): """ Test that LLM raises ImportError when native Gemini completion fails.