Skip to content

fix(providers): use environment API key when saved key is blank - #541

Open
gezerui888 wants to merge 4 commits into
plmbr:mainfrom
gezerui888:fix/openai-key-fallback-439
Open

gezerui888 wants to merge 4 commits into
plmbr:mainfrom
gezerui888:fix/openai-key-fallback-439

Conversation

@gezerui888

@gezerui888 gezerui888 commented Nov 11, 2026 •

Copy link
Copy Markdown

Closes #439

A blank saved API key prevents the OpenAI-compatible provider from using OPENAI_API_KEY. I use the environment key when it is nonempty and preserve the installed SDK’s behavior when no key is available. Saved null values retain the SDK behavior, and explicit keys take precedence.

I also mark the key optional in Settings and explain the shared environment variable and custom-endpoint behavior in the administrator guide.

The 34 provider tests pass with OpenAI 2.20.0 and 3.54.0, including request URLs, authorization and missing credentials. I also ran pytest tests/ -q (2,533 passed), jlpm tsc --noEmit, jlpm lint:check, and jlpm jest --runInBand (955 passed).

cc @mbektas

@gezerui888

Copy link
Copy Markdown
Author

@mbektas, could you review the environment-key fallback fix and approve the pending CI workflows? I'm happy to address any changes.

@mbektas mbektas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@mbektas

mbektas commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

@pjdoland could you review too?

@pjdoland

Copy link
Copy Markdown
Collaborator

Will do tomorrow.

@mbektas mbektas added the enhancement New feature or request label Nov 14, 2026
@pjdoland

Copy link
Copy Markdown
Collaborator

Thanks for picking this up, @gezerui888, and for the tests that go through the real SDK. I ran them locally and the full suite passes.

There's one case my proposal in #439 missed: an OpenAI-compatible endpoint that doesn't need a key at all (an internal gateway, or a local server such as Ollama or vLLM), with the saved key left blank and no OPENAI_API_KEY set. On openai 2.x, which litellm 1.103.x still requires (openai>=2.20.0,<3.0.0), main passes api_key="" and the SDK sends the request with no Authorization header, so that setup works today. With this PR the blank key becomes None, nothing is in the environment, and the SDK raises OpenAIError ("The api_key client option must be set...") on every chat and inline completion request.

I ran both models against main and this branch, pointed at a mocked endpoint:

openai OPENAI_API_KEY main (blank key) this PR (blank key)
2.8.0 unset request sent, no auth header OpenAIError
2.8.0 set request sent, no auth header Bearer <env key>
3.49.0 unset OpenAIError OpenAIError
3.49.0 set OpenAIError Bearer <env key>

One way to fix #439 without the regression is to fall back to the environment only when it actually has a key, and otherwise keep passing "":

api_key = self.get_property("api_key").value
if not (api_key and api_key.strip()):
    api_key = None if os.environ.get("OPENAI_API_KEY") else ""

A test case with OPENAI_API_KEY unset would pin it; the current parametrization always sets it. The admin guide note could then say the key can be omitted when OPENAI_API_KEY is set.

Two smaller things:

  • OPENAI_API_KEY is also the variable NBI uses for the Codex ACP agent's key (acp_api_key in STRING_OVERRIDE_SPEC). A deployment that sets it for Codex would now also send it to whatever base URL the OpenAI-compatible provider uses when its saved key is blank. That's what fix(providers): openai-compatible sends an empty api_key, so the documented env-var fallback never happens #439 asked for, so it may be fine, but @mbektas, you may want a sentence about it in the admin guide.
  • The provider still declares api_key as required (optional=False), so Settings shows "API key is required." on a blank field, while the docs now say it can be omitted. litellm-compatible already marks it optional. That could go in this PR or a follow-up.

@gezerui888

Copy link
Copy Markdown
Author

I corrected both request paths to preserve keyless requests with OpenAI 2.x while retaining environment-key fallback and explicit-key precedence. Saved null values keep the existing SDK behavior. I also made the key optional in Settings and documented the shared ACP variable and custom-endpoint behavior. The request matrix, full Python suite, Jest, typecheck, and lint pass.

@mbektas

mbektas commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

@gezerui888 can you check the failed tests

@gezerui888

Copy link
Copy Markdown
Author

I reproduced all 16 failures with OpenAI 3.54.0. The tests assumed every supported SDK accepted an empty key. I corrected the expectations and the admin guide. The 34 provider tests pass on both 2.20.0 and 3.54.0. The full Python suite, Jest, typecheck, and lint also pass locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(providers): openai-compatible sends an empty api_key, so the documented env-var fallback never happens

3 participants