Skip to content

configuration

langroid/utils/configuration.py

SettingsProxy

A proxy for the settings that returns a thread‐local override if set, or else falls back to the global settings.

update_global_settings(cfg, keys)

Update global settings so that modules can later access them via, e.g.,

from langroid.utils.configuration import settings
if settings.debug: ...

This updates the global default.

Source code in langroid/utils/configuration.py
def update_global_settings(cfg: BaseSettings, keys: List[str]) -> None:
    """
    Update global settings so that modules can later access them via, e.g.,

        from langroid.utils.configuration import settings
        if settings.debug: ...

    This updates the global default.
    """
    config_dict = cfg.dict()
    filtered_config = {key: config_dict[key] for key in keys if key in config_dict}
    new_settings = Settings(**filtered_config)
    _global_settings.__dict__.update(new_settings.__dict__)

set_global(key_vals)

Update the global settings object.

Source code in langroid/utils/configuration.py
def set_global(key_vals: Settings) -> None:
    """
    Update the global settings object.
    """
    _global_settings.__dict__.update(key_vals.__dict__)

temporary_settings(temp_settings)

Temporarily override the settings for the calling thread.

Within the context, any access to "settings" will use the provided temporary settings. Once the context is exited, the thread reverts to the global settings.

Source code in langroid/utils/configuration.py
@contextmanager
def temporary_settings(temp_settings: Settings) -> Iterator[None]:
    """
    Temporarily override the settings for the calling thread.

    Within the context, any access to "settings" will use the provided temporary
    settings. Once the context is exited, the thread reverts to the global settings.
    """
    saved = getattr(_thread_local, "override", None)
    _thread_local.override = temp_settings
    try:
        yield
    finally:
        if saved is not None:
            _thread_local.override = saved
        else:
            del _thread_local.override

quiet_mode(quiet=True)

Temporarily override settings.quiet for the current thread. This implementation builds on the thread‑local temporary_settings context manager. The effective quiet mode is merged: if quiet is already True (from an outer context), then it remains True even if a nested context passes quiet=False.

Source code in langroid/utils/configuration.py
@contextmanager
def quiet_mode(quiet: bool = True) -> Iterator[None]:
    """
    Temporarily override settings.quiet for the current thread.
    This implementation builds on the thread‑local temporary_settings context manager.
    The effective quiet mode is merged:
    if quiet is already True (from an outer context),
    then it remains True even if a nested context passes quiet=False.
    """
    current_effective = settings.dict()  # get the current thread's effective settings
    # Create a new settings instance from the current effective state.
    temp = Settings(**current_effective)
    # Merge the new flag: once quiet is enabled, it stays enabled.
    temp.quiet = settings.quiet or quiet
    with temporary_settings(temp):
        yield

set_env(settings_instance)

Set environment variables from a BaseSettings instance.

Each field in the settings is written to os.environ.

Source code in langroid/utils/configuration.py
def set_env(settings_instance: BaseSettings) -> None:
    """
    Set environment variables from a BaseSettings instance.

    Each field in the settings is written to os.environ.
    """
    for field_name, field in settings_instance.__class__.__fields__.items():
        env_var_name = field.field_info.extra.get("env", field_name).upper()
        os.environ[env_var_name] = str(settings_instance.dict()[field_name])