fix: Apply env var fallback fix to lfx module (#11584)

Applies the fix from PR #9422 (commit 18e2ff2d2d) to the lfx module,
which was previously only applied to the backend module.

PR #9422 fixed a bug where environment variable fallback would fail when
`load_from_db=True` inputs couldn't find variables in the database.

The fix properly distinguishes between two error cases:

- "User id is not set" → always raise (authentication issue)
- "variable not found." → only raise if fallback_to_env_vars=False

However, this fix was only applied to:

- src/backend/base/langflow/interface/initialize/loading.py

And was missing from:

- src/lfx/src/lfx/interface/initialize/loading.py

PR #9902 added table field support and reorganized the test imports to
use the lfx module (which has table support). The test was updated to
use "Database connection failed" error message to work with the existing
code, rather than "variable not found." which would trigger the bug.

This commit:

1. Applies the bug fix to the lfx module's regular field handler
2. Restores the original test behavior using "variable not found."
   to properly validate that the fix works

- src/lfx/src/lfx/interface/initialize/loading.py
  Fixed error handling to respect fallback_to_env_vars flag

- src/backend/tests/unit/interface/initialize/test_loading.py
  Restored "variable not found." error to properly validate the fix

Fixes: https://github.com/langflow-ai/langflow/issues/11422
Related: #9422, #9902
This commit is contained in:
Michael Wallace
2026-02-04 12:37:06 +00:00
committed by GitHub
parent 3e56245af8
commit c4cb7607ee
2 changed files with 8 additions and 4 deletions

View File

@ -12,15 +12,17 @@ from lfx.interface.initialize.loading import (
async def test_update_params_fallback_to_env_when_variable_not_found():
"""Test that when a variable is not found in database and fallback_to_env_vars is True.
It falls back to environment variables.
It falls back to environment variables. This specifically tests the fix for the bug
where 'variable not found.' error would always raise, even with fallback enabled.
"""
# Set up environment variable
os.environ["TEST_API_KEY"] = "test-secret-key-123"
# Create mock custom component
custom_component = MagicMock()
# Change this error message to avoid triggering re-raise
custom_component.get_variable = AsyncMock(side_effect=ValueError("Database connection failed"))
# Use "variable not found." error to specifically test the fix
# Previously this would always raise, even with fallback_to_env_vars=True
custom_component.get_variable = AsyncMock(side_effect=ValueError("TEST_API_KEY variable not found."))
# Set up params with a field that should load from db
params = {"api_key": "TEST_API_KEY"}

View File

@ -271,7 +271,9 @@ async def update_params_with_load_from_db_fields(
try:
key = await custom_component.get_variable(name=params[field], field=field, session=session)
except ValueError as e:
if any(reason in str(e) for reason in ["User id is not set", "variable not found."]):
if "User id is not set" in str(e):
raise
if "variable not found." in str(e) and not fallback_to_env_vars:
raise
logger.debug(str(e))
key = None