Files
crewAI/lib
Devin AI e0ee53b1b6 Fix: Load .env before importing crew module in chat command
This fixes issue #3934 where 'crewai chat' fails when the crew has
module-level LLM instantiation that requires OPENAI_API_KEY.

The issue occurred because 'crewai chat' imports the crew module directly
using __import__(), and any module-level code executes immediately during
import. At this point, the .env file hadn't been loaded yet, so
OPENAI_API_KEY was not available in the environment.

Changes:
- Load .env file in load_crew_and_name() before importing the crew module
- Use os.environ.setdefault() to avoid overriding existing env vars
- Add comprehensive tests covering:
  - Loading crew with .env containing OPENAI_API_KEY
  - Environment variable precedence (existing vars not overridden)
  - Loading crew without .env file

Fixes #3934

Co-Authored-By: João <joao@crewai.com>
2025-11-17 22:18:23 +00:00
..