llm
auth_error
ai_generated
true
openai.AuthenticationError: 提供的API密钥格式不正确:sk-... 您可以在https://platform.openai.com/account/api-keys找到您的API密钥。
openai.AuthenticationError: Incorrect API key provided: sk-... You can find your API key at https://platform.openai.com/account/api-keys.
ID: llm/invalid-api-key-format
95%修复率
90%置信度
1证据数
2023-03-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| openai>=1.0.0 | active | — | — | — |
| openai==0.28.0 | active | — | — | — |
根因分析
API密钥字符串格式错误、已过期,或属于与当前请求不同的组织/项目。
English
The API key string is malformed, expired, or belongs to a different organization/project than the one being used for the request.
官方文档
https://platform.openai.com/docs/guides/error-codes/api-errors解决方案
-
通过curl直接测试API密钥: curl -H 'Authorization: Bearer YOUR_API_KEY' https://api.openai.com/v1/models 如果失败,则密钥无效。检查.env文件中是否有尾随换行符或空格。
-
确保从环境变量正确加载API密钥,不带引号: import os import openai openai.api_key = os.getenv('OPENAI_API_KEY').strip() print(repr(openai.api_key)) # 应显示'sk-...',无多余空格
无效尝试
常见但无效的做法:
-
60% 失败
The root cause is often a copy-paste error or whitespace, not the key itself.
-
80% 失败
The key may be revoked by the provider after exposure, or the code leaks secrets.