{"record":{"id":"199eb7a383edf87f","repo":"666ghj/MiroFish","slug":"llm-api-key-199eb7","errorCode":null,"errorMessage":"LLM_API_KEY 未配置","messagePattern":"LLM_API_KEY 未配置","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"backend/app/services/simulation_config_generator.py","lineNumber":237,"sourceCode":"    # 各步骤的上下文截断长度（字符数）\n    TIME_CONFIG_CONTEXT_LENGTH = 10000   # 时间配置\n    EVENT_CONFIG_CONTEXT_LENGTH = 8000   # 事件配置\n    ENTITY_SUMMARY_LENGTH = 300          # 实体摘要\n    AGENT_SUMMARY_LENGTH = 300           # Agent配置中的实体摘要\n    ENTITIES_PER_TYPE_DISPLAY = 20       # 每类实体显示数量\n    \n    def __init__(\n        self,\n        api_key: Optional[str] = None,\n        base_url: Optional[str] = None,\n        model_name: Optional[str] = None\n    ):\n        self.api_key = api_key or Config.LLM_API_KEY\n        self.base_url = base_url or Config.LLM_BASE_URL\n        self.model_name = model_name or Config.LLM_MODEL_NAME\n        \n        if not self.api_key:\n            raise ValueError(\"LLM_API_KEY 未配置\")\n        \n        self.client = OpenAI(\n            api_key=self.api_key,\n            base_url=self.base_url\n        )\n    \n    def generate_config(\n        self,\n        simulation_id: str,\n        project_id: str,\n        graph_id: str,\n        simulation_requirement: str,\n        document_text: str,\n        entities: List[EntityNode],\n        enable_twitter: bool = True,\n        enable_reddit: bool = True,\n        progress_callback: Optional[Callable[[int, int, str], None]] = None,\n    ) -> SimulationParameters:","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/666ghj/MiroFish/blob/b5b53acc57189a4a42e44a23e149dc655c98fe82/backend/app/services/simulation_config_generator.py#L219-L255","documentation":"Raised in SimulationConfigGenerator.__init__: identical bootstrap check to the profile generator — the optional api_key argument falls back to Config.LLM_API_KEY, and if neither is present the constructor raises ValueError('LLM_API_KEY 未配置') before building the OpenAI client used to generate simulation configs.","triggerScenarios":"Constructing SimulationConfigGenerator (typically inside SimulationManager.prepare_simulation's config-generation stage) with LLM_API_KEY unset/empty in the environment and no explicit api_key argument.","commonSituations":"Same env issues as error 28: fresh deployment, .env not loaded in the container, variable typo, or secrets not ported to a new environment. Often surfaces only when a user first triggers /prepare with LLM config generation enabled.","solutions":["Set LLM_API_KEY in the backend environment (.env / docker-compose) and restart.","Or pass api_key explicitly to the SimulationConfigGenerator constructor / config-generation entrypoint.","Double-check Config loading order — the .env must be loaded before Config class attributes are evaluated.","Add a startup env check so misconfiguration is caught at boot, not mid-simulation."],"exampleFix":"# before\ngen = SimulationConfigGenerator()  # ValueError\n# after\ngen = SimulationConfigGenerator(api_key=os.environ['LLM_API_KEY'])","handlingStrategy":"validation","validationCode":"assert Config.LLM_API_KEY, 'LLM_API_KEY missing: set it in .env before starting the backend'","typeGuard":null,"tryCatchPattern":"try:\n    gen = SimulationConfigGenerator()\nexcept ValueError as e:\n    if 'LLM_API_KEY' in str(e):\n        raise SystemExit('Configure LLM_API_KEY in the environment and restart') from e\n    raise","preventionTips":["Validate all LLM env vars in one startup check shared by every generator service.","Ensure .env is loaded before Config is imported (check import order in the app entrypoint).","Surface configuration status on a /health or /config endpoint for quick diagnosis."],"tags":["config","llm","api-key","openai","environment"],"backgroundTag":null,"analyzedSha":"b5b53acc57189a4a42e44a23e149dc655c98fe82","analyzedAt":"2026-08-14T22:29:33.146Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}