Skip to content

feat: Add support for passing env vars to run command#10986

Merged
edwinjosechittilappilly merged 50 commits intomainfrom
feat-variable-passthrough-lfx
Dec 17, 2025
Merged

feat: Add support for passing env vars to run command#10986
edwinjosechittilappilly merged 50 commits intomainfrom
feat-variable-passthrough-lfx

Conversation

@edwinjosechittilappilly
Copy link
Collaborator

@edwinjosechittilappilly edwinjosechittilappilly commented Dec 12, 2025

This pull request introduces several improvements and new features to the Langflow execution and testing infrastructure. The main changes include enhanced environment variable handling for graph execution, the addition of integration tests for the run module, improved dynamic script loading, and better logging setup for tests.

Environment variable handling improvements:

  • The load_from_env_vars function and related code now support overriding environment variables using a request_variables context, allowing for more flexible and testable variable injection when running flows. [1] [2] [3] [4]

Testing and integration enhancements:

  • Added a comprehensive suite of integration tests for the run_flow function in src/lfx/tests/unit/run/test_base_integration.py, covering real graph execution, environment variable injection, error handling, and utility functions for test graph creation.
  • Introduced a module docstring for the test package and the run module test directory for clarity and documentation.

Core module improvements:

  • Added an __init__.py file to the src/lfx/src/lfx/run package, exposing RunError and run_flow for easier imports and clearer API boundaries.

Dynamic script loading reliability:

  • Improved module loading in script_loader.py by registering dynamically loaded scripts in sys.modules under their filename stem, ensuring Python's inspect module can correctly resolve them. This also adds cleanup logic on exceptions.

Test infrastructure and logging:

  • Configured structlog at the session level in test setup to ensure consistent and reliable logging during all tests, preventing logger-related errors when mocking or capturing logs.

These changes collectively improve the reliability, testability, and maintainability of the Langflow execution system and its test suite.

The run CLI command now accepts --env-var KEY=VALUE options to inject global variables into the graph context. Includes validation for variable format and unit tests to verify correct injection and error handling. Also improves script loading to register modules by script name for better inspection.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 12, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

This PR introduces CLI support for passing global variables to graph execution via a new repeatable --env-var option, and modifies the loading system to prioritize context-based variables over environment variables before falling back to OS-level lookups.

Changes

Cohort / File(s) Summary
CLI Environment Variable Support
src/lfx/src/lfx/cli/run.py
Added --env-var repeatable CLI option to accept KEY=VALUE pairs, parses and validates format, injects parsed variables into graph.context.request_variables after graph loading, and logs injected keys when verbosity is enabled.
Script Loading Module Naming
src/lfx/src/lfx/cli/script_loader.py
Changed module identification strategy to use script filename (stem) as Python module name instead of fixed "script_module"; registers module in sys.modules before execution and cleans up on error to prevent stale module leaks.
Context-Aware Variable Lookup
src/lfx/src/lfx/interface/initialize/loading.py
Modified load_from_env_vars and update_table_params_with_load_from_db_fields to check graph.context.request_variables for contextual variable overrides before falling back to os.getenv; updated function signatures to accept optional custom_component parameter.
Test Infrastructure & Environment Variable Tests
src/lfx/tests/conftest.py, src/lfx/tests/unit/cli/test_run_env_var.py
Added conditional langflow presence check guarded by LFX_TEST_ALLOW_LANGFLOW environment variable; introduced comprehensive new test suite validating CLI --env-var injection, multiple variable handling, invalid format rejection, and precedence behavior (CLI vars override OS environment).

Sequence Diagram

sequenceDiagram
    participant User
    participant CLI as CLI (run.py)
    participant Graph as Graph<br/>Context
    participant Loader as Loading<br/>Interface
    participant Component as Custom<br/>Component
    participant OS as OS<br/>Environment

    User->>CLI: --env-var KEY1=VAL1<br/>--env-var KEY2=VAL2
    CLI->>CLI: Parse & validate<br/>env_var format
    CLI->>Graph: Load graph<br/>(script/JSON)
    CLI->>Graph: Inject parsed vars into<br/>graph.context<br/>.request_variables
    
    rect rgb(220, 240, 250)
    Note over Component,OS: Variable Lookup with Context Precedence
    Loader->>Component: load_from_env_vars()<br/>with custom_component
    Component->>Graph: Check context<br/>.request_variables
    alt Variable found in context
        Graph-->>Component: Return context var
    else Variable not in context
        Component->>OS: Fallback to os.getenv()
        OS-->>Component: Return OS env var
    end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • run.py: New parameter parsing logic with validation and error handling requires careful review of format validation and injection behavior
  • loading.py: Multiple function signature changes with context-aware lookup fallback pattern applied across three functions; ensure precedence logic is consistent
  • script_loader.py: Module naming strategy change and sys.modules lifecycle management; verify cleanup on error paths
  • test_run_env_var.py: New comprehensive test suite with multiple scenarios; validate fixture setup and test coverage of edge cases

Possibly related PRs

Suggested labels

lgtm

Suggested reviewers

  • jordanrfrazier

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Test Quality And Coverage ⚠️ Warning Test coverage is incomplete for new implementations. CLI env-var functionality has integration tests, but script_loader.py changes, loading.py modifications, and validate_env_var function lack unit test coverage. Add unit tests for validate_env_var function including edge cases (empty values, missing equals, multiple equals). Create test files for script_loader.py module registration changes and loading.py context override logic with various custom_component scenarios.
Excessive Mock Usage Warning ❓ Inconclusive The PR summary promises test_run_env_var.py with integration-style tests for --env-var feature, but this file does not exist in the repository. Existing test_run.py shows moderate mock usage (2.25 patterns per test) with appropriate CLI framework isolation, but the new feature's test implementation cannot be evaluated without the missing test file. Examine the promised test_run_env_var.py file to assess whether new tests for the --env-var feature use excessive mocking or employ real integration-style tests with actual Python script fixtures and graph context injection validation.
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.
Test Coverage For New Implementations ✅ Passed PR includes new test file test_run_env_var.py that comprehensively tests the --env-var CLI feature with multiple scenarios including env var injection, invalid format handling, precedence, fallback, and output validation.
Test File Naming And Structure ✅ Passed Test files follow correct patterns: test_run_env_var.py uses test_*.py naming, proper directory structure (tests/unit/cli/), class-based organization with TestRunEnvVar, 6 descriptive test methods covering positive/negative scenarios, edge cases, and error conditions with proper assertions and pytest fixtures.
Title check ✅ Passed The title accurately describes the main feature addition: support for passing environment variables to the run command via a new --env-var CLI option.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-variable-passthrough-lfx

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@edwinjosechittilappilly edwinjosechittilappilly changed the title Add support for passing env vars to run command feat:Add support for passing env vars to run command Dec 12, 2025
@github-actions github-actions bot added the enhancement New feature or request label Dec 12, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Dec 12, 2025
Copy link
Contributor

@ogabrielluiz ogabrielluiz left a comment

Choose a reason for hiding this comment

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

Won't this risk leaking the variables? Why not use the .env file?

Updated environment variable loading logic to prioritize CLI-provided variables (request_variables in context) over OS environment variables for load_from_db fields. Added unit tests to verify correct precedence and fallback behavior.
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Dec 12, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Dec 12, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Dec 12, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Dec 12, 2025
@edwinjosechittilappilly
Copy link
Collaborator Author

Won't this risk leaking the variables? Why not use the .env file?

ENV works!

this is used for a method similar to the global variable pass through we have for the Run end point.

I am planning to use run function from LFX to run the agentic flows for Langflow assistant programatically so the global variable passthrough helps for it.

Even with Global variable passthrough the ENV works.
So if the Global variable passthrough is given it over rided the env.

example are dynamic variables that we cannot set as ENV but will change with respect to flows.

Ideally it should not leak the variables its just an alternative way to override the env.

@edwinjosechittilappilly edwinjosechittilappilly marked this pull request as ready for review December 12, 2025 17:36
Copilot AI review requested due to automatic review settings December 12, 2025 17:36
@ogabrielluiz
Copy link
Contributor

Ideally it should not leak the variables its just an alternative way to override the env.

Wait.. but langflow assistant can just import the functions and run the graphs directly, no? why use lfx run?

Copy link
Contributor

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

This PR adds support for passing environment variables to the run CLI command via --env-var KEY=VALUE options. These variables are injected into the graph context and take precedence over OS environment variables for load_from_db fields.

Key changes:

  • Added --env-var option to the run command for passing global variables
  • Enhanced variable loading logic to check graph context before falling back to OS environment variables
  • Improved script loading to register modules by their script name for better inspection

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/lfx/src/lfx/cli/run.py Adds --env-var CLI option, parses KEY=VALUE pairs, and injects them into graph context
src/lfx/src/lfx/interface/initialize/loading.py Updates variable loading functions to check request_variables in graph context before OS environment
src/lfx/src/lfx/cli/script_loader.py Changes module loading to use script name and registers it in sys.modules for inspection
src/lfx/tests/unit/cli/test_run_env_var.py Adds comprehensive unit tests for environment variable functionality
src/lfx/tests/conftest.py Adds environment variable to optionally allow langflow during tests

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

from lfx.schema.data import Data
from lfx.services.deps import get_settings_service, session_scope
from lfx.services.session import NoopSession
from pydantic import PydanticDeprecatedSince20
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The import for PydanticDeprecatedSince20 was moved from line 9 to line 16, but imports should be organized according to PEP 8 guidelines (standard library, third-party, then local imports). This import belongs with other third-party imports near the top, not after local imports.

Copilot uses AI. Check for mistakes.
try:
with temporary_sys_path(str(script_path.parent)):
spec.loader.exec_module(module)
except Exception:
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The bare except Exception clause is too broad and could catch unexpected errors. Consider catching specific exceptions (like ImportError, SyntaxError, or AttributeError) that would indicate the module failed to load properly, while allowing system-level exceptions to propagate normally.

Suggested change
except Exception:
except (ImportError, AttributeError, ModuleNotFoundError, SyntaxError):

Copilot uses AI. Check for mistakes.
pass
import os

if not os.getenv("LFX_TEST_ALLOW_LANGFLOW"):
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The new environment variable LFX_TEST_ALLOW_LANGFLOW is not documented. Consider adding a comment explaining its purpose and when it should be set, or updating the error message to mention this override option.

Copilot uses AI. Check for mistakes.
@Adam-Aghili Adam-Aghili changed the title feat:Add support for passing env vars to run command feat: Add support for passing env vars to run command Dec 17, 2025
@edwinjosechittilappilly edwinjosechittilappilly added lgtm This PR has been approved by a maintainer and removed lgtm This PR has been approved by a maintainer labels Dec 17, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Dec 17, 2025
@edwinjosechittilappilly edwinjosechittilappilly added this pull request to the merge queue Dec 17, 2025
Merged via the queue into main with commit 841a133 Dec 17, 2025
108 of 115 checks passed
@edwinjosechittilappilly edwinjosechittilappilly deleted the feat-variable-passthrough-lfx branch December 17, 2025 17:44
Wallgau pushed a commit that referenced this pull request Dec 18, 2025
* Add support for passing env vars to run command

The run CLI command now accepts --env-var KEY=VALUE options to inject global variables into the graph context. Includes validation for variable format and unit tests to verify correct injection and error handling. Also improves script loading to register modules by script name for better inspection.

* [autofix.ci] apply automated fixes

* Support CLI env var precedence over OS env vars

Updated environment variable loading logic to prioritize CLI-provided variables (request_variables in context) over OS environment variables for load_from_db fields. Added unit tests to verify correct precedence and fallback behavior.

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* fix ruff

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* Refactor run CLI to delegate execution to run module

Refactors the run CLI command to delegate execution logic to a new lfx.run.base module, improving separation of concerns and maintainability. Moves core run logic and error handling into lfx.run.base, introduces a RunError exception, and updates tests to target the new structure. Removes the --env-var option from the CLI and migrates related tests to the run module.

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* Update src/lfx/src/lfx/interface/initialize/loading.py

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>

* Update src/lfx/src/lfx/interface/initialize/loading.py

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* Update loading.py

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* Reorder import of PydanticDeprecatedSince20

Moved the import of PydanticDeprecatedSince20 to group it with other pydantic imports, improving code organization.

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* fix ruff

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* Update test_base.py

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* Fix structlog setup and logger patching in tests

Add a pytest fixture to configure structlog before tests to prevent AttributeError when mocking logger.configure. Update unit tests to patch the actual logger module using sys.modules, ensuring correct patching and avoiding issues with module references.

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants