Repository navigation
Fix: Bedrock api_key overridden by existing-key fallback in add_llm - #14707
Conversation
…in add_llm Upstream commit 0501134 introduced a fallback that replaces api_key with the existing DB value when req["api_key"] is None. For Bedrock, credentials are sent as separate fields (bedrock_ak, bedrock_sk, etc.) so req["api_key"] is always None — causing the assembled JSON key to be silently replaced by "x", which then fails json.loads() during the connection test. Fix: write the assembled key into req["api_key"] so the None check is False. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
📝 WalkthroughWalkthroughThe ChangesBedrock Authentication Payload Assignment
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes 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)
202-207: ⚡ Quick winMissing logging for the new Bedrock flow.
The Bedrock branch is a new/modified code path but does not emit any log entry, unlike the rest of the function (e.g., line 142 logs rerank pass/fail). A single debug log when the Bedrock JSON key is assembled aids troubleshooting similar auth issues in future.
req["api_key"] = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) api_key = req["api_key"] + logging.debug("Bedrock: assembled api_key from request credentials for llm_name=%s", llm_name)As per coding guidelines: "Add logging for new flows" for
**/*.pyfiles.🤖 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 202 - 207, Add a debug log in the Bedrock branch (the block where factory == "Bedrock") immediately after assembling req["api_key"] / api_key via apikey_json so the new flow is visible in logs; use the same logger instance used elsewhere in this function and log a concise message like "Bedrock auth assembled" including only non-secret fields (e.g., auth_mode, bedrock_region, aws_role_arn) from the apikey_json result and ensure bedrock_ak/bedrock_sk are not printed.
🤖 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 202-207: The Bedrock branch sets req["api_key"] to
apikey_json(...) unconditionally, which produces a non-null JSON string even
when all Bedrock fields are empty and thus prevents the existing-key fallback
from running; change the logic in the factory == "Bedrock" branch to inspect the
individual fields (auth_mode, bedrock_ak, bedrock_sk, bedrock_region,
aws_role_arn) and only assign req["api_key"] = apikey_json(...) if at least one
of those fields is non-empty/Truthy, otherwise leave req["api_key"] unset so the
later req.get("api_key") fallback will reuse the stored key. Ensure you
reference the same apikey_json helper and the req["api_key"] slot used by the
fallback logic.
---
Nitpick comments:
In `@api/apps/llm_app.py`:
- Around line 202-207: Add a debug log in the Bedrock branch (the block where
factory == "Bedrock") immediately after assembling req["api_key"] / api_key via
apikey_json so the new flow is visible in logs; use the same logger instance
used elsewhere in this function and log a concise message like "Bedrock auth
assembled" including only non-secret fields (e.g., auth_mode, bedrock_region,
aws_role_arn) from the apikey_json result and ensure bedrock_ak/bedrock_sk are
not printed.
🪄 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: 1be761d6-a172-4c3f-ba46-14264a3d0df7
📒 Files selected for processing (1)
api/apps/llm_app.py
| elif factory == "Bedrock": | ||
| # For Bedrock, due to its special authentication method | ||
| # Assemble bedrock_ak, bedrock_sk, bedrock_region | ||
| api_key = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) | ||
| # Write into req["api_key"] to prevent the "existing key" override logic from replacing it | ||
| req["api_key"] = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) | ||
| api_key = req["api_key"] |
There was a problem hiding this comment.
Empty Bedrock credentials bypass the existing-key fallback, breaking update-without-re-entering-credentials.
When a user updates an existing Bedrock model without supplying new credentials, all the Bedrock-specific fields (auth_mode, bedrock_ak, etc.) are absent or empty strings in req. apikey_json produces '{"auth_mode": "", "bedrock_ak": "", "bedrock_sk": "", "bedrock_region": "", "aws_role_arn": ""}' — a non-None string. Writing this to req["api_key"] before the fallback check at line 253 means req.get("api_key") is None is never True for Bedrock, so the stored key is never reused and empty credentials are persisted to the DB. This contradicts the PR's own test-plan point: "Update the model and verify existing key reuse when credential fields are left empty".
The fix should conditionally write to req["api_key"] only when at least one credential field carries a real value, allowing the existing-key fallback to run when the user supplies nothing new.
🐛 Proposed fix
elif factory == "Bedrock":
# For Bedrock, due to its special authentication method
# Assemble bedrock_ak, bedrock_sk, bedrock_region
- # Write into req["api_key"] to prevent the "existing key" override logic from replacing it
- req["api_key"] = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"])
- api_key = req["api_key"]
+ # Only write into req["api_key"] when real credentials were provided so that
+ # the "existing key" fallback can still run when the user leaves all fields empty.
+ _bedrock_keys = ["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]
+ if any(req.get(k) for k in _bedrock_keys):
+ req["api_key"] = apikey_json(_bedrock_keys)
+ api_key = req["api_key"]📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| elif factory == "Bedrock": | |
| # For Bedrock, due to its special authentication method | |
| # Assemble bedrock_ak, bedrock_sk, bedrock_region | |
| api_key = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) | |
| # Write into req["api_key"] to prevent the "existing key" override logic from replacing it | |
| req["api_key"] = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) | |
| api_key = req["api_key"] | |
| elif factory == "Bedrock": | |
| # For Bedrock, due to its special authentication method | |
| # Assemble bedrock_ak, bedrock_sk, bedrock_region | |
| # Only write into req["api_key"] when real credentials were provided so that | |
| # the "existing key" fallback can still run when the user leaves all fields empty. | |
| _bedrock_keys = ["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"] | |
| if any(req.get(k) for k in _bedrock_keys): | |
| req["api_key"] = apikey_json(_bedrock_keys) | |
| api_key = req["api_key"] |
🤖 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 202 - 207, The Bedrock branch sets
req["api_key"] to apikey_json(...) unconditionally, which produces a non-null
JSON string even when all Bedrock fields are empty and thus prevents the
existing-key fallback from running; change the logic in the factory == "Bedrock"
branch to inspect the individual fields (auth_mode, bedrock_ak, bedrock_sk,
bedrock_region, aws_role_arn) and only assign req["api_key"] = apikey_json(...)
if at least one of those fields is non-empty/Truthy, otherwise leave
req["api_key"] unset so the later req.get("api_key") fallback will reuse the
stored key. Ensure you reference the same apikey_json helper and the
req["api_key"] slot used by the fallback logic.
|
Hi @yingfeng — this PR fixes a regression introduced by your commit That commit added a fallback in The fix is a one-liner, consistent with how |
|
@Lynn-Inf please consider handle this logic in the new refined code. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14707 +/- ##
==========================================
- 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:
|
…nfiniflow#14707) ## Summary - Adding a Bedrock model from the frontend fails with `Fail to access model(Bedrock/<model>).Expecting value: line 1 column 1 (char 0)`. - The assembled Bedrock JSON credentials are silently replaced by `"x"` before the connection test, causing `json.loads("x")` to raise a `JSONDecodeError`. ## What problem does this PR solve? Commit `050113482` introduced a fallback in `add_llm()` that reuses the existing DB key when `req.get("api_key") is None`: ```python if req.get("api_key") is None: api_key = existing_api_key if existing_api_key is not None else "x" ``` For Bedrock, credentials are sent as separate fields (`auth_mode`, `bedrock_ak`, `bedrock_sk`, `bedrock_region`, `aws_role_arn`) — the frontend does not send an `api_key` field. The function correctly assembles the JSON key: ```python api_key = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) ``` But since `req.get("api_key")` is `None`, the override immediately replaces `api_key` with `"x"` (or a stale DB value). `LiteLLMBase` then calls `json.loads("x")` for Bedrock auth → `JSONDecodeError`. ## Type of change - [x] Bug Fix (non-breaking change which fixes an issue) ## Changes **`api/apps/llm_app.py`** Write the assembled key into `req["api_key"]` so the `None` check evaluates to `False` and the override is skipped — consistent with how `Tencent Cloud` is already handled. ```python # Before api_key = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) # After req["api_key"] = apikey_json(["auth_mode", "bedrock_ak", "bedrock_sk", "bedrock_region", "aws_role_arn"]) api_key = req["api_key"] ``` ## Test plan - [ ] Configure a Bedrock provider in Model Providers with valid AWS credentials - [ ] Add a Bedrock chat model — verify no `Expecting value` error - [ ] Update the same model — verify the existing key is reused correctly when credentials fields are left empty 🤖 Generated with [Claude Code](https://claude.ai/claude-code) Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Summary
Fail to access model(Bedrock/<model>).Expecting value: line 1 column 1 (char 0)."x"before the connection test, causingjson.loads("x")to raise aJSONDecodeError.What problem does this PR solve?
Commit
050113482introduced a fallback inadd_llm()that reuses the existing DB key whenreq.get("api_key") is None:For Bedrock, credentials are sent as separate fields (
auth_mode,bedrock_ak,bedrock_sk,bedrock_region,aws_role_arn) — the frontend does not send anapi_keyfield. The function correctly assembles the JSON key:But since
req.get("api_key")isNone, the override immediately replacesapi_keywith"x"(or a stale DB value).LiteLLMBasethen callsjson.loads("x")for Bedrock auth →JSONDecodeError.Type of change
Changes
api/apps/llm_app.pyWrite the assembled key into
req["api_key"]so theNonecheck evaluates toFalseand the override is skipped — consistent with howTencent Cloudis already handled.Test plan
Expecting valueerror🤖 Generated with Claude Code