{"record":{"id":"fb92959006c66d21","repo":"FoundationAgents/MetaGPT","slug":"set-the-access-key-secret-key-or-api-key-se","errorCode":null,"errorMessage":"Set the `access_key`&`secret_key` or `api_key`&`secret_key` first","messagePattern":"Set the `access_key`&`secret_key` or `api_key`&`secret_key` first","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"metagpt/provider/qianfan_api.py","lineNumber":52,"sourceCode":"        self.config = config\n        self.use_system_prompt = False  # only some ERNIE-x related models support system_prompt\n        self.__init_qianfan()\n        self.cost_manager = CostManager(token_costs=self.token_costs)\n\n    def __init_qianfan(self):\n        self.model = self.config.model\n        if self.config.access_key and self.config.secret_key:\n            # for system level auth, use access_key and secret_key, recommended by official\n            # set environment variable due to official recommendation\n            os.environ.setdefault(\"QIANFAN_ACCESS_KEY\", self.config.access_key)\n            os.environ.setdefault(\"QIANFAN_SECRET_KEY\", self.config.secret_key)\n        elif self.config.api_key and self.config.secret_key:\n            # for application level auth, use api_key and secret_key\n            # set environment variable due to official recommendation\n            os.environ.setdefault(\"QIANFAN_AK\", self.config.api_key)\n            os.environ.setdefault(\"QIANFAN_SK\", self.config.secret_key)\n        else:\n            raise ValueError(\"Set the `access_key`&`secret_key` or `api_key`&`secret_key` first\")\n\n        if self.config.base_url:\n            os.environ.setdefault(\"QIANFAN_BASE_URL\", self.config.base_url)\n\n        support_system_pairs = [\n            (\"ERNIE-Bot-4\", \"completions_pro\"),  # (model, corresponding-endpoint)\n            (\"ERNIE-Bot-8k\", \"ernie_bot_8k\"),\n            (\"ERNIE-Bot\", \"completions\"),\n            (\"ERNIE-Bot-turbo\", \"eb-instant\"),\n            (\"ERNIE-Speed\", \"ernie_speed\"),\n            (\"EB-turbo-AppBuilder\", \"ai_apaas\"),\n        ]\n        if self.model in [pair[0] for pair in support_system_pairs]:\n            # only some ERNIE models support\n            self.use_system_prompt = True\n        if self.config.endpoint in [pair[1] for pair in support_system_pairs]:\n            self.use_system_prompt = True\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/provider/qianfan_api.py#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["In config2.yaml under the qianfan provider, set both api_key and secret_key to your application credentials.","Or set access_key and secret_key for system-level auth (recommended by Baidu).","Verify neither field is empty string (the truthiness check treats '' as missing).","Confirm the config actually targets qianfan (api_type) so the right provider reads your keys."],"exampleFix":"# before (config2.yaml)\nllm:\n  api_type: qianfan\n  api_key: \"bsp_xxx\"      # secret_key missing -> ValueError\n\n# after\nllm:\n  api_type: qianfan\n  api_key: \"bsp_xxx\"\n  secret_key: \"your_secret\"","handlingStrategy":"validation","validationCode":"cfg = config.llm\nhas_system = bool(cfg.access_key and cfg.secret_key)\nhas_app = bool(cfg.api_key and cfg.secret_key)\nassert has_system or has_app, (\n    \"qianfan needs access_key+secret_key or api_key+secret_key\"\n)","typeGuard":null,"tryCatchPattern":"try:\n    provider = QianfanProvider(config)\nexcept ValueError as e:\n    if \"access_key\" in str(e):\n        raise SystemExit(\"Set qianfan api_key & secret_key (or access_key & secret_key) in config2.yaml\") from e\n    raise","preventionTips":["Configure credential pairs together, never singly","Fail fast on startup with an explicit credential check","Treat empty strings as missing — the check is truthiness-based"],"tags":["qianfan","credentials","configuration","authentication"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}