666ghj/MiroFish · critical · ValueError
缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY
Error message
缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY
What it means
Raised in backend/scripts/run_parallel_simulation.py during LLM setup: the script reads LLM_API_KEY, copies it into os.environ["OPENAI_API_KEY"] for camel-ai's OpenAI backend, and then raises this ValueError if os.environ.get("OPENAI_API_KEY") is still falsy — i.e. neither LLM_API_KEY nor a pre-existing OPENAI_API_KEY is available. It fails before ModelFactory.create, since every simulation turn would fail authentication.
Source
Thrown at backend/scripts/run_parallel_simulation.py:1027
llm_model = boost_model or os.environ.get("LLM_MODEL_NAME", "")
config_label = "[加速LLM]"
else:
# 使用通用配置
llm_api_key = os.environ.get("LLM_API_KEY", "")
llm_base_url = os.environ.get("LLM_BASE_URL", "")
llm_model = os.environ.get("LLM_MODEL_NAME", "")
config_label = "[通用LLM]"
# 如果 .env 中没有模型名,则使用 config 作为备用
if not llm_model:
llm_model = config.get("llm_model", "gpt-4o-mini")
# 设置 camel-ai 所需的环境变量
if llm_api_key:
os.environ["OPENAI_API_KEY"] = llm_api_key
if not os.environ.get("OPENAI_API_KEY"):
raise ValueError("缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY")
if llm_base_url:
os.environ["OPENAI_API_BASE_URL"] = llm_base_url
print(f"{config_label} model={llm_model}, base_url={llm_base_url[:40] if llm_base_url else '默认'}...")
return ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=llm_model,
)
def get_active_agents_for_round(
env,
config: Dict[str, Any],
current_hour: int,
round_num: int
) -> List:View on GitHub (pinned to b5b53acc57)
Solutions
- Add LLM_API_KEY=... to the backend .env (project root) or export it before running.
- If OPENAI_API_KEY is already exported in the shell, that also passes the check — use it as a fallback.
- Verify with a preflight: python -c "import os;print(bool(os.environ.get('LLM_API_KEY') or os.environ.get('OPENAI_API_KEY')))".
Example fix
# before $ python scripts/run_parallel_simulation.py # no LLM_API_KEY # after # .env LLM_API_KEY=sk-... LLM_MODEL_NAME=gpt-4o-mini $ python scripts/run_parallel_simulation.py
Defensive patterns
Strategy: validation
Validate before calling
import os
def llm_env_ready() -> bool:
return bool(os.environ.get("LLM_API_KEY") or os.environ.get("OPENAI_API_KEY")) Prevention
- Ship a .env.example listing LLM_API_KEY, LLM_MODEL_NAME, LLM_BASE_URL so setup is discoverable.
- Add a preflight at script start that prints all required-but-missing env vars once.
- In CI/cron, source the environment explicitly or pass --env-file.
When it happens
Trigger: Running run_parallel_simulation.py with LLM_API_KEY unset or empty and no pre-exported OPENAI_API_KEY: missing .env, .env not loaded, wrong working directory, or LLM_API_KEY="".
Common situations: New clone without .env; cron/CI shell where .env is not sourced; key stored under a different variable name; .env present but script launched from another directory.
Related errors
- 缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY
- 缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY
- ZEP_API_KEY 未配置
- ZEP_API_KEY must be supplied through the process environment
- LLM_API_KEY 未配置
AI-assisted analysis of 666ghj/MiroFish@b5b53acc57 (2026-08-14).
Data as JSON: /api/errors/b2de96cf33bbd14d.
Report an issue: GitHub.