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

data_analysis_agent's default-LLM factory raises RuntimeError with explicit remediation text when LLM_API_KEY is unset. The message offers the two supported fixes: export the variable or pass an llm argument at agent construction.

Source

Thrown at Co-creation-projects/lcyting-StockSage-agent/agents/data_analysis_agent.py:123

        system_prompt=prompt,
        config=Config(temperature=0.2, 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.2,
    )


def analyze_data_stream(
    agent: ReActAgent,
    stock_code: str = "",
    stock_name: str = "",

View on GitHub (pinned to 606a07d341)

Solutions

  1. export LLM_API_KEY=your_key (or add to .env and load it) before launching.
  2. Construct HelloAgentsLLM yourself and pass llm=... to the agent.
  3. In docker-compose, add LLM_API_KEY to environment: or env_file:.
  4. Add a startup assertion for LLM_API_KEY to fail fast with one aggregated message.

Example fix

# before
agent = DataAnalysisAgent()  # RuntimeError with export instructions

# after
llm = HelloAgentsLLM(model=os.getenv("LLM_MODEL_ID"), api_key=os.getenv("LLM_API_KEY"), base_url=os.getenv("LLM_BASE_URL"))
agent = DataAnalysisAgent(llm=llm)
Defensive patterns

Strategy: validation

Validate before calling

import os
if not os.getenv("LLM_API_KEY"):
    raise SystemExit("LLM_API_KEY not set; export it or pass llm= to DataAnalysisAgent")
agent = DataAnalysisAgent()

Type guard

def can_build_default_llm() -> bool:
    return bool(os.getenv("LLM_API_KEY"))

Try / catch

try:
    agent = DataAnalysisAgent()
except RuntimeError as e:
    if "LLM_API_KEY" in str(e):
        agent = DataAnalysisAgent(llm=preconfigured_llm)
    else:
        raise

Prevention

When it happens

Trigger: Constructing the data-analysis agent with no LLM_API_KEY in the environment and no llm= argument; env vars present in the dev shell but not in the deployed process.

Common situations: Notebook/server restarts losing exported vars; containerized deploys missing env plumbing; .env file present but never loaded because python-dotenv isn't invoked in this code path.

Related errors


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