{"record":{"id":"61b1b2bee68696d8","repo":"666ghj/MiroFish","slug":"api-key-env-llm-api-key-61b1b2","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_twitter_simulation.py","lineNumber":450,"sourceCode":"        - LLM_API_KEY: API密钥\n        - LLM_BASE_URL: API基础URL\n        - LLM_MODEL_NAME: 模型名称\n        \"\"\"\n        # 优先从 .env 读取配置\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        \n        # 如果 .env 中没有，则使用 config 作为备用\n        if not llm_model:\n            llm_model = self.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\"LLM配置: 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    def _get_active_agents_for_round(\n        self, \n        env, \n        current_hour: int,\n        round_num: int\n    ) -> List:\n        \"\"\"","sourceCodeStart":432,"sourceCodeEnd":468,"githubUrl":"https://github.com/666ghj/MiroFish/blob/b5b53acc57189a4a42e44a23e149dc655c98fe82/backend/scripts/run_twitter_simulation.py#L432-L468","documentation":"Same missing-key guard in backend/scripts/run_twitter_simulation.py: LLM_API_KEY is copied into OPENAI_API_KEY for camel-ai, and the script raises this ValueError when OPENAI_API_KEY is still absent afterwards. It fails at model-factory setup time, before any LLM call.","triggerScenarios":"Launching run_twitter_simulation.py with both LLM_API_KEY and OPENAI_API_KEY unset or empty in the process environment.","commonSituations":"Docker container run without --env-file; CI job missing the secret; developer renaming the key variable in .env so the lookup misses.","solutions":["Provide LLM_API_KEY in .env or the environment before starting the Twitter simulation.","For Docker, pass --env-file .env (or define the variable in compose environment:).","Exporting OPENAI_API_KEY directly also passes the check."],"exampleFix":"# before\n$ docker run mirofish-twitter-sim  # env not passed -> ValueError\n\n# after\n$ docker run --env-file .env mirofish-twitter-sim","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":["Pass the environment into containers explicitly (--env-file / compose environment:).","Standardize on LLM_API_KEY across all three simulation runners to avoid per-script drift.","Add a startup banner printing which LLM config was resolved."],"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-15T17:31:12.345Z"}