Repository navigation
Description
Problem
Both modules call load_dotenv() at import time with no gate:
lib/crewai/src/crewai/llm.py line 90 (module level):
load_dotenv()lib/crewai/src/crewai/project/crew_base.py line 130 (module level):
load_dotenv()Since crewai/__init__.py line 21 does from crewai.llm import LLM, a bare import crewai from any working directory picks up the nearest .env (python-dotenv's default CWD-upward find_dotenv()) and mutates os.environ for the whole process. There is no opt-out flag, path restriction, or override=False-style escape hatch beyond dotenv's own semantics.
The codebase already treats import-time dotenv loading as a real hazard elsewhere, which is why I think this is worth aligning:
lib/crewai/src/crewai/__init__.py(lines 6-10) pins the machine's proxy/CA before importingcrewai.llm, with a comment saying the pin must precede the import.lib/crewai/src/crewai/llm.py(lines 93-94) lazy-loads litellm specifically "to avoid its module-level dotenv.load_dotenv() from polluting env vars".lib/cli/src/crewai_cli/create_json_crew.py(lines 1303-1306) snapshots and restores proxy/CA env because "Importing crewai_tools runs load_dotenv(), which can pull the .env of the project crewai is installed in into this process ... never let that .env choose them."lib/cli/tests/test_create_crew.py(lines 1039-1103,test_platform_auth_ignores_transport_from_a_dotenv_loaded_on_import) explicitly simulates a hostile.envsettingHTTPS_PROXY/REQUESTS_CA_BUNDLEto attacker-controlled values via an import-timeload_dotenvand asserts the CLI's platform-auth path is isolated from it.
So the threat is clearly acknowledged in tests and partially worked around in the CLI — but the autoload channel itself in crewai.llm and crewai.project.crew_base remains unconditional.
Suggested direction
The current behavior is presumably intentional convenience for the common "cd my-crew && python main.py" workflow, so I'd keep it as the default. Two non-breaking options:
- An opt-out env flag checked before the call, e.g.:
import os
if os.getenv("CREWAI_NO_AUTO_DOTENV", "") not in {"", "0", "false"}:
load_dotenv()(grep shows no CREWAI_NO_AUTO_DOTENV or similar exists today).
- Restrict the autoload to a
.envin the CWD itself (not the upwardfind_dotenv()walk), or expose an explicit API (crewai.load_project_env(path)/crewai.disable_auto_dotenv()) so hosts that manage env themselves can opt out before the first import.
Option 1 is the smallest change and composes with the existing CLI-side isolation. Happy to send a PR if a maintainer indicates which name/semantics they'd prefer.
Activity
jimmyallinai commentedon Nov 25, 2026
I'll take this one — implementing the opt-out flag (Option 1 from the issue) and opening a PR shortly.
Vidit-Ostwal commentedon Nov 25, 2026
Thanks for the report, Freedomn (@freedom2132). I'm closing this as not planned.