{"record":{"id":"76cc1e61ea80bf10","repo":"langflow-ai/langflow","slug":"error-loading-flow-module-e","errorCode":null,"errorMessage":"Error loading flow module: {e}","messagePattern":"Error loading flow module: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"src/backend/base/langflow/agentic/services/helpers/flow_loader.py","lineNumber":149,"sourceCode":"    Raises:\n        HTTPException: If the flow file cannot be loaded or executed.\n    \"\"\"\n    module_name = flow_path.stem\n    spec = importlib.util.spec_from_file_location(module_name, flow_path)\n    if spec is None or spec.loader is None:\n        raise HTTPException(status_code=500, detail=f\"Could not load flow module: {flow_path}\")\n\n    module = importlib.util.module_from_spec(spec)\n    sys.modules[module_name] = module\n\n    try:\n        with _temporary_sys_path(str(flow_path.parent)):\n            spec.loader.exec_module(module)\n    except Exception as e:\n        if module_name in sys.modules:\n            del sys.modules[module_name]\n        logger.error(f\"Error loading Python flow module: {e}\")\n        raise HTTPException(status_code=500, detail=f\"Error loading flow module: {e}\") from e\n\n    if not hasattr(module, \"get_graph\"):\n        # Fallback: check for 'graph' variable for backward compatibility\n        if hasattr(module, \"graph\"):\n            graph = module.graph\n            validate_flow_for_current_settings(graph)\n            if module_name in sys.modules:\n                del sys.modules[module_name]\n            return graph\n        if module_name in sys.modules:\n            del sys.modules[module_name]\n        raise HTTPException(status_code=500, detail=f\"Flow module must define 'get_graph()' function: {flow_path}\")\n\n    get_graph_func = module.get_graph\n\n    # Build kwargs for get_graph based on what it accepts\n    sig = inspect.signature(get_graph_func)\n    kwargs = {}","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/langflow-ai/langflow/blob/976ec789d2886a86de109c044d089d68e96c9a35/src/backend/base/langflow/agentic/services/helpers/flow_loader.py#L131-L167","documentation":"Raised when spec.loader.exec_module(module) throws while executing the .py flow file — i.e. the file loaded but running its top-level code failed (syntax error at exec time, ImportError of a missing dependency, exception at module level). The module entry is cleaned out of sys.modules, the exception is logged ('Error loading flow module: {e}'), and HTTP 500 with the exception text is returned.","triggerScenarios":"A Python flow file that imports a package not installed in the server environment, raises at import time (e.g. reads a missing env var at module scope), or contains a SyntaxError that only surfaces on execution; the file's parent dir is temporarily added to sys.path during exec, so relative imports of sibling modules can also fail.","commonSituations":"Flow depends on a library (requests, langchain community packages, etc.) missing from the server venv; module-level code assuming environment variables or files; flow developed against a different Langflow version whose APIs moved; syntax valid for a newer Python than the server runs.","solutions":["Read the 500 detail — it contains the actual exception (e.g. ModuleNotFoundError: No module named 'x') — and fix that import or add the dependency to the server environment.","Move environment-dependent logic inside get_graph() instead of module top-level so loading never depends on runtime state.","Run 'python <flow>.py' in the server environment locally to reproduce the exec failure quickly.","Keep the flow file dependency-free except for langflow/lfx APIs where possible."],"exampleFix":"# before (module-level, breaks load if var missing)\nimport os\nKEY = os.environ['MY_KEY']\ndef get_graph(): ...\n# after (lazy, load always succeeds)\nimport os\ndef get_graph():\n    key = os.environ.get('MY_KEY', '')\n    ...","handlingStrategy":"try-catch","validationCode":"subprocess.run(['python', '-m', 'py_compile', str(flow_path)], check=True)  # catches syntax errors pre-deploy\n# plus: import the module in a throwaway venv matching the server env to catch ImportErrors","typeGuard":null,"tryCatchPattern":"except HTTPException as e:\n    if e.status_code == 500 and 'Error loading flow module' in e.detail:\n        show_author_error(e.detail)  # contains the real exception, e.g. ModuleNotFoundError\n        block_deployment()","preventionTips":["Keep flow files free of third-party imports beyond langflow/lfx; put env-dependent logic inside get_graph(), not at module top level.","Compile-check and import-check flow files in CI against the same dependency set as the server.","Pin the Python version used to author flows to the server's version."],"tags":["agentic","flow-loader","import","http-500","python-flow","dependencies"],"backgroundTag":null,"analyzedSha":"976ec789d2886a86de109c044d089d68e96c9a35","analyzedAt":"2026-08-14T18:23:12.227Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}