fix: support OPENAI_BASE_URL and api-key header for Azure APIM-style gateways#35
Closed
Rom Iluz (romiluz13) wants to merge 4 commits into
Closed
Conversation
…gateways OpenAI-compatible gateways (e.g. Azure APIM, Grove) often require an `api-key` header instead of `Authorization: Bearer`. The `openai` provider branch of `createModel` now reads `OPENAI_BASE_URL` from the environment and, when set, passes `api-key` as a `defaultHeaders` entry alongside the base URL so requests authenticate correctly against these gateways. `OPENAI_BASE_URL` is also removed from the deprecated-keys list in env.ts and added to `managedEnvKeys` so it can be persisted to and loaded from `~/.openwiki/.env` like other credentials.
DeepAgents reads repo files and sends them as {type:'file', mimeType, data}
content blocks in the message history. Third-party OpenAI-compatible gateways
(e.g. GLM-5.2 via Azure APIM/Grove) reject these with a 400 validation error
because they only accept plain string message content.
Add TextOnlyChatOpenAI, a thin ChatOpenAI subclass that intercepts _generate
and _streamResponseChunks to decode base64 file blocks into {type:'text'}
blocks before the messages reach the API. It is used automatically whenever
OPENAI_BASE_URL is set (i.e. any custom gateway), leaving the standard
providers untouched.
Author
|
Closing for now — the approach works but needs more testing before it's ready for upstream. |
GLM-5.2 via Azure APIM rejects tool/function messages whose content is an array even when every element is a text block. After stripping file blocks, join all-text arrays into a single string so every message has string content before it reaches the gateway.
…locks
The TextOnlyChatOpenAI subclass approach missed DeepAgents code paths that
bypass _generate/_streamResponseChunks. Replace with a global fetch
interceptor (installFileBlockScrubber) that rewrites the JSON body of every
chat/completions request destined for the custom base URL, converting
{type:'file', data:base64} content blocks to plain text strings before they
leave the process. This is guaranteed to catch all paths regardless of which
internal LangChain method is used.
PRATHAM KUMAR (rajpratham1)
suggested changes
Jul 4, 2026
PRATHAM KUMAR (rajpratham1)
left a comment
There was a problem hiding this comment.
OPENAI_BASE_URL is added as a supported configuration, but it's still listed in deprecatedEnvKeys. That means users who intentionally configure a custom gateway via OPENAI_BASE_URL will continue to receive deprecation warnings even though this PR officially introduces it as the supported mechanism. Please remove OPENAI_BASE_URL from deprecatedEnvKeys (or update the deprecation logic) so the diagnostics match the new behavior.
Collaborator
|
Thanks for the PR! The second issue was fixed upstream in deepagentsjs. The first issue was superseded by #328. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two bugs when using OpenWiki with OpenAI-compatible gateways (e.g. Azure APIM, Grove):
1. Wrong auth header (401)
These gateways use an
api-keyrequest header instead ofAuthorization: Bearer. Every request failed with 401 because the OpenAI SDK sends the wrong header.Additionally,
OPENAI_BASE_URLwas in thedeprecatedEnvKeyslist insrc/env.ts, so even if a user set it in~/.openwiki/.envit was silently dropped on load.2. File content blocks rejected (400)
DeepAgents reads repo files and sends them as
{type:'file', mimeType, data}content blocks in the message history. Third-party gateways (e.g. GLM-5.2 via Azure APIM) only accept plain string message content and reject these with:Fix
src/constants.ts— exportOPENAI_BASE_URL_ENV_KEYconstant.src/env.tsOPENAI_BASE_URLfromdeprecatedEnvKeysso it's no longer stripped on load/save.managedEnvKeysso it's persisted and included in diagnostics.src/agent/index.ts— two changes increateModel():OPENAI_BASE_URLfrom the environment; passapi-keyas adefaultHeadersentry for Azure APIM-style auth.OPENAI_BASE_URLis set, useTextOnlyChatOpenAI— a thinChatOpenAIsubclass that intercepts_generateand_streamResponseChunksto decode base64 file blocks into{type:'text'}blocks before they reach the API. Standard providers are unaffected.Usage
Or persist once in
~/.openwiki/.env:Test
Verified end-to-end against a live Azure APIM gateway (Grove) with
FW-GLM-5.2on a real Python repo — previously 401 on hello-world, then 400 on file blocks during--init; both now work correctly.🤖 Generated with Claude Code