Description
The Swarms CLI does not reliably locate or load the .env file, causing commands to fail silently when API keys and environment variables are not picked up.
Steps to Reproduce
- Create a
.env file in the project root with valid API keys (e.g. OPENAI_API_KEY=sk-...)
- Run any CLI command:
swarms agent --task "hello"
- The CLI proceeds as if no API keys are set, or raises an API key error
Expected Behavior
The CLI should automatically discover and load .env from the current working directory (or the user's home directory as a fallback) before executing any command.
Actual Behavior
API keys defined in .env are not loaded. Commands either fail with an API key error or the setup-check command reports no active provider.
Environment
- OS: macOS / Linux
- Python version: 3.10+
- Swarms version:
swarms --version
Root Cause (suspected)
load_swarms_env() is called at module import time in swarms/cli/main.py, but if the working directory at import time differs from where the .env lives, the file is not found. Additionally, python-dotenv is not explicitly searching parent directories.
Suggested Fix
- Use
find_dotenv() from python-dotenv to search parent directories
- Or explicitly resolve the
.env path relative to Path.cwd() and fall back to Path.home()
Additional Context
Running swarms setup-check should display a clear error when .env is missing or empty rather than silently continuing with no keys loaded.
🤖 Generated with Claude Code
Description
The Swarms CLI does not reliably locate or load the
.envfile, causing commands to fail silently when API keys and environment variables are not picked up.Steps to Reproduce
.envfile in the project root with valid API keys (e.g.OPENAI_API_KEY=sk-...)swarms agent --task "hello"Expected Behavior
The CLI should automatically discover and load
.envfrom the current working directory (or the user's home directory as a fallback) before executing any command.Actual Behavior
API keys defined in
.envare not loaded. Commands either fail with an API key error or thesetup-checkcommand reports no active provider.Environment
swarms --versionRoot Cause (suspected)
load_swarms_env()is called at module import time inswarms/cli/main.py, but if the working directory at import time differs from where the.envlives, the file is not found. Additionally,python-dotenvis not explicitly searching parent directories.Suggested Fix
find_dotenv()frompython-dotenvto search parent directories.envpath relative toPath.cwd()and fall back toPath.home()Additional Context
Running
swarms setup-checkshould display a clear error when.envis missing or empty rather than silently continuing with no keys loaded.🤖 Generated with Claude Code