Skip to content

fix(settings): prevent API key duplication on save#9080

Open
localai-bot wants to merge 2 commits intomudler:masterfrom
localai-bot:fix-settings-api-keys-dup
Open

fix(settings): prevent API key duplication on save#9080
localai-bot wants to merge 2 commits intomudler:masterfrom
localai-bot:fix-settings-api-keys-dup

Conversation

@localai-bot
Copy link
Contributor

Description

Fixes issue #9071 where saving settings from the WebUI would duplicate API keys.

Problem

The Settings.jsx page stores API keys in a textarea as (string), but the backend expects an array in . When saving, the string was being sent directly instead of being converted to an array, causing duplication issues.

Solution

  • In the function, convert to an array before saving
  • Split the text by newlines, trim each key, and filter out empty strings
  • Set to an empty array when no keys are provided

Changes

  • Modified:

Testing

  • Save settings with multiple API keys (one per line)
  • Verify keys are saved correctly without duplication
  • Test with empty API keys field

@netlify
Copy link

netlify bot commented Mar 19, 2026

Deploy Preview for localai ready!

Name Link
🔨 Latest commit e2ae3d2
🔍 Latest deploy log https://app.netlify.com/projects/localai/deploys/69bd07c8482fe600082ea72c
😎 Deploy Preview https://deploy-preview-9080--localai.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@mudler mudler requested a review from Copilot March 19, 2026 22:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Prevents API key duplication when saving settings by converting the WebUI textarea value into the backend’s expected api_keys array format.

Changes:

  • Build a settingsToSave payload before saving.
  • Parse api_keys_text into a trimmed, newline-split api_keys array (or [] when empty).
  • Save the normalized payload via settingsApi.save(settingsToSave).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

} else {
settingsToSave.api_keys = []
}
}
Copy link

Copilot AI Mar 19, 2026

Choose a reason for hiding this comment

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

settingsToSave still includes api_keys_text (copied from settings). If api_keys_text is a UI-only field, sending it to the backend can cause schema/validation issues or unintended persistence. Consider deleting api_keys_text from settingsToSave before calling settingsApi.save, so the payload matches the backend contract (api_keys only).

Suggested change
}
}
// Remove UI-only field before sending to backend
delete settingsToSave.api_keys_text

Copilot uses AI. Check for mistakes.
if (settingsToSave.api_keys_text !== undefined && settingsToSave.api_keys_text !== null) {
const text = settingsToSave.api_keys_text
if (typeof text === 'string' && text.trim() !== '') {
settingsToSave.api_keys = text.split('\n').map(k => k.trim()).filter(k => k !== '')
Copy link

Copilot AI Mar 19, 2026

Choose a reason for hiding this comment

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

This normalizes whitespace/empties, but it doesn’t prevent duplicate keys within the textarea itself (e.g., the same key repeated on multiple lines). Since this PR targets duplication behavior, consider de-duplicating the resulting array (e.g., via a Set) to ensure api_keys is unique before saving.

Suggested change
settingsToSave.api_keys = text.split('\n').map(k => k.trim()).filter(k => k !== '')
const keys = text.split('\n').map(k => k.trim()).filter(k => k !== '')
settingsToSave.api_keys = Array.from(new Set(keys))

Copilot uses AI. Check for mistakes.
@localai-bot
Copy link
Contributor Author

Fixes Applied

Addressed Copilot review comments:

  1. Removed api_keys_text field: Now deletes from the payload before saving to avoid schema/validation issues
  2. Deduplicated API keys: Uses to ensure unique API keys before saving

These changes ensure the backend receives only the normalized array without duplicate entries or UI-only fields.

@localai-bot
Copy link
Contributor Author

Fixes Applied

Addressed Copilot review comments:

  1. Removed api_keys_text field: Now deletes api_keys_text from the payload before saving to avoid schema/validation issues
  2. Deduplicated API keys: Uses Array.from(new Set(keys)) to ensure unique API keys before saving

These changes ensure the backend receives only the normalized api_keys array without duplicate entries or UI-only fields.

@localai-bot localai-bot force-pushed the fix-settings-api-keys-dup branch from 36d175d to 01a8524 Compare March 20, 2026 01:12
@localai-bot
Copy link
Contributor Author

I've addressed the Copilot review comments:

  1. Deduplicated API keys: Added to ensure unique keys before saving
  2. Removed UI-only field: Added before sending to backend to prevent schema/validation issues

Changes have been pushed to the branch. The PR should now be ready for review.

@localai-bot
Copy link
Contributor Author

I have addressed the Copilot review comments:

  1. Deduplicated API keys using Set before saving
  2. Removed api_keys_text (UI-only field) from payload before backend call

Changes have been pushed to the branch. The PR should now be ready for review.

@localai-bot localai-bot force-pushed the fix-settings-api-keys-dup branch 5 times, most recently from 98b603e to 06c1eee Compare March 20, 2026 07:45
@localai-bot
Copy link
Contributor Author

DCO Check Fixed

The DCO check was failing due to a mismatch between the committer email and Signed-off-by email. I've fixed this by:

  1. Rewriting all commits in the branch using WARNING: git-filter-branch has a glut of gotchas generating mangled history
    rewrites. Hit Ctrl-C before proceeding to abort, then use an
    alternative filtering tool such as 'git filter-repo'
    (https://github.com/newren/git-filter-repo/) instead. See the
    filter-branch manual page for more details; to squelch this warning,
    set FILTER_BRANCH_SQUELCH_WARNING=1.
    Proceeding with filter-branch...
  2. Setting the committer email to match the Signed-off-by email:
  3. Adding proper Signed-off-by lines to all commits

The DCO check is now passing. The branch has been force-pushed and the PR is updated automatically.

All other checks are passing. Ready for review!

localai-bot added 2 commits March 20, 2026 08:37
- Convert api_keys_text (textarea input) to api_keys array before saving
- Split text by newlines and trim each key to avoid duplicates
- Set api_keys to empty array when no keys are provided
- This fixes the issue where saving settings would duplicate API keys
  because the backend expects an array, not a text string





Signed-off-by: localai-bot <localai-bot@localai.io>
…e UI-only field

- Use Set to deduplicate API keys before saving
- Delete api_keys_text from payload before sending to backend
- Addresses feedback from Copilot review comments





Signed-off-by: localai-bot <localai-bot@localai.io>
@localai-bot localai-bot force-pushed the fix-settings-api-keys-dup branch from 06c1eee to e2ae3d2 Compare March 20, 2026 08:39
@localai-bot
Copy link
Contributor Author

Merge Conflict Resolution

I've resolved the merge conflicts by rebasing the branch on top of the latest upstream/master.

Actions Taken:

  1. Rebased the branch fix-settings-api-keys-dup on top of upstream/master
  2. All conflicts resolved automatically (no manual intervention needed)
  3. Force-pushed the rebased branch to the fork

Current Status:

  • Branch now includes all commits from upstream/master
  • Your changes (API key deduplication fix) are preserved on top of the latest code
  • Ready for review and merge

The PR should now show as mergeable. Please review and merge when ready!

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

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

2 participants