{"record":{"id":"c3ac3df0d03f5c03","repo":"datawhalechina/hello-agents","slug":"llm-api-key-export-llm-api-key-y-c3ac3d","errorCode":null,"errorMessage":"LLM_API_KEY 环境变量未设置，请先设置环境变量：\nexport LLM_API_KEY=your_llm_api_key_here\n或在创建Agent时传入 llm 参数","messagePattern":"LLM_API_KEY 环境变量未设置，请先设置环境变量：\nexport LLM_API_KEY=your_llm_api_key_here\n或在创建Agent时传入 llm 参数","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"Co-creation-projects/lcyting-StockSage-agent/agents/sentiment_agent.py","lineNumber":125,"sourceCode":"        system_prompt=prompt,\n        config=Config(temperature=0.3, max_tokens=4096),  # 低温度确保分析稳定\n        max_steps=max_steps,\n    )\n\n    return agent\n\n\ndef _create_default_llm() -> HelloAgentsLLM:\n    \"\"\"从环境变量创建默认LLM实例\"\"\"\n    import os\n\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(\n            \"LLM_API_KEY 环境变量未设置，请先设置环境变量：\\n\"\n            \"export LLM_API_KEY=your_llm_api_key_here\\n\"\n            \"或在创建Agent时传入 llm 参数\"\n        )\n\n    return HelloAgentsLLM(\n        model=model,\n        api_key=api_key,\n        base_url=base_url,\n        provider=provider,\n        temperature=0.3,\n    )\n\n\ndef analyze_sentiment_stream(\n    agent: ReActAgent,\n    stock_code: str = \"\",\n    stock_name: str = \"\",","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/lcyting-StockSage-agent/agents/sentiment_agent.py#L107-L143","documentation":"sentiment_agent's default-LLM factory raises RuntimeError (with export instructions) when LLM_API_KEY is unset. Same pattern as data_analysis_agent: environment-driven construction with the API key as the only mandatory variable.","triggerScenarios":"Creating the sentiment agent without LLM_API_KEY exported and without an llm argument; batch pipelines that spawn worker processes without inheriting the parent env.","commonSituations":"Celery/multiprocessing workers lacking env; .env loaded in the web process but not in the worker; key present under a different name (e.g. OPENAI_API_KEY) while LLM_PROVIDER expects the generic LLM_API_KEY.","solutions":["export LLM_API_KEY=... or add it to .env and ensure it is loaded in every process that builds agents.","Inject llm= into SentimentAgent to bypass env resolution.","Propagate env to worker processes (e.g. Celery env= setting, compose environment:).","Fail fast at startup with an aggregated check of all LLM_* variables."],"exampleFix":"# before\nagent = SentimentAgent()  # RuntimeError\n\n# after\nassert os.getenv(\"LLM_API_KEY\"), \"LLM_API_KEY missing — source .env before starting workers\"\nagent = SentimentAgent()","handlingStrategy":"validation","validationCode":"if not os.getenv(\"LLM_API_KEY\"):\n    raise SystemExit(\"LLM_API_KEY not set — sentiment agent cannot start\")\nagent = SentimentAgent()","typeGuard":"def sentiment_llm_ready() -> bool:\n    return bool(os.getenv(\"LLM_API_KEY\"))","tryCatchPattern":"try:\n    agent = SentimentAgent()\nexcept RuntimeError as e:\n    if \"LLM_API_KEY\" in str(e):\n        agent = SentimentAgent(llm=shared_llm)\n    else:\n        raise","preventionTips":["Propagate env into worker processes (Celery env=, compose environment:).","Inject llm instances in workers instead of relying on inherited env.","Standardize env var names (LLM_API_KEY vs provider-specific) across the stack."],"tags":["env-vars","configuration","llm-client","python"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}