feat: Add support for passing env vars to run command#10986
feat: Add support for passing env vars to run command#10986edwinjosechittilappilly merged 50 commits intomainfrom
Conversation
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.
|
Note Other AI code review bot(s) detectedCodeRabbit 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. WalkthroughThis PR introduces CLI support for passing global variables to graph execution via a new repeatable Changes
Sequence DiagramsequenceDiagram
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
ogabrielluiz
left a comment
There was a problem hiding this comment.
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.
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. 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. |
Wait.. but langflow assistant can just import the functions and run the graphs directly, no? why use |
There was a problem hiding this comment.
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-varoption 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 |
There was a problem hiding this comment.
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.
| try: | ||
| with temporary_sys_path(str(script_path.parent)): | ||
| spec.loader.exec_module(module) | ||
| except Exception: |
There was a problem hiding this comment.
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.
| except Exception: | |
| except (ImportError, AttributeError, ModuleNotFoundError, SyntaxError): |
| pass | ||
| import os | ||
|
|
||
| if not os.getenv("LFX_TEST_ALLOW_LANGFLOW"): |
There was a problem hiding this comment.
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.
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.
* 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>
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:
load_from_env_varsfunction and related code now support overriding environment variables using arequest_variablescontext, allowing for more flexible and testable variable injection when running flows. [1] [2] [3] [4]Testing and integration enhancements:
run_flowfunction insrc/lfx/tests/unit/run/test_base_integration.py, covering real graph execution, environment variable injection, error handling, and utility functions for test graph creation.Core module improvements:
__init__.pyfile to thesrc/lfx/src/lfx/runpackage, exposingRunErrorandrun_flowfor easier imports and clearer API boundaries.Dynamic script loading reliability:
script_loader.pyby registering dynamically loaded scripts insys.modulesunder their filename stem, ensuring Python'sinspectmodule can correctly resolve them. This also adds cleanup logic on exceptions.Test infrastructure and logging:
structlogat 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.