fix: respect USE_USER_TOKEN config in OpenAI endpoint#2187
Open
balex42 wants to merge 2 commits intohuggingface:mainfrom
Open
fix: respect USE_USER_TOKEN config in OpenAI endpoint#2187balex42 wants to merge 2 commits intohuggingface:mainfrom
balex42 wants to merge 2 commits intohuggingface:mainfrom
Conversation
Previously, the OpenAI endpoint wrapper unconditionally injected the user's OIDC token into the Authorization header if a session existed. This overwrote the OPENAI_API_KEY, causing 401 Unauthorized errors when using external providers alongside OIDC authentication. This commit updates the header injection to properly check if USE_USER_TOKEN is set to "true" before passing the user token, restoring compatibility with external OpenAI-compatible backends.
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.
Description
This PR fixes an authentication bug where the
USE_USER_TOKENenvironment variable is ignored across both standard and MCP OpenAI-compatible endpoint calls.Currently, if a user session exists (e.g., via a custom OIDC provider),
locals.tokenis unconditionally injected as theAuthorization: Bearerheader for LLM inference calls. This overwrites the configuredOPENAI_API_KEY, causing a401 Unauthorizederror when interacting with external OpenAI-compatible backends (like Ollama Cloud or vLLM) that do not recognize the OIDC token.Motivation and Context
The
.envfile explicitly documents that the user token should only be used for inference ifUSE_USER_TOKEN=true:# When set to true, user token will be used for inference callsUSE_USER_TOKEN=falseHowever, the implementation was unconditionally applying
locals.tokenif it existed. This PR updates the headers payload to respect theUSE_USER_TOKENconfiguration for both standard generation and tool use.Changes
Modified the following files to check
config.USE_USER_TOKEN === "true"before injectinglocals.tokeninto the authorization header:src/lib/server/endpoints/openai/endpointOai.ts(Covers standard streaming and non-streaming completions)src/lib/server/textGeneration/mcp/runMcpFlow.ts(Covers MCP tool-enabled streaming and fallback non-streaming completions)Before:
After:
How Has This Been Tested?
OPENID_CLIENT_ID,OPENID_PROVIDER_URL).OPENAI_BASE_URLandOPENAI_API_KEY).USE_USER_TOKEN=false.401 Unauthorizedand[mcp] flow failed, falling back to default endpointbecause the backend receives the OIDC token instead of the API key.OPENAI_API_KEYis correctly utilized.