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

  1. export LLM_API_KEY=... or add it to .env and ensure it is loaded in every process that builds agents.
  2. Inject llm= into SentimentAgent to bypass env resolution.
  3. Propagate env to worker processes (e.g. Celery env= setting, compose environment:).
  4. 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

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


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/c3ac3df0d03f5c03. Report an issue: GitHub.