Skip to content

fix(model_fetcher): extract first key from multi-line api_key for HTTP requests#593

Open
haojiajieyangde wants to merge 1 commit into
iOfficeAI:mainfrom
haojiajieyangde:fix/multi-key-502
Open

fix(model_fetcher): extract first key from multi-line api_key for HTTP requests#593
haojiajieyangde wants to merge 1 commit into
iOfficeAI:mainfrom
haojiajieyangde:fix/multi-key-502

Conversation

@haojiajieyangde

Copy link
Copy Markdown

问题

api_key 包含多个 key(官方多 key 轮询格式 key1\nkey2)时,POST /api/providers/fetch-modelsPOST /api/providers/{id}/models 返回 502。

根因

FetchConfig.api_key 从请求或数据库解密后直接传入 HTTP Authorization header。多行 key 包含 \n,导致 reqwest HTTP 客户端拒绝设置 header,返回 "Invalid authorization header" 错误(0ms 延迟,无网络请求)。

修复

添加 extract_first_key() 辅助函数,按 \n 分割只取第一个 key。在 fetch_models_anonymousload_provider_config 两处调用。

为什么只取第一个 key?

  • Model fetching 只需要一个能用的 key 来获取模型列表
  • 多 key 轮询由前端 RotatingApiClient / ApiKeyManager 在 chat 请求层处理(环境变量级轮换)
  • detect-protocol 端点已有 test_all_keys: true 用于多 key 验证

测试

新增 7 个测试,全部通过。

When api_key contains multiple keys separated by newlines (the official
multi-key rotation format: 'key1\nkey2'), the model_fetcher endpoints
(fetch-models and {id}/models) would pass the full multi-line string as
an Authorization header value. HTTP clients (reqwest) reject header
values containing newlines, causing a 502 BadGateway.

Fix: add extract_first_key() helper that splits by newline and returns
only the first key. Apply it in both fetch_models_anonymous and
load_provider_config so model fetching only uses a single key.

The frontend's RotatingApiClient handles multi-key rotation for chat
requests independently via environment variables — model fetching only
needs one working key.

Fixes: POST /api/providers/fetch-models 502 with multi-key api_key
Fixes: POST /api/providers/{id}/models 502 with multi-key api_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant