-
Notifications
You must be signed in to change notification settings - Fork 8.2k
fix: preserve user-selected model when updating available options #9495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughUpdates three provider components to initialize build_config["model_name"] with setdefault and preserve any existing model_name.value by using setdefault("value", ids[0]) instead of overwriting. Options (ids) and combobox flags remain unchanged; no public signatures altered. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
✨ Finishing Touches🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (5)
src/backend/base/langflow/components/groq/groq.py (1)
117-120: Consider preserving the user’s previous model selection across refreshed dropdown optionsTo improve UX consistency, if the current selection isn’t returned in
ids, avoid showing an empty or invalid choice. Instead, prepend the existing value to the new options (or retain the prior options whenidsis empty). This pattern appears in three components:
- src/backend/base/langflow/components/groq/groq.py (lines 117–120)
- src/backend/base/langflow/components/google/google_generative_ai.py (lines 148–151)
- src/backend/base/langflow/components/anthropic/anthropic.py (lines 180–182)
Apply the following more resilient variant at each location:
- build_config.setdefault("model_name", {}) - build_config["model_name"]["options"] = ids - build_config["model_name"].setdefault("value", ids[0]) + model_cfg = build_config.setdefault("model_name", {}) + current_value = model_cfg.get("value") + if ids: + # If the current value isn’t in the refreshed list, keep it visible. + model_cfg["options"] = ( + [current_value] + [m for m in ids if m != current_value] + if current_value and current_value not in ids + else ids + ) + model_cfg.setdefault("value", current_value or ids[0]) + else: + # On transient failures, preserve the last-known options. + model_cfg["options"] = model_cfg.get("options", [])This change ensures dropdowns never become stale or empty, enhancing robustness across all model‐selection components.
src/backend/base/langflow/components/anthropic/anthropic.py (2)
180-183: Keep UX consistent when selected model is missing from refreshed optionsIf the current value isn’t in ids, prepend it to options so the UI doesn’t display a selection not present in the list. Also avoid clearing options on empty ids.
Apply this improved logic:
- build_config.setdefault("model_name", {}) - build_config["model_name"]["options"] = ids - build_config["model_name"].setdefault("value", ids[0]) + model_cfg = build_config.setdefault("model_name", {}) + current_value = model_cfg.get("value") + if ids: + model_cfg["options"] = ( + [current_value] + [m for m in ids if m != current_value] + if current_value and current_value not in ids + else ids + ) + model_cfg.setdefault("value", current_value or ids[0]) + else: + model_cfg["options"] = model_cfg.get("options", [])
180-181: Nit: default nested structure to dotdict for consistencySince build_config is a dotdict, initializing the nested "model_name" with dotdict keeps shape consistent.
Apply this small tweak:
- build_config.setdefault("model_name", {}) + build_config.setdefault("model_name", dotdict({}))src/backend/base/langflow/components/google/google_generative_ai.py (2)
148-151: Optional: handle stale selection not present in refreshed idsPreserve the current selection visually and functionally when it drops from the returned list; avoid wiping options when none are returned.
Proposed improvement:
- build_config.setdefault("model_name", {}) - build_config["model_name"]["options"] = ids - build_config["model_name"].setdefault("value", ids[0]) + model_cfg = build_config.setdefault("model_name", {}) + current_value = model_cfg.get("value") + if ids: + model_cfg["options"] = ( + [current_value] + [m for m in ids if m != current_value] + if current_value and current_value not in ids + else ids + ) + model_cfg.setdefault("value", current_value or ids[0]) + else: + model_cfg["options"] = model_cfg.get("options", [])
148-149: Nit: use dotdict for nested default to match parent typeKeep nested structure consistent with build_config type.
Apply:
- build_config.setdefault("model_name", {}) + build_config.setdefault("model_name", dotdict({}))
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
src/backend/base/langflow/components/anthropic/anthropic.py(1 hunks)src/backend/base/langflow/components/google/google_generative_ai.py(1 hunks)src/backend/base/langflow/components/groq/groq.py(1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/backend/base/langflow/components/**/*.py
📄 CodeRabbit inference engine (.cursor/rules/backend_development.mdc)
src/backend/base/langflow/components/**/*.py: Add new backend components to the appropriate subdirectory under src/backend/base/langflow/components/
Implement async component methods using async def and await for asynchronous operations
Use asyncio.create_task for background work in async components and ensure proper cleanup on cancellation
Use asyncio.Queue for non-blocking queue operations in async components and handle timeouts appropriately
Files:
src/backend/base/langflow/components/google/google_generative_ai.pysrc/backend/base/langflow/components/groq/groq.pysrc/backend/base/langflow/components/anthropic/anthropic.py
{src/backend/**/*.py,tests/**/*.py,Makefile}
📄 CodeRabbit inference engine (.cursor/rules/backend_development.mdc)
{src/backend/**/*.py,tests/**/*.py,Makefile}: Run make format_backend to format Python code before linting or committing changes
Run make lint to perform linting checks on backend Python code
Files:
src/backend/base/langflow/components/google/google_generative_ai.pysrc/backend/base/langflow/components/groq/groq.pysrc/backend/base/langflow/components/anthropic/anthropic.py
src/backend/**/components/**/*.py
📄 CodeRabbit inference engine (.cursor/rules/icons.mdc)
In your Python component class, set the
iconattribute to a string matching the frontend icon mapping exactly (case-sensitive).
Files:
src/backend/base/langflow/components/google/google_generative_ai.pysrc/backend/base/langflow/components/groq/groq.pysrc/backend/base/langflow/components/anthropic/anthropic.py
🔇 Additional comments (3)
src/backend/base/langflow/components/groq/groq.py (1)
117-120: Good fix: setdefault preserves user-selected model_name.valueUsing build_config.setdefault("model_name", {}) and setdefault("value", ...) prevents clobbering a previously selected model on refresh. This aligns with the PR goal.
src/backend/base/langflow/components/anthropic/anthropic.py (1)
180-183: Preserving user selection with setdefault — aligns with PR intentThe switch to setdefault avoids overwriting a previously chosen model while still defaulting on first-time setup. This is consistent with the other providers.
src/backend/base/langflow/components/google/google_generative_ai.py (1)
148-151: Correct approach to preserve model selectionUsing setdefault to initialize the "model_name" block and its "value" preserves existing user choice across refreshes. Matches the pattern used in other providers.
| build_config.setdefault("model_name", {}) | ||
| build_config["model_name"]["options"] = ids | ||
| build_config["model_name"]["value"] = ids[0] | ||
| build_config["model_name"].setdefault("value", ids[0]) | ||
| build_config["model_name"]["combobox"] = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent IndexError when no models are returned
ids can be empty after filtering tool-capable models or due to API/listing issues. Accessing ids[0] will raise IndexError. Guard it.
Apply this diff:
- build_config.setdefault("model_name", {})
- build_config["model_name"]["options"] = ids
- build_config["model_name"].setdefault("value", ids[0])
+ model_cfg = build_config.setdefault("model_name", {})
+ model_cfg["options"] = ids
+ if ids:
+ model_cfg.setdefault("value", ids[0])📝 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.
| build_config.setdefault("model_name", {}) | |
| build_config["model_name"]["options"] = ids | |
| build_config["model_name"]["value"] = ids[0] | |
| build_config["model_name"].setdefault("value", ids[0]) | |
| build_config["model_name"]["combobox"] = True | |
| model_cfg = build_config.setdefault("model_name", {}) | |
| model_cfg["options"] = ids | |
| if ids: | |
| model_cfg.setdefault("value", ids[0]) | |
| build_config["model_name"]["combobox"] = True |
🤖 Prompt for AI Agents
In src/backend/base/langflow/components/anthropic/anthropic.py around lines 180
to 183, the code unconditionally does
build_config["model_name"].setdefault("value", ids[0]) which will raise
IndexError if ids is empty; change this to first set
build_config["model_name"]["options"] = ids, then set the default value to
ids[0] only when ids is non-empty otherwise set a safe fallback (e.g. an empty
string or None) and keep combobox behavior; in short, guard the access to ids[0]
with an if ids: ... else: ... and assign a sensible default value when ids is
empty.
| build_config.setdefault("model_name", {}) | ||
| build_config["model_name"]["options"] = ids | ||
| build_config["model_name"]["value"] = ids[0] | ||
| build_config["model_name"].setdefault("value", ids[0]) | ||
| except Exception as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid IndexError when ids is empty
ids[0] will throw if the list is empty (e.g., transient API issues or filtering). Add a guard.
Apply this diff:
- build_config.setdefault("model_name", {})
- build_config["model_name"]["options"] = ids
- build_config["model_name"].setdefault("value", ids[0])
+ model_cfg = build_config.setdefault("model_name", {})
+ model_cfg["options"] = ids
+ if ids:
+ model_cfg.setdefault("value", ids[0])📝 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.
| build_config.setdefault("model_name", {}) | |
| build_config["model_name"]["options"] = ids | |
| build_config["model_name"]["value"] = ids[0] | |
| build_config["model_name"].setdefault("value", ids[0]) | |
| except Exception as e: | |
| model_cfg = build_config.setdefault("model_name", {}) | |
| model_cfg["options"] = ids | |
| if ids: | |
| model_cfg.setdefault("value", ids[0]) |
🤖 Prompt for AI Agents
In src/backend/base/langflow/components/google/google_generative_ai.py around
lines 148-151, the code accesses ids[0] without checking that ids is non-empty
which can raise IndexError; ensure you guard against an empty ids list by
checking if ids and setting build_config["model_name"]["value"] only when ids
has at least one element, otherwise set a safe fallback (e.g., None or an empty
string) or omit the "value" key and optionally log a warning/error so transient
API or filtering yielding an empty list does not crash the process.
| build_config.setdefault("model_name", {}) | ||
| build_config["model_name"]["options"] = ids | ||
| build_config["model_name"]["value"] = ids[0] | ||
| build_config["model_name"].setdefault("value", ids[0]) | ||
| except Exception as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard against empty ids to avoid IndexError
ids can be empty (e.g., transient API failure or aggressive filtering when tool_model_enabled is True). Accessing ids[0] will raise IndexError even inside setdefault. Add a simple guard.
Apply this diff:
- build_config.setdefault("model_name", {})
- build_config["model_name"]["options"] = ids
- build_config["model_name"].setdefault("value", ids[0])
+ model_cfg = build_config.setdefault("model_name", {})
+ model_cfg["options"] = ids
+ if ids:
+ model_cfg.setdefault("value", ids[0])📝 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.
| build_config.setdefault("model_name", {}) | |
| build_config["model_name"]["options"] = ids | |
| build_config["model_name"]["value"] = ids[0] | |
| build_config["model_name"].setdefault("value", ids[0]) | |
| except Exception as e: | |
| model_cfg = build_config.setdefault("model_name", {}) | |
| model_cfg["options"] = ids | |
| if ids: | |
| model_cfg.setdefault("value", ids[0]) | |
| except Exception as e: |
🤖 Prompt for AI Agents
In src/backend/base/langflow/components/groq/groq.py around lines 117 to 120,
the code unconditionally sets build_config["model_name"]["options"] = ids and
build_config["model_name"].setdefault("value", ids[0]) which will raise
IndexError when ids is empty; add a guard that checks if ids is truthy before
setting "options" and using ids[0] — if ids is empty, set "options" to an empty
list (or omit it) and set "value" to None or a sensible default, or skip those
assignments entirely; ensure this check is inside the same try/except block so
transient failures won't cause an IndexError.
|
edwinjosechittilappilly
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
|
I couldn't test it before the merge, but I tested today's main build and the components worked as expected. @italojohnny, thanks for the solution! Many users have been waiting for this, and now we finally have a working model selection. Tests performed with Selection List Models and Custom Model: Anthropic Component |



Previously, the Agent components (Anthropic, Google, Groq) always reset
model_name.valueto the first available model ID when refreshing thelist of options. This caused the user’s last selection to be lost
without consent.
This patch ensures that
model_name.valueis only set if it is missing,using
setdefault. As a result, the user’s choice is preserved acrossupdates while still defaulting to the first option when no value exists.
Summary by CodeRabbit