Repository navigation
Description
Summary
OpenCode's bundled @ai-sdk/openai implementation fails to forward constructor-level headers to HTTP requests when using the OpenAI Responses API, causing 401 "missing" errors with Azure API Management (APIM) and other services that require custom authentication headers.
Problem Details
Text-level substitution works correctly - environment variables like {env:AZURE_APIM_KEY} are properly resolved in configuration.
However, when using GPT-5+ models (which trigger shouldUseCopilotResponsesApi), the headers from createOpenAI({ headers: {...} }) constructor are not included in the actual HTTP requests to the /responses endpoint.
Evidence
- Azure APIM reports "missing" (not "invalid") - indicates header never arrives
- Text substitution confirmed working -
{env:AZURE_APIM_KEY}resolves to real key - Only affects Responses API - Chat Completions API works fine with same config
- Model-specific routing - GPT-4 works, GPT-5+ fails (due to API switching)
Root Cause Analysis
OpenCode has two different @ai-sdk/openai implementations:
- Official @ai-sdk/openai (line 116 in
BUNDLED_PROVIDERS) - works correctly - Custom GitHub Copilot implementation (line 131) - used for GPT-5+ models via Responses API
The issue is in the custom implementation path:
createOpenAI({ headers }) →
shouldUseCopilotResponsesApi(modelID) →
createGitHubCopilotOpenAICompatible() →
OpenAIResponsesLanguageModel
The headers from the original createOpenAI({ headers }) constructor don't make it through this chain to the final HTTP requests.
Affected Code Locations
packages/opencode/src/provider/provider.ts:55-shouldUseCopilotResponsesApirouting logicpackages/opencode/src/provider/provider.ts:190-199- GitHub Copilot custom loaderpackages/opencode/src/provider/sdk/copilot/copilot-provider.ts:78-83- Custom provider creationpackages/opencode/src/provider/sdk/copilot/responses/openai-responses-language-model.ts:409,782- Header forwarding logic (correct but receives no headers)
Reproduction
Working Configuration (GPT-4)
{
"provider": {
"openai": {
"options": {
"apiKey": "{env:OPENAI_API_KEY}",
"baseURL": "https://my-apim.azure-api.net/openai",
"headers": {
"Ocp-Apim-Subscription-Key": "{env:AZURE_APIM_KEY}"
}
}
}
}
}Broken Configuration (GPT-5+)
Same config, different model triggers custom Responses API implementation and headers are lost.
Expected Behavior
Headers from createOpenAI({ headers }) constructor should be forwarded to all HTTP requests regardless of whether the model uses Chat Completions API or Responses API.
Actual Behavior
- GPT-4 (Chat API): Headers forwarded ✅
- GPT-5+ (Responses API): Headers missing ❌
Impact
- Azure APIM integration broken for GPT-5+ models
- Enterprise proxies requiring custom auth headers fail
- Inconsistent behavior between model families
- Silent failures - no error indicating headers were dropped
Workaround Approaches Tried
- Provider-level headers (
options.headers) - ❌ Not forwarded - Model-level headers (
models.{model}.headers) - ❌ Not forwarded - Custom User-Agent - ❌ Not forwarded
Related Issues
- Are custom provider headers from config expected to work in v1.2.x? #15306 - Custom provider headers from config not working (similar but broader)
- bug(copilot): gpt-5.x-mini variants incorrectly routed to Responses API #15823 - GPT-5.x-mini routing issues (same routing logic involved)
- fix: provider headers from config not applied to fetch requests #11788 - Previous attempt to fix provider headers (referenced in Are custom provider headers from config expected to work in v1.2.x? #15306)
Environment
- OpenCode version: v1.2.x
- Provider: @ai-sdk/openai
- Models affected: gpt-5, gpt-5.1, gpt-5.2, etc. (any model triggering Responses API)
- Models working: gpt-4, gpt-4-turbo, etc. (Chat Completions API)
Fix Direction
The headers need to be properly passed through the custom provider chain:
createOpenAI options → GitHub Copilot provider → OpenAI Responses model → HTTP requests
Likely requires updating the header passing logic in the custom loader or provider creation chain.
Activity
tbrandenburg commentedon Apr 19, 2026
Status check against v1.14.17 (current as of 2026-04-19)
Partially still relevant, but the scope has narrowed.
What changed since the original report
The original report was filed against v1.2.x. Since then the codebase was significantly restructured in the provider layer. The key change relevant to this issue: the
@ai-sdk/github-copilotbundled provider now uses a custom copilot provider (packages/opencode/src/provider/sdk/copilot/copilot-provider.ts) that explicitly accepts and forwardsheadersthrough both thechatandresponsesmodel constructors via agetHeadersclosure (lines 60-83). So for the GitHub Copilot provider specifically, headers should now be forwarded to the Responses API path.What is still broken
The original report describes using the
openaiprovider (notgithub-copilot) with a custombaseURLpointing to an APIM gateway. In that case:provider.options.headersfrom user config is correctly merged inresolveSDK(provider.ts:1382+1415-1419) and passed to@ai-sdk/openai@3.0.53as constructor options.shouldUseCopilotResponsesApi(provider.ts:34-38) — this only triggers for models namedgpt-5+, and only for thegithub-copilotcustom loader (lines 191-199). The plainopenaiprovider does not go through this routing — it usessdk.languageModel(modelID)by default (line 1551).openaiprovider, whether headers reach the Responses API endpoint depends entirely on whether@ai-sdk/openaiv3.0.53 forwards constructor headers to its Responses implementation — this is an upstream SDK concern, not an opencode one.Current code paths
openaiprovider +gpt-5+model: Uses@ai-sdk/openai@3.0.53viasdk.languageModel(). Headers forwarding is upstream SDK responsibility.github-copilotprovider +gpt-5+model: Uses the custom copilot provider, which explicitly forwards headers toOpenAIResponsesLanguageModel. Likely fixed.model.headersin config): Applied atresolveSDKline 1415 and merged into the constructor options before SDK instantiation. Should work for both paths.Recommendation
If you're hitting this with the
openaiprovider pointing at an APIM, the fix is to use model-levelheadersin config (which are merged at SDK construction time) rather than provider-leveloptions.headers. If that also doesn't work with `@ai-sdk/openai@3.0.53, the issue is upstream in the AI SDK and should be filed there.If you're hitting this with the
github-copilotprovider, test with current v1.14.17 — the custom copilot provider now forwards headers throughgetHeadersto both chat and responses models.github-actions commentedon Jul 3, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.