Repository navigation
Fix: restore saved api_key fallback in add_llm (#14921) - #14941
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe ChangesAPI Key Recovery for LLM edits
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
api/apps/llm_app.py (1)
197-203: ⚡ Quick winSuffix map duplicates the per-factory branches below; extract a single source of truth.
The mapping in
_LLM_NAME_SUFFIX(lines 197–202) restates the same suffix policy that is hand-rolled in theelif factory == "LocalAI"/"HuggingFace"/"OpenAI-API-Compatible"/"VLLM"branches at lines 238–248. Any future change (new factory, renamed suffix) must be made in both places or the fallback silently fails to locate the saved row. Lifting it to a module-level constant and reusing it in both spots removes the drift risk.♻️ Proposed refactor
+# Factories whose stored TenantLLM.llm_name is suffixed to disambiguate user-named models. +_LLM_NAME_SUFFIX = { + "LocalAI": "___LocalAI", + "HuggingFace": "___HuggingFace", + "OpenAI-API-Compatible": "___OpenAI-API", + "VLLM": "___VLLM", +} + `@manager.route`("/add_llm", methods=["POST"]) # noqa: F821 ... - if req.get("api_key") is None and llm_name: - _LLM_NAME_SUFFIX = { - "LocalAI": "___LocalAI", - "HuggingFace": "___HuggingFace", - "OpenAI-API-Compatible": "___OpenAI-API", - "VLLM": "___VLLM", - } - saved_llm_name = llm_name + _LLM_NAME_SUFFIX.get(factory, "") + if req.get("api_key") is None and llm_name: + saved_llm_name = llm_name + _LLM_NAME_SUFFIX.get(factory, "") ... - elif factory == "LocalAI": - llm_name += "___LocalAI" - - elif factory == "HuggingFace": - llm_name += "___HuggingFace" - - elif factory == "OpenAI-API-Compatible": - llm_name += "___OpenAI-API" - - elif factory == "VLLM": - llm_name += "___VLLM" + elif factory in _LLM_NAME_SUFFIX: + llm_name += _LLM_NAME_SUFFIX[factory]🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/llm_app.py` around lines 197 - 203, The suffix map is duplicated: extract _LLM_NAME_SUFFIX into a single module-level constant and use that constant wherever suffixes are determined (replace the inline map and the per-factory branches that currently hand-roll suffixes); update the saved_llm_name computation (using llm_name + SUFFIX_MAP.get(factory, "")) and refactor the factory-specific branches (the blocks checking factory == "LocalAI"/"HuggingFace"/"OpenAI-API-Compatible"/"VLLM") to reference the same SUFFIX_MAP constant so there is a single source of truth for factory→suffix mapping (keep existing keys "LocalAI", "HuggingFace", "OpenAI-API-Compatible", "VLLM").
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@api/apps/llm_app.py`:
- Around line 196-214: Add structured logging to the API-key recovery flow
inside the block that checks req.get("api_key") is None and llm_name: emit a
debug/info log when you attempt recovery (include llm_name and factory), log
whether TenantLLMService.query returned any matching existing_llms (e.g., count
or existence) and log the result of TenantLLMService._decode_api_key_config as
"decoded key present" vs "no decoded key" without ever logging the actual
decoded value; update the branch that assigns req["api_key"] to also log that
the request was mutated (include non-sensitive context like saved_llm_name and
tenant id/current_user.id) and ensure logs use the existing project
logger/structured fields rather than printing.
---
Nitpick comments:
In `@api/apps/llm_app.py`:
- Around line 197-203: The suffix map is duplicated: extract _LLM_NAME_SUFFIX
into a single module-level constant and use that constant wherever suffixes are
determined (replace the inline map and the per-factory branches that currently
hand-roll suffixes); update the saved_llm_name computation (using llm_name +
SUFFIX_MAP.get(factory, "")) and refactor the factory-specific branches (the
blocks checking factory ==
"LocalAI"/"HuggingFace"/"OpenAI-API-Compatible"/"VLLM") to reference the same
SUFFIX_MAP constant so there is a single source of truth for factory→suffix
mapping (keep existing keys "LocalAI", "HuggingFace", "OpenAI-API-Compatible",
"VLLM").
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8f9d8aa8-27a9-4636-a2db-a27f9c4b1522
📒 Files selected for processing (1)
api/apps/llm_app.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14941 +/- ##
==========================================
- Coverage 95.59% 94.16% -1.43%
==========================================
Files 10 10
Lines 703 703
Branches 112 112
==========================================
- Hits 672 662 -10
- Misses 21 25 +4
- Partials 10 16 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Closes #14921.
Reconfiguring an existing LLM provider to enable tool call or
vision fails with
Your API key is invalid. Fail to access model.even when the saved API key is correct. The most visible report is
VLLM ("Cannot add vllm model" once
--enable-auto-tool-choice/vision is toggled on), but the bug applies to every provider whose
api_key field stays blank in edit mode.
Root cause
PR #14885 ("Fix: llm add api key overridden") removed the existing-key
lookup in
api/apps/llm_app.py::add_llm. The intent was correct —stop the saved key from clobbering a user-provided new one — but the
removal was unconditional, so the edit path now has no fallback at all:
web/src/pages/user-setting/setting-model/hooks.tsx:230sets theinitial
api_keyform value to''in edit mode (the real key isnever returned to the browser).
is_tools/visionwithout retyping the key.hooks.tsx:183-185strips the emptyapi_keyfrom the payload.add_llmdefaults to the placeholder"x"(
api/apps/llm_app.py:182)."x"withYour API key is invalid.Fix
Restore the fallback narrowly, before any factory-specific handler
runs:
req.get("api_key") is None, look up the tenant's existing record(using the correctly suffixed
llm_namefor VLLM /OpenAI-API-Compatible / LocalAI / HuggingFace).
_decode_api_key_configand write onlythe decoded
api_keystring back intoreq["api_key"]. Never usethe raw JSON payload — that was the exact thing PR Fix: llm add api key overridden #14885 was trying
to avoid.
req.get("api_key")is notNoneand the fallback is skipped, so PR Fix: llm add api key overridden #14885's fix is preserved.provider_orderonlyapikey_json([...])rebuilds the JSON with savedapi_key+ new fieldFiles changed
api/apps/llm_app.py— restored fallback inadd_llm(no other call sites touched).Test plan
→ save succeeds, validation runs against the saved key.
image2text),leave api_key blank → save succeeds.
saved one (
is Nonecheck skips the fallback). Verify via the DBrow or by deliberately typing a wrong key and observing the
validation failure.
provider_order→ resulting api_key JSON contains the savedapi_keyand the newprovider_order.no-ops, behaves as before.
Type of change