From 77e66647b09a427ceb309b5c97af4ece4dc9d1c6 Mon Sep 17 00:00:00 2001 From: Greyson LaLonde Date: Sun, 12 Apr 2026 04:42:19 +0800 Subject: [PATCH] test: update test_create_llm_openai_missing_api_key for lazy init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `LLM(model="gpt-4o")` no longer raises at construction when `OPENAI_API_KEY` is missing — the descriptive error now surfaces when the client is actually built. Update the test to assert that contract: `create_llm` succeeds, and `llm._get_sync_client()` raises. --- lib/crewai/tests/utilities/test_llm_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/crewai/tests/utilities/test_llm_utils.py b/lib/crewai/tests/utilities/test_llm_utils.py index 5d7d70b76..a32fdcbc9 100644 --- a/lib/crewai/tests/utilities/test_llm_utils.py +++ b/lib/crewai/tests/utilities/test_llm_utils.py @@ -119,10 +119,12 @@ def test_create_llm_with_invalid_type() -> None: def test_create_llm_openai_missing_api_key() -> None: - """Test that create_llm raises error when OpenAI API key is missing""" + """Credentials are validated lazily: `create_llm` succeeds, and the + descriptive error only surfaces when the client is actually built.""" with patch.dict(os.environ, {}, clear=True): + llm = create_llm(llm_value="gpt-4o") with pytest.raises((ValueError, ImportError)) as exc_info: - create_llm(llm_value="gpt-4o") + llm._get_sync_client() error_message = str(exc_info.value).lower() assert "openai_api_key" in error_message or "api_key" in error_message