datawhalechina/hello-agents · critical · RuntimeError
LLM_API_KEY 环境变量未设置,请先设置环境变量: export LLM_API_KEY=your_llm_ap
Error message
LLM_API_KEY 环境变量未设置,请先设置环境变量: export LLM_API_KEY=your_llm_api_key_here 或在创建Agent时传入 llm 参数
What it means
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.
Source
Thrown at Co-creation-projects/lcyting-StockSage-agent/agents/sentiment_agent.py:125
system_prompt=prompt,
config=Config(temperature=0.3, max_tokens=4096), # 低温度确保分析稳定
max_steps=max_steps,
)
return agent
def _create_default_llm() -> HelloAgentsLLM:
"""从环境变量创建默认LLM实例"""
import os
model = os.getenv("LLM_MODEL_ID")
api_key = os.getenv("LLM_API_KEY")
base_url = os.getenv("LLM_BASE_URL")
provider = os.getenv("LLM_PROVIDER", "auto")
if not api_key:
raise RuntimeError(
"LLM_API_KEY 环境变量未设置,请先设置环境变量:\n"
"export LLM_API_KEY=your_llm_api_key_here\n"
"或在创建Agent时传入 llm 参数"
)
return HelloAgentsLLM(
model=model,
api_key=api_key,
base_url=base_url,
provider=provider,
temperature=0.3,
)
def analyze_sentiment_stream(
agent: ReActAgent,
stock_code: str = "",
stock_name: str = "",View on GitHub (pinned to 606a07d341)
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.
Example fix
# before
agent = SentimentAgent() # RuntimeError
# after
assert os.getenv("LLM_API_KEY"), "LLM_API_KEY missing — source .env before starting workers"
agent = SentimentAgent() Defensive patterns
Strategy: validation
Validate before calling
if not os.getenv("LLM_API_KEY"):
raise SystemExit("LLM_API_KEY not set — sentiment agent cannot start")
agent = SentimentAgent() Type guard
def sentiment_llm_ready() -> bool:
return bool(os.getenv("LLM_API_KEY")) Try / catch
try:
agent = SentimentAgent()
except RuntimeError as e:
if "LLM_API_KEY" in str(e):
agent = SentimentAgent(llm=shared_llm)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- LLM client is not configured. Check .env.
- API密钥和服务地址必须被提供或在.env文件中定义。
- LLM_API_KEY 环境变量未设置
- LLM_API_KEY 环境变量未设置,请先设置环境变量: export LLM_API_KEY=your_llm_ap
- 未配置 AMiner API Key。请前往 https://open.aminer.cn/ 注册获取,然后在 .env
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/c3ac3df0d03f5c03.
Report an issue: GitHub.