fix: allow api_key="" to bypass credential validation for local servers#3225
Open
tbille wants to merge 2 commits into
Open
fix: allow api_key="" to bypass credential validation for local servers#3225tbille wants to merge 2 commits into
tbille wants to merge 2 commits into
Conversation
In v2.34.0, the credential validation changed from an identity check (api_key is None) to a truthiness check (not self.api_key), which caused api_key="" to be rejected as missing credentials. This broke OpenAI-compatible local servers (llama.cpp, llamafile, LM Studio, vLLM) that don't require authentication. Track whether api_key was explicitly provided by the caller and skip the credential error when it was, even if the value is an empty string. Fixes openai#3224
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 168a416518
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Move _api_key_explicitly_set check before the OPENAI_API_KEY env var lookup so that only caller-provided api_key="" bypasses validation. OPENAI_API_KEY="" in the environment (likely misconfiguration) still raises the Missing credentials error. Add tests for the OPENAI_API_KEY="" env var scenario.
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.
Summary
Fixes #3224
_client.pychanged from an identity check (api_key is None) to a truthiness check (not self.api_key), which causesapi_key=""to be rejected as "Missing credentials"api_key=""api_keywas explicitly provided by the caller and skips the credential error when it was, even if the value is an empty stringChanges
src/openai/_client.py: Added_api_key_explicitly_setlocal variable in bothOpenAI.__init__()andAsyncOpenAI.__init__()that tracks whether the caller explicitly passed anapi_keyargument (vs it being resolved from the environment or defaulting toNone). The credential validation check now also considers this flag, soapi_key=""no longer triggers the error whileapi_key=Nonewith no env var still does.tests/test_client.py: Added test cases for both sync and async clients verifying thatapi_key=""does not raiseOpenAIError, restoring the pre-v2.34.0 behavior.