Skip to content

fix(fallback): preserve provider-specific headers when activating fallback - #6076

Closed
SeeYangZhi wants to merge 3 commits into
NousResearch:mainfrom
SeeYangZhi:fix/fallback-provider-header-loss
Closed

fix(fallback): preserve provider-specific headers when activating fallback#6076
SeeYangZhi wants to merge 3 commits into
NousResearch:mainfrom
SeeYangZhi:fix/fallback-provider-header-loss

Conversation

@SeeYangZhi

@SeeYangZhi SeeYangZhi commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #6075

When _try_activate_fallback() swaps to a provider that requires provider-specific HTTP headers (e.g. kimi-coding requires User-Agent: KimiCLI/1.x), the returned client from resolve_provider_client() correctly has the headers baked in. However, _client_kwargs was saved with only api_key and base_url, dropping default_headers.

Every subsequent API call rebuilds the client from _client_kwargs via _create_request_openai_client(), producing a bare OpenAI client without the required headers — causing 403s from Kimi Coding and potentially missing auth headers for GitHub Copilot.

Changes

# In _try_activate_fallback(), after self.client = fb_client:
fb_headers = getattr(fb_client, "_custom_headers", None)
if not fb_headers:
    fb_headers = getattr(fb_client, "default_headers", None)
self._client_kwargs = {
    "api_key": fb_client.api_key,
    "base_url": fb_base_url,
    **({"default_headers": dict(fb_headers)} if fb_headers else {}),
}

The OpenAI SDK stores default_headers passed to the constructor in client._custom_headers, so this correctly round-trips the headers through any client rebuild.

Verification

  • Synthetic round-trip: OpenAI client created with KimiCLI headers → read from _custom_headers → passed to new client → headers preserved
  • E2E: AIAgent with bogus primary key + kimi-coding fallback. Primary 401s → fallback activates → 3 consecutive calls all succeed with KimiCLI/1.3 in _client_kwargs. Without the fix, turns 2+ fail with 403.

Related

…sync clients

When  is explicitly set to ,
the custom-endpoint path in  creates a plain
client without provider-specific headers. This means sync vision calls (e.g.
) use the generic  User-Agent and get rejected by
Kimi's coding endpoint with a 403:

    'Kimi For Coding is currently only available for Coding Agents such as Kimi CLI...'

The async converter  already injects , and the
auto-detected API-key provider path also injects it, but the explicit custom
endpoint shortcut was missing it entirely.

This patch adds the same  injection to the custom endpoint
branch, and updates all existing Kimi header sites to  for
consistency.

Fixes <issue number to be filled in>
…ding pool

The credential pool seeder (_seed_from_env) hardcoded the base URL
for API-key providers without running provider-specific auto-detection.
For kimi-coding, this caused sk-kimi- prefixed keys to be seeded with
the legacy api.moonshot.ai/v1 endpoint instead of api.kimi.com/coding/v1,
resulting in HTTP 401 on the first request.

Import and call _resolve_kimi_base_url for kimi-coding so the pool
uses the correct endpoint based on the key prefix, matching the
runtime credential resolver behavior.

Also fix a comment: sk-kimi- keys are issued by kimi.com/code,
not platform.kimi.ai.

Fixes NousResearch#5561
…lback

When _try_activate_fallback() swaps to a new provider (e.g.
kimi-coding), resolve_provider_client() correctly injects
provider-specific default_headers (like KimiCLI User-Agent) into the
returned OpenAI client. However, _client_kwargs was saved with only
api_key and base_url, dropping those headers.

Every subsequent API call rebuilds the client from _client_kwargs via
_create_request_openai_client(), producing a bare OpenAI client without
the required headers. Kimi Coding rejects this with 403; Copilot would
lose its auth headers similarly.

This patch reads _custom_headers from the fallback client (where the
OpenAI SDK stores the default_headers kwarg) and includes them in
_client_kwargs so any client rebuild preserves provider-specific headers.

Fixes NousResearch#6075
@teknium1

teknium1 commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Merged via salvage PR #6754. All three commits cherry-picked onto current main with your authorship preserved in git log. Thank you @SeeYangZhi!

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.

[Bug]: Provider-specific headers (e.g. KimiCLI User-Agent) lost after fallback activation

2 participants