{"record":{"id":"b2de96cf33bbd14d","repo":"666ghj/MiroFish","slug":"api-key-env-llm-api-key","errorCode":null,"errorMessage":"缺少 API Key 配置，请在项目根目录 .env 文件中设置 LLM_API_KEY","messagePattern":"缺少 API Key 配置，请在项目根目录 \\.env 文件中设置 LLM_API_KEY","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"backend/scripts/run_parallel_simulation.py","lineNumber":1027,"sourceCode":"        llm_model = boost_model or os.environ.get(\"LLM_MODEL_NAME\", \"\")\n        config_label = \"[加速LLM]\"\n    else:\n        # 使用通用配置\n        llm_api_key = os.environ.get(\"LLM_API_KEY\", \"\")\n        llm_base_url = os.environ.get(\"LLM_BASE_URL\", \"\")\n        llm_model = os.environ.get(\"LLM_MODEL_NAME\", \"\")\n        config_label = \"[通用LLM]\"\n    \n    # 如果 .env 中没有模型名，则使用 config 作为备用\n    if not llm_model:\n        llm_model = config.get(\"llm_model\", \"gpt-4o-mini\")\n    \n    # 设置 camel-ai 所需的环境变量\n    if llm_api_key:\n        os.environ[\"OPENAI_API_KEY\"] = llm_api_key\n    \n    if not os.environ.get(\"OPENAI_API_KEY\"):\n        raise ValueError(\"缺少 API Key 配置，请在项目根目录 .env 文件中设置 LLM_API_KEY\")\n    \n    if llm_base_url:\n        os.environ[\"OPENAI_API_BASE_URL\"] = llm_base_url\n    \n    print(f\"{config_label} model={llm_model}, base_url={llm_base_url[:40] if llm_base_url else '默认'}...\")\n    \n    return ModelFactory.create(\n        model_platform=ModelPlatformType.OPENAI,\n        model_type=llm_model,\n    )\n\n\ndef get_active_agents_for_round(\n    env,\n    config: Dict[str, Any],\n    current_hour: int,\n    round_num: int\n) -> List:","sourceCodeStart":1009,"sourceCodeEnd":1045,"githubUrl":"https://github.com/666ghj/MiroFish/blob/b5b53acc57189a4a42e44a23e149dc655c98fe82/backend/scripts/run_parallel_simulation.py#L1009-L1045","documentation":"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.","triggerScenarios":"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=\"\".","commonSituations":"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.","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')))\"."],"exampleFix":"# before\n$ python scripts/run_parallel_simulation.py  # no LLM_API_KEY\n\n# after\n# .env\nLLM_API_KEY=sk-...\nLLM_MODEL_NAME=gpt-4o-mini\n$ python scripts/run_parallel_simulation.py","handlingStrategy":"validation","validationCode":"import os\n\ndef llm_env_ready() -> bool:\n    return bool(os.environ.get(\"LLM_API_KEY\") or os.environ.get(\"OPENAI_API_KEY\"))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["configuration","authentication","environment","llm","missing-api-key","camel-ai"],"backgroundTag":null,"analyzedSha":"b5b53acc57189a4a42e44a23e149dc655c98fe82","analyzedAt":"2026-08-14T22:29:33.146Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}