FoundationAgents/MetaGPT · critical · ValueError

Please set your API key in {repo_config_path}

Error message

Please set your API key in {repo_config_path}

What it means

Same api_key validator in LLMConfig, but this branch means: no root-level config2.yaml exists, and the repository-local METAGPT_ROOT/config/config2.yaml exists yet its api_key is still empty/placeholder. The value must be set in that repo file specifically.

Source

Thrown at metagpt/configs/llm_config.py:128

    use_system_prompt: bool = True

    # reasoning / thinking switch
    reasoning: bool = False
    reasoning_max_token: int = 4000  # reasoning budget tokens to generate, usually smaller than max_token

    @field_validator("api_key")
    @classmethod
    def check_llm_key(cls, v):
        if v in ["", None, "YOUR_API_KEY"]:
            repo_config_path = METAGPT_ROOT / "config/config2.yaml"
            root_config_path = CONFIG_ROOT / "config2.yaml"
            if root_config_path.exists():
                raise ValueError(
                    f"Please set your API key in {root_config_path}. If you also set your config in {repo_config_path}, \n"
                    f"the former will overwrite the latter. This may cause unexpected result.\n"
                )
            elif repo_config_path.exists():
                raise ValueError(f"Please set your API key in {repo_config_path}")
            else:
                raise ValueError("Please set your API key in config2.yaml")
        return v

    @field_validator("timeout")
    @classmethod
    def check_timeout(cls, v):
        return v or LLM_API_TIMEOUT

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Edit METAGPT_ROOT/config/config2.yaml and fill api_key for your provider.
  2. Or create a root-level config (~/.metagpt/config2.yaml) with a valid key, which then takes precedence.
  3. Or pass LLMConfig(api_key=...) explicitly in code / export the provider's env key and load config from env.

Example fix

# before (metagpt/config/config2.yaml)
openai:
  api_key: ''

# after
openai:
  api_key: 'sk-...'
Defensive patterns

Strategy: validation

Validate before calling

import yaml
cfgp = Path('metagpt/config/config2.yaml')
if cfgp.exists():
    data = yaml.safe_load(cfgp.read_text())
    key = (data.get('openai') or {}).get('api_key')
    assert key not in ('', None, 'YOUR_API_KEY'), 'repo config2.yaml needs a real api_key'

Prevention

When it happens

Trigger: Instantiating LLM or any LLM-dependent Action when only metagpt's repo config/config2.yaml is present (no ~/.metagpt config) and its api_key is '' / 'YOUR_API_KEY' / unset.

Common situations: Fresh clone of MetaGPT used in-place; CI environments that mount the repo but never write a user config; the template file copied unmodified.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/b73b8e011a0cfce9. Report an issue: GitHub.