fix: allow api_key="" to bypass credential validation for local servers#3274
Open
SanskaarUndale21 wants to merge 2 commits into
Open
fix: allow api_key="" to bypass credential validation for local servers#3274SanskaarUndale21 wants to merge 2 commits into
SanskaarUndale21 wants to merge 2 commits into
Conversation
Fixes openai#3224 - v2.34.0 changed the credential check from an identity check to a truthiness check, breaking api_key="" used by local OpenAI-compatible servers (llama.cpp, LM Studio, vLLM, etc.). Track _api_key_explicitly_provided before env var fallback so an explicit empty string is treated as intentional and skips the error, while omitting api_key entirely with no env var still raises.
There was a problem hiding this comment.
Pull request overview
This PR adjusts credential enforcement in the core OpenAI client constructors to restore compatibility with OpenAI-compatible local servers that intentionally pass api_key="" (no auth required), which started failing after a truthiness-based credential check was introduced in v2.34.0.
Changes:
- Track whether
api_keywas explicitly provided (pre–env var fallback) inOpenAI.__init__. - Apply the same tracking and updated enforcement logic in
AsyncOpenAI.__init__. - Update the “missing credentials” guard to allow explicitly provided empty-string API keys.
Comments suppressed due to low confidence (2)
src/openai/_client.py:702
- Same as the sync client: allowing
api_key=""at init is not sufficient for local-server usage because requests will still fail header validation (_validate_headers) unlessAuthorizationis explicitly omitted/provided. Consider carrying the “explicit empty api_key” signal through to request header validation for the async client as well.
if (
_enforce_credentials
and not self.api_key
and not _api_key_explicitly_provided
and self._api_key_provider is None
src/openai/_client.py:193
- Tests appear to cover the missing-credentials error when
api_key=None(seetests/test_client.py::test_validate_headers), but there’s no assertion for the new behavior thatapi_key=""is allowed. Adding sync + async unit tests forapi_key=""(withOPENAI_API_KEY/OPENAI_ADMIN_KEYunset) would prevent regressions in credential enforcement logic.
if (
_enforce_credentials
and not self.api_key
and not _api_key_explicitly_provided
and self._api_key_provider is None
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
189
to
193
| if ( | ||
| _enforce_credentials | ||
| and not self.api_key | ||
| and not _api_key_explicitly_provided | ||
| and self._api_key_provider is None |
…alidation for api_key=""
Copilot flagged that bypassing the init-time credential check was incomplete --
_validate_headers still raised TypeError on every request when api_key="" because
_bearer_auth returns {} for falsy keys. Store the flag as an instance variable and
check it in _validate_headers for both OpenAI and AsyncOpenAI.
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.
Fixes #3224
Summary
v2.34.0changed the credential check fromapi_key is Nonetonot self.api_key(truthiness), which rejectsapi_key=""as missing credentialsapi_key=""because no auth is needed_api_key_explicitly_providedbefore the env var fallback in bothOpenAI.__init__()andAsyncOpenAI.__init__()How it works
Capture whether the caller explicitly passed
api_keybefore the env var fallback:Then skip the error when the caller explicitly passed
api_key="":Behavior
api_key=""explicitly passedOpenAIErrorapi_key=None, no env varOpenAIErrorOpenAIErrorapi_key=None, env var setworkload_identitysetTest plan
OpenAI(api_key="", base_url="http://localhost:8080/v1")no longer raisesAsyncOpenAI(api_key="", base_url="http://localhost:8080/v1")no longer raisesOpenAI()with no env var still raisesOpenAIError