langflow-ai/langflow · error · ValueError

Settings service is already initialized. This indicates pote

Error message

Settings service is already initialized. This indicates potential race conditions with settings initialization. Ensure the settings service is not created during module loading.

What it means

When 'langflow run --env-file <path>' is passed, the CLI must load the dotenv file before the settings service reads configuration. If the settings service is already initialized at that point (some import touched it during module load), the CLI raises ValueError because applying the env file now would be too late and silently ignored. It is a race-condition guard that makes 'my .env had no effect' impossible to miss.

Source

Thrown at src/backend/base/langflow/__main__.py:415

        None,
        help="Defines the polling interval for the webhook.",
        show_default=False,
    ),
    ssl_cert_file_path: str | None = typer.Option(
        None, help="Defines the SSL certificate file path.", show_default=False
    ),
    ssl_key_file_path: str | None = typer.Option(None, help="Defines the SSL key file path.", show_default=False),
) -> None:
    """Run Langflow."""
    if env_file:
        if is_settings_service_initialized():
            err = (
                "Settings service is already initialized. This indicates potential race conditions "
                "with settings initialization. Ensure the settings service is not created during "
                "module loading."
            )
            # i.e. ensures the env file is loaded before the settings service is initialized
            raise ValueError(err)
        load_dotenv(env_file, override=True)

    # Set and normalize log level, with precedence: cli > env > default
    log_level = (log_level or os.environ.get("LANGFLOW_LOG_LEVEL") or "info").lower()
    os.environ["LANGFLOW_LOG_LEVEL"] = log_level

    configure(log_level=log_level, log_file=log_file, log_rotation=log_rotation)

    # Create progress indicator (show verbose timing if log level is DEBUG)
    verbose = log_level == "debug"
    progress = create_langflow_progress(verbose=verbose)

    # Step 0: Initializing Langflow
    with progress.step(0):
        logger.debug(f"Loading config from file: '{env_file}'" if env_file else "No env_file provided.")
        set_var_for_macos_issue()
        settings_service = get_settings_service()

View on GitHub (pinned to 976ec789d2)

Solutions

  1. Remove module-level get_settings_service() calls from your own code — defer settings access into functions.
  2. Load the env file yourself before anything imports langflow: call load_dotenv(path, override=True) at the top of your entry script, or export the variables into the process environment.
  3. If a third-party import is the culprit, import it lazily inside the run path instead of at module top.

Example fix

# before: module-level settings access forces early init
from langflow.services.settings.utils import get_settings_service
SETTINGS = get_settings_service()  # then `langflow run --env-file .env` fails
# after: defer
from langflow.services.settings.utils import get_settings_service
def settings():
    return get_settings_service().settings
Defensive patterns

Strategy: validation

Validate before calling

# at the very top of your custom entry point, BEFORE importing langflow modules:
from dotenv import load_dotenv
load_dotenv(".env", override=True)
import langflow  # now safe: env is loaded before settings init

Prevention

When it happens

Trigger: Passing --env-file while an imported module already called get_settings_service() at import time; running the CLI through a wrapper that imports langflow components before invoking run; plugins or conftest code initializing settings early.

Common situations: Custom entry points that 'import langflow' (triggering settings creation) before delegating to the CLI; test harnesses reusing the process; third-party code calling get_settings_service() at module scope.

Related errors


AI-assisted analysis of langflow-ai/langflow@976ec789d2 (2026-08-14). Data as JSON: /api/errors/6613b2c5bc86aff2. Report an issue: GitHub.