Skip to content

Conversation

@italojohnny
Copy link
Contributor

@italojohnny italojohnny commented Aug 22, 2025

Previously, the Agent components (Anthropic, Google, Groq) always reset
model_name.value to the first available model ID when refreshing the
list of options. This caused the user’s last selection to be lost
without consent.

This patch ensures that model_name.value is only set if it is missing,
using setdefault. As a result, the user’s choice is preserved across
updates while still defaulting to the first option when no value exists.

Summary by CodeRabbit

  • Bug Fixes
    • Preserves your previously selected model when updating configuration for Anthropic, Google Generative AI, and Groq providers; a default is only applied if no selection exists.
    • Prevents model dropdowns from resetting unexpectedly during reloads or edits.
    • Improves consistency across providers so user choices persist reliably.
    • Enhances overall stability of model selection in the UI.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 22, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Updates 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

Cohort / File(s) Summary of Changes
Provider model_name initialization and default preservation
src/backend/base/langflow/components/anthropic/anthropic.py, src/backend/base/langflow/components/google/google_generative_ai.py, src/backend/base/langflow/components/groq/groq.py
Ensure build_config.setdefault("model_name", {}) exists; assign options as before; change default assignment to build_config["model_name"].setdefault("value", ids[0]) to avoid overwriting existing selections; no API/signature changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

bug, size:S, lgtm

Suggested reviewers

  • edwinjosechittilappilly
  • ogabrielluiz
  • mfortman11
✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/preserve-model-selection

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Aug 22, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 options

To 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 when ids is 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 options

If 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 consistency

Since 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 ids

Preserve 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 type

Keep 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 8af1909 and 7363c24.

📒 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.py
  • src/backend/base/langflow/components/groq/groq.py
  • src/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.py
  • src/backend/base/langflow/components/groq/groq.py
  • src/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 icon attribute to a string matching the frontend icon mapping exactly (case-sensitive).

Files:

  • src/backend/base/langflow/components/google/google_generative_ai.py
  • src/backend/base/langflow/components/groq/groq.py
  • src/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.value

Using 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 intent

The 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 selection

Using setdefault to initialize the "model_name" block and its "value" preserves existing user choice across refreshes. Matches the pattern used in other providers.

Comment on lines +180 to 183
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
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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.

Comment on lines +148 to 151
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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.

Comment on lines +117 to 120
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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.

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Aug 22, 2025
@sonarqubecloud
Copy link

Copy link
Collaborator

@edwinjosechittilappilly edwinjosechittilappilly left a comment

Choose a reason for hiding this comment

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

LGTM.

@github-actions github-actions bot added the lgtm This PR has been approved by a maintainer label Aug 22, 2025
@italojohnny italojohnny added this pull request to the merge queue Aug 22, 2025
Merged via the queue into main with commit 2f06fa9 Aug 22, 2025
20 checks passed
@italojohnny italojohnny deleted the fix/preserve-model-selection branch August 22, 2025 16:59
@Empreiteiro Empreiteiro removed the request for review from Victor-w-Madeira August 22, 2025 19:28
@Empreiteiro
Copy link
Collaborator

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
Google AI Component
Agent with Anthropic
Agent with Google AI

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

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants