Skip to content

feat/optional api key for OpenAI compatible#187

Merged
xiami762 merged 4 commits intomainfrom
feat/optional-api-key-for-openai-compatible
Apr 24, 2026
Merged

feat/optional api key for OpenAI compatible#187
xiami762 merged 4 commits intomainfrom
feat/optional-api-key-for-openai-compatible

Conversation

@duguwanglong
Copy link
Copy Markdown
Contributor

fix(provider): hide no-auth API key sentinel from credentials GET response

GET /providers/{id}/credentials was returning the internal "not-needed"
placeholder verbatim, causing the WebUI to pre-fill the API Key input
with that literal string when users reopened the configure dialog for
openai-compatible / custom-* providers without a real API key.

Mask the sentinel back to None on read while keeping has_credential=True
so the UI knows a credential record exists. Also extract the value into
a module-level _NO_API_KEY_PLACEHOLDER constant + _is_placeholder_api_key
helper so the sentinel is defined in exactly one place and defended both
when the value comes from the secret manager and from an inline
flocks.json apiKey entry.

Adds three regression tests covering placeholder masking, real key
pass-through, and the inline-flocks.json edge case.

…om providers

Self-hosted LLM gateways (vLLM, internal proxies, etc.) often expose an
OpenAI-compatible endpoint without enforcing auth, so requiring an API
key locked these users out of `openai-compatible` and `custom-*`
providers. Mandatory keys remain enforced for SaaS providers (OpenAI,
Anthropic, Google, Azure, DeepSeek, Volcengine, Alibaba, Tencent,
SiliconFlow) — behavior unchanged for them.

Backend
- routes/provider.py: `set_provider_credentials` accepts an empty
  `api_key` for `openai-compatible` / `custom-*` and persists a
  `not-needed` sentinel so the OpenAI SDK still constructs cleanly.
  Other provider IDs continue to return HTTP 400.
- sdk/openai_base.py: new opt-in `ALLOW_NO_API_KEY` class flag; when
  True, `_get_client()` falls back to `NO_API_KEY_PLACEHOLDER` instead
  of raising. Default False keeps SaaS subclasses strict.
- provider.py: `DynamicOpenAIProvider` opts into `ALLOW_NO_API_KEY`,
  defaults `_api_key` to the sentinel when unresolved, and overrides
  `is_configured()` to treat a configured base URL alone as sufficient.
  Same `_api_key` fallback for `DynamicCherryProvider` (it inherits
  `is_configured` via OpenAICompatibleProvider).
- sdk/openai_compatible.py: override `is_configured()` to require an
  explicit `configure()` call AND accept either api_key or base_url —
  guards against the constructor's env-default seeds being misread as
  "ready" before the user configures anything.

Frontend
- pages/Model/index.tsx: extract `providerAllowsEmptyApiKey()` helper
  and apply it to all four API-key validation sites in the wizard and
  configure dialog. Drop the `*` mark, swap placeholder, and add a
  hint line for these providers.
- locales/{en-US,zh-CN}/model.json: add `apiKeyOptional`,
  `apiKeyOptionalHint`, `apiKeyOptionalPlaceholder`.

Tests
- tests/server/routes/test_provider_optional_api_key.py: 13 cases
  covering route acceptance, route rejection for strict providers,
  dynamic provider client construction, `is_configured()` semantics
  (including the regression where constructor env defaults must not
  imply "configured"), and Cherry inheriting the override.

Made-with: Cursor
…ponse

GET /providers/{id}/credentials was returning the internal "not-needed"
placeholder verbatim, causing the WebUI to pre-fill the API Key input
with that literal string when users reopened the configure dialog for
openai-compatible / custom-* providers without a real API key.

Mask the sentinel back to None on read while keeping has_credential=True
so the UI knows a credential record exists. Also extract the value into
a module-level _NO_API_KEY_PLACEHOLDER constant + _is_placeholder_api_key
helper so the sentinel is defined in exactly one place and defended both
when the value comes from the secret manager and from an inline
flocks.json apiKey entry.

Adds three regression tests covering placeholder masking, real key
pass-through, and the inline-flocks.json edge case.

Made-with: Cursor
@duguwanglong duguwanglong requested a review from xiami762 April 24, 2026 09:05
…inel

Three small follow-ups suggested in the PR review (no behavior regression):

1. Document the (extremely unlikely) collision risk where an upstream
   service might mint a real key whose literal value equals the
   "not-needed" sentinel, so future maintainers know the sentinel is
   reserved and where to change it.

2. Replace the naive 4/4 mask of the sentinel ("not-***eded", which can
   look like a real short API key in log searches) with an explicit
   "<no-auth>" marker in the credentials.saving audit log. New regression
   test asserts both presence of the marker and absence of the naive
   mask form.

3. Tighten the WebUI ProviderCredentials TypeScript type so optional
   string fields are typed as ``string | null | undefined`` (matching
   what FastAPI/Pydantic actually emits as JSON ``null``) and document
   that ``api_key`` may be null even when ``has_credential`` is true,
   directing UI code to ``has_credential`` for the "is configured" check.

Made-with: Cursor
…dentials

After widening ProviderCredentials.secret_id / api_key_masked to
``string | null | undefined`` (so Pydantic ``null`` values type-check
correctly), the providerCredentialsAdapter no longer satisfies the local
Credentials interface in CredentialsCard which had ``string | undefined``.

Widen the local Credentials interface symmetrically. MCPCredentials with
the narrower ``string | undefined`` remains assignable as a subtype, so
the mcpCredentialsAdapter path continues to compile.

Made-with: Cursor
@xiami762 xiami762 merged commit 441dca8 into main Apr 24, 2026
2 checks passed
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.

2 participants