Skip to content

Import-time load_dotenv() in crewai.llm and crewai.project.crew_base is ungated — add an opt-out #9744

Done

Description

@freedom2132

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 importing crewai.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 .env setting HTTPS_PROXY / REQUESTS_CA_BUNDLE to attacker-controlled values via an import-time load_dotenv and 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:

  1. 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).

  1. Restrict the autoload to a .env in the CWD itself (not the upward find_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

  1. jimmyallinai commented on Nov 25, 2026

    @jimmyallinai

    I'll take this one — implementing the opt-out flag (Option 1 from the issue) and opening a PR shortly.

  2. Vidit-Ostwal commented on Nov 25, 2026

    @Vidit-Ostwal
    Member

    Thanks for the report, Freedomn (@freedom2132). I'm closing this as not planned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions