{"record":{"id":"9766e671cdcbc4e3","repo":"datawhalechina/hello-agents","slug":"llm-api-key-9766e6","errorCode":null,"errorMessage":"LLM_API_KEY 环境变量未设置","messagePattern":"LLM_API_KEY 环境变量未设置","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"Co-creation-projects/lcyting-StockSage-agent/agents/general_advisor_agent.py","lineNumber":119,"sourceCode":"\n    yield {\"type\": \"status\", \"content\": \"投资顾问正在分析...\"}\n\n    try:\n        result = agent.run(task)\n        yield {\"type\": \"delta\", \"content\": result}\n        yield {\"type\": \"done\"}\n    except Exception as e:\n        yield {\"type\": \"error\", \"content\": f\"投资分析出错: {e}\"}\n\n\ndef _create_default_llm() -> HelloAgentsLLM:\n    model = os.getenv(\"LLM_MODEL_ID\")\n    api_key = os.getenv(\"LLM_API_KEY\")\n    base_url = os.getenv(\"LLM_BASE_URL\")\n    provider = os.getenv(\"LLM_PROVIDER\", \"auto\")\n\n    if not api_key:\n        raise RuntimeError(\"LLM_API_KEY 环境变量未设置\")\n\n    return HelloAgentsLLM(\n        model=model,\n        api_key=api_key,\n        base_url=base_url,\n        provider=provider,\n        temperature=0.35,\n    )\n","sourceCodeStart":101,"sourceCodeEnd":128,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/lcyting-StockSage-agent/agents/general_advisor_agent.py#L101-L128","documentation":"general_advisor_agent's default-LLM factory raises RuntimeError when LLM_API_KEY is missing, identical guard to the sibling agents. Note this agent's streaming generator wraps exceptions as {\"type\": \"error\"} events instead of raising, so a missing key here can also surface as an error event if construction is deferred.","triggerScenarios":"Instantiating the general advisor without LLM_API_KEY and without llm=; or triggering its streaming analyze path where the constructor error is caught and yielded as an error event (\"Investment analysis error: ...\"), obscuring the root cause.","commonSituations":"Frontend showing a generic analysis error whose underlying cause is the missing key; env differences between the process serving requests and the developer's shell.","solutions":["Set LLM_API_KEY in the process environment (export, .env + load_dotenv, or container env).","Pass llm= explicitly when constructing the agent.","When debugging generic 'Investment analysis error' events, log the exception fully server-side to expose this RuntimeError.","Centralize the env preflight so all four agents fail with one clear startup message."],"exampleFix":"# before\ndef analyze():\n    agent = GeneralAdvisorAgent()  # RuntimeError hidden inside error event\n\n# after\nif not os.getenv(\"LLM_API_KEY\"):\n    raise SystemExit(\"LLM_API_KEY not set; refusing to start\")\nagent = GeneralAdvisorAgent()","handlingStrategy":"try-catch","validationCode":"if not os.getenv(\"LLM_API_KEY\"):\n    raise SystemExit(\"LLM_API_KEY not set; the advisor cannot start\")\nagent = GeneralAdvisorAgent()","typeGuard":"def general_advisor_ready() -> bool:\n    return bool(os.getenv(\"LLM_API_KEY\"))","tryCatchPattern":"# In the streaming handler, distinguish config errors from runtime errors\ntry:\n    agent = GeneralAdvisorAgent()\nexcept RuntimeError as e:\n    yield {\"type\": \"error\", \"content\": f\"configuration error: {e}\"}  # surface real cause\n    return\nfor ev in agent.analyze():\n    yield ev","preventionTips":["Log full exceptions behind generic error events so config failures are diagnosable.","Construct agents at request start and fail fast on missing env.","Share one env preflight across all StockSage agents."],"tags":["env-vars","configuration","streaming","python"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}