datawhalechina/hello-agents · critical · RuntimeError

LLM_API_KEY 环境变量未设置

Error message

LLM_API_KEY 环境变量未设置

What it means

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.

Source

Thrown at Co-creation-projects/lcyting-StockSage-agent/agents/general_advisor_agent.py:119

    yield {"type": "status", "content": "投资顾问正在分析..."}

    try:
        result = agent.run(task)
        yield {"type": "delta", "content": result}
        yield {"type": "done"}
    except Exception as e:
        yield {"type": "error", "content": f"投资分析出错: {e}"}


def _create_default_llm() -> HelloAgentsLLM:
    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 环境变量未设置")

    return HelloAgentsLLM(
        model=model,
        api_key=api_key,
        base_url=base_url,
        provider=provider,
        temperature=0.35,
    )

View on GitHub (pinned to 606a07d341)

Solutions

  1. Set LLM_API_KEY in the process environment (export, .env + load_dotenv, or container env).
  2. Pass llm= explicitly when constructing the agent.
  3. When debugging generic 'Investment analysis error' events, log the exception fully server-side to expose this RuntimeError.
  4. Centralize the env preflight so all four agents fail with one clear startup message.

Example fix

# before
def analyze():
    agent = GeneralAdvisorAgent()  # RuntimeError hidden inside error event

# after
if not os.getenv("LLM_API_KEY"):
    raise SystemExit("LLM_API_KEY not set; refusing to start")
agent = GeneralAdvisorAgent()
Defensive patterns

Strategy: try-catch

Validate before calling

if not os.getenv("LLM_API_KEY"):
    raise SystemExit("LLM_API_KEY not set; the advisor cannot start")
agent = GeneralAdvisorAgent()

Type guard

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

Try / catch

# In the streaming handler, distinguish config errors from runtime errors
try:
    agent = GeneralAdvisorAgent()
except RuntimeError as e:
    yield {"type": "error", "content": f"configuration error: {e}"}  # surface real cause
    return
for ev in agent.analyze():
    yield ev

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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