666ghj/MiroFish · critical · ValueError

缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY

Error message

缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY

What it means

Identical guard in backend/scripts/run_reddit_simulation.py: the Reddit simulation copies LLM_API_KEY into OPENAI_API_KEY for camel-ai's OpenAI backend and raises this ValueError when os.environ.get("OPENAI_API_KEY") is still falsy after the copy. It fires before ModelFactory.create, so no simulation round starts without credentials.

Source

Thrown at backend/scripts/run_reddit_simulation.py:457

        - LLM_API_KEY: API密钥
        - LLM_BASE_URL: API基础URL
        - LLM_MODEL_NAME: 模型名称
        """
        # 优先从 .env 读取配置
        llm_api_key = os.environ.get("LLM_API_KEY", "")
        llm_base_url = os.environ.get("LLM_BASE_URL", "")
        llm_model = os.environ.get("LLM_MODEL_NAME", "")
        
        # 如果 .env 中没有,则使用 config 作为备用
        if not llm_model:
            llm_model = self.config.get("llm_model", "gpt-4o-mini")
        
        # 设置 camel-ai 所需的环境变量
        if llm_api_key:
            os.environ["OPENAI_API_KEY"] = llm_api_key
        
        if not os.environ.get("OPENAI_API_KEY"):
            raise ValueError("缺少 API Key 配置,请在项目根目录 .env 文件中设置 LLM_API_KEY")
        
        if llm_base_url:
            os.environ["OPENAI_API_BASE_URL"] = llm_base_url
        
        print(f"LLM配置: model={llm_model}, base_url={llm_base_url[:40] if llm_base_url else '默认'}...")
        
        return ModelFactory.create(
            model_platform=ModelPlatformType.OPENAI,
            model_type=llm_model,
        )
    
    def _get_active_agents_for_round(
        self, 
        env, 
        current_hour: int,
        round_num: int
    ) -> List:
        """

View on GitHub (pinned to b5b53acc57)

Solutions

  1. Set LLM_API_KEY in .env or the process environment before launching the Reddit simulation.
  2. Alternatively export OPENAI_API_KEY directly — the check reads that variable.
  3. Preflight-print missing LLM variables instead of discovering them mid-run.

Example fix

# before
$ python scripts/run_reddit_simulation.py

# after
export LLM_API_KEY=sk-...
$ python scripts/run_reddit_simulation.py
Defensive patterns

Strategy: validation

Validate before calling

import os

def llm_env_ready() -> bool:
    return bool(os.environ.get("LLM_API_KEY") or os.environ.get("OPENAI_API_KEY"))

Prevention

When it happens

Trigger: Launching run_reddit_simulation.py in a process where LLM_API_KEY is unset/empty and OPENAI_API_KEY was never exported.

Common situations: Missing or misnamed .env entries; running under systemd/cron where the environment is stripped; a copied config that sets LLM_BASE_URL and LLM_MODEL_NAME but forgets the key.

Related errors


AI-assisted analysis of 666ghj/MiroFish@b5b53acc57 (2026-08-14). Data as JSON: /api/errors/87b5c574de9ce927. Report an issue: GitHub.