Skip to content

fix(kb): resolve provider API key from env var over stored key#89

Merged
magroski merged 1 commit into
mainfrom
fix/kb-openai-key-from-env
Jul 7, 2026
Merged

fix(kb): resolve provider API key from env var over stored key#89
magroski merged 1 commit into
mainfrom
fix/kb-openai-key-from-env

Conversation

@gabrielbressan-tfy

Copy link
Copy Markdown
Collaborator

Problem

Knowledge bases persist the OpenAI/Gemini API key at creation time in up to three places — the KB params JSON (embedding_model / reranking_model), and, for ML-handler-backed embedding models, the model's own args. After we rotated all OpenAI keys, existing KBs kept using the stale stored key and failed with:

401 - Incorrect API key provided: sk-proj-…

The stored key shadows the environment variable because get_api_key gives creation-time args priority over env vars.

Change

Make the environment variable take precedence over any stored key on all KB key-resolution paths, so a rotated key can be updated in one place (the env) without recreating knowledge bases:

  • llm_client.py — embedding model prefers {PROVIDER}_API_KEY (gemini also honors GOOGLE_API_KEY) before the stored api_key.
  • controller.py — reranking model uses the same env-first resolution; adds the missing import os.
  • openai_handler.py — adds _resolve_api_key() (env-first, keyed off api_key_name so provider subclasses stay correct) and routes the predict / describe / validate / finetune sites through it.

Scoped to the KB + OpenAI-handler paths deliberately; the global get_api_key precedence is left unchanged.

Behavioral note

For OpenAI models, the env var now overrides even an inference-time USING api_key='...'. Intended for a single-source-of-truth setup, but flagging it as a genuine precedence change on the OpenAI ML engine.

Deploy

Requires OPENAI_API_KEY (and GOOGLE_API_KEY) present in the container env. In the dev compose this is wired via env_file: ./mindsdb/.env on the mindsdb service (that compose lives outside this repo, so it's not part of this diff).

Follow-up (discussed, not in this PR)

The more robust fix is to stop persisting the secret at creation and scrub existing rows (KB params, embedding-model args, and the openai ML engine connection args), after which env becomes the sole source of truth and these env-first overrides can be removed.

🤖 Generated with Claude Code

Knowledge bases persisted the OpenAI/Gemini API key at creation time
(in KB params, reranking params, and the embedding model's args). When
keys are rotated, every KB kept using the stale stored key and failed
with 401 Incorrect API key.

Make the environment variable take precedence over any stored key on all
KB key-resolution paths, so a rotated key can be updated in one place
without recreating knowledge bases:

- llm_client.py: embedding model prefers {PROVIDER}_API_KEY env var
  (gemini also honors GOOGLE_API_KEY) before the stored api_key.
- controller.py: reranking model uses the same env-first resolution;
  add missing `import os`.
- openai_handler.py: add `_resolve_api_key()` (env-first, keyed off
  api_key_name so provider subclasses stay correct) and route the
  predict/describe/validate/finetune sites through it.

Follow-up (discussed, not in this PR): stop persisting the secret at
creation and scrub existing rows so env becomes the sole source of
truth and these overrides can be removed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@gabrielbressan-tfy gabrielbressan-tfy requested review from gnumoksha, magroski and patrickadeelino and removed request for gnumoksha July 7, 2026 20:13
@gabrielbressan-tfy

Copy link
Copy Markdown
Collaborator Author

Fix temporario, vou subir uma PR no mktplace pra parar de enviar a key na criação da kb, que era o jeito que o mindsdb aceitava

@mergify

mergify Bot commented Jul 7, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@magroski magroski merged commit e33379c into main Jul 7, 2026
3 of 5 checks passed
@magroski magroski deleted the fix/kb-openai-key-from-env branch July 7, 2026 20:14
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