FoundationAgents/MetaGPT · critical · ValueError

Set the `access_key`&`secret_key` or `api_key`&`secret_key`

Error message

Set the `access_key`&`secret_key` or `api_key`&`secret_key` first

What it means

QianfanProvider.__init__ requires credentials in pairs: either access_key + secret_key (system-level auth) or api_key + secret_key (application-level). If neither pair is complete, it raises this ValueError before any API call; valid pairs are exported as QIANFAN_ACCESS_KEY/QIANFAN_SECRET_KEY or QIANFAN_AK/QIANFAN_SK environment variables.

Source

Thrown at metagpt/provider/qianfan_api.py:52

        self.config = config
        self.use_system_prompt = False  # only some ERNIE-x related models support system_prompt
        self.__init_qianfan()
        self.cost_manager = CostManager(token_costs=self.token_costs)

    def __init_qianfan(self):
        self.model = self.config.model
        if self.config.access_key and self.config.secret_key:
            # for system level auth, use access_key and secret_key, recommended by official
            # set environment variable due to official recommendation
            os.environ.setdefault("QIANFAN_ACCESS_KEY", self.config.access_key)
            os.environ.setdefault("QIANFAN_SECRET_KEY", self.config.secret_key)
        elif self.config.api_key and self.config.secret_key:
            # for application level auth, use api_key and secret_key
            # set environment variable due to official recommendation
            os.environ.setdefault("QIANFAN_AK", self.config.api_key)
            os.environ.setdefault("QIANFAN_SK", self.config.secret_key)
        else:
            raise ValueError("Set the `access_key`&`secret_key` or `api_key`&`secret_key` first")

        if self.config.base_url:
            os.environ.setdefault("QIANFAN_BASE_URL", self.config.base_url)

        support_system_pairs = [
            ("ERNIE-Bot-4", "completions_pro"),  # (model, corresponding-endpoint)
            ("ERNIE-Bot-8k", "ernie_bot_8k"),
            ("ERNIE-Bot", "completions"),
            ("ERNIE-Bot-turbo", "eb-instant"),
            ("ERNIE-Speed", "ernie_speed"),
            ("EB-turbo-AppBuilder", "ai_apaas"),
        ]
        if self.model in [pair[0] for pair in support_system_pairs]:
            # only some ERNIE models support
            self.use_system_prompt = True
        if self.config.endpoint in [pair[1] for pair in support_system_pairs]:
            self.use_system_prompt = True

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. In config2.yaml under the qianfan provider, set both api_key and secret_key to your application credentials.
  2. Or set access_key and secret_key for system-level auth (recommended by Baidu).
  3. Verify neither field is empty string (the truthiness check treats '' as missing).
  4. Confirm the config actually targets qianfan (api_type) so the right provider reads your keys.

Example fix

# before (config2.yaml)
llm:
  api_type: qianfan
  api_key: "bsp_xxx"      # secret_key missing -> ValueError

# after
llm:
  api_type: qianfan
  api_key: "bsp_xxx"
  secret_key: "your_secret"
Defensive patterns

Strategy: validation

Validate before calling

cfg = config.llm
has_system = bool(cfg.access_key and cfg.secret_key)
has_app = bool(cfg.api_key and cfg.secret_key)
assert has_system or has_app, (
    "qianfan needs access_key+secret_key or api_key+secret_key"
)

Try / catch

try:
    provider = QianfanProvider(config)
except ValueError as e:
    if "access_key" in str(e):
        raise SystemExit("Set qianfan api_key & secret_key (or access_key & secret_key) in config2.yaml") from e
    raise

Prevention

When it happens

Trigger: Instantiating the qianfan provider with only one of the two keys (e.g. api_key set but secret_key missing), both empty, or with an access_key/api_key mix that matches neither branch.

Common situations: config2.yaml with a copy-pasted single token, env-var-based setups where one variable is unset, or migrating from Baidu's older single-key scheme to the ak/sk pair scheme.

Related errors


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