# openai.AuthenticationError: 提供的API密钥格式不正确：sk-... 您可以在https://platform.openai.com/account/api-keys找到您的API密钥。

- **ID:** `llm/invalid-api-key-format`
- **领域:** llm
- **类别:** auth_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

API密钥字符串格式错误、已过期，或属于与当前请求不同的组织/项目。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai>=1.0.0 | active | — | — |
| openai==0.28.0 | active | — | — |

## 解决方案

1. ```
   通过curl直接测试API密钥：
curl -H 'Authorization: Bearer YOUR_API_KEY' https://api.openai.com/v1/models
如果失败，则密钥无效。检查.env文件中是否有尾随换行符或空格。
   ```
2. ```
   确保从环境变量正确加载API密钥，不带引号：
import os
import openai
openai.api_key = os.getenv('OPENAI_API_KEY').strip()
print(repr(openai.api_key))  # 应显示'sk-...'，无多余空格
   ```

## 无效尝试

- **** — The root cause is often a copy-paste error or whitespace, not the key itself. (60% 失败率)
- **** — The key may be revoked by the provider after exposure, or the code leaks secrets. (80% 失败率)
