666ghj/MiroFish · critical · ValueError

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

Error message

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

What it means

Same missing-key guard in backend/scripts/run_twitter_simulation.py: LLM_API_KEY is copied into OPENAI_API_KEY for camel-ai, and the script raises this ValueError when OPENAI_API_KEY is still absent afterwards. It fails at model-factory setup time, before any LLM call.

Source

Thrown at backend/scripts/run_twitter_simulation.py:450

        - 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. Provide LLM_API_KEY in .env or the environment before starting the Twitter simulation.
  2. For Docker, pass --env-file .env (or define the variable in compose environment:).
  3. Exporting OPENAI_API_KEY directly also passes the check.

Example fix

# before
$ docker run mirofish-twitter-sim  # env not passed -> ValueError

# after
$ docker run --env-file .env mirofish-twitter-sim
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_twitter_simulation.py with both LLM_API_KEY and OPENAI_API_KEY unset or empty in the process environment.

Common situations: Docker container run without --env-file; CI job missing the secret; developer renaming the key variable in .env so the lookup misses.

Related errors


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