Skip to content

fix: allow api_key="" to bypass credential validation for local servers#3225

Open
tbille wants to merge 2 commits into
openai:mainfrom
tbille:fix/empty-api-key-credential-check
Open

fix: allow api_key="" to bypass credential validation for local servers#3225
tbille wants to merge 2 commits into
openai:mainfrom
tbille:fix/empty-api-key-credential-check

Conversation

@tbille
Copy link
Copy Markdown

@tbille tbille commented May 11, 2026

Summary

Fixes #3224

  • In v2.34.0, the credential validation in _client.py changed from an identity check (api_key is None) to a truthiness check (not self.api_key), which causes api_key="" to be rejected as "Missing credentials"
  • This broke OpenAI-compatible local servers (llama.cpp, llamafile, LM Studio, vLLM, etc.) that don't require authentication and intentionally pass api_key=""
  • This fix tracks whether api_key was explicitly provided by the caller and skips the credential error when it was, even if the value is an empty string

Changes

src/openai/_client.py: Added _api_key_explicitly_set local variable in both OpenAI.__init__() and AsyncOpenAI.__init__() that tracks whether the caller explicitly passed an api_key argument (vs it being resolved from the environment or defaulting to None). The credential validation check now also considers this flag, so api_key="" no longer triggers the error while api_key=None with no env var still does.

tests/test_client.py: Added test cases for both sync and async clients verifying that api_key="" does not raise OpenAIError, restoring the pre-v2.34.0 behavior.

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
@tbille tbille requested a review from a team as a code owner May 11, 2026 19:32
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/openai/_client.py Outdated
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AsyncOpenAI(api_key="") raises OpenAIError in v2.34.0, breaking OpenAI-compatible local servers

1 participant