Skip to content

utils

Class info

🛈 DocStrings

Utility functions for the config package.

merge_models

merge_models(base: T, overlay: T) -> T

Deep merge two Pydantic models.

Source code in src/llmling/config/utils.py
def merge_models[T: BaseModel](base: T, overlay: T) -> T:
    """Deep merge two Pydantic models."""
    if not isinstance(overlay, type(base)):
        msg = f"Cannot merge different types: {type(base)} and {type(overlay)}"
        raise TypeError(msg)

    # Start with copy of base
    merged_data = base.model_dump()

    # Get overlay data (excluding None values)
    overlay_data = overlay.model_dump(exclude_none=True)

    for field_name, field_value in overlay_data.items():
        base_value = merged_data.get(field_name)

        match (base_value, field_value):
            case (list(), list()):
                merged_data[field_name] = [
                    *base_value,
                    *(item for item in field_value if item not in base_value),
                ]
            case (dict(), dict()):
                merged_data[field_name] = base_value | field_value
            case _:
                merged_data[field_name] = field_value

    return base.__class__.model_validate(merged_data)

prepare_runtime

prepare_runtime(
    runtime_cls: type[RuntimeConfig],
    source: JoinablePathLike | Config,
    *,
    validate: bool = True,
    strict: bool = False
) -> RuntimeConfig

Prepare runtime configuration from source.

Parameters:

Name Type Description Default
runtime_cls type[RuntimeConfig]

RuntimeConfig class to instantiate

required
source JoinablePathLike | Config

Path to configuration file or Config object

required
validate bool

Whether to validate config

True
strict bool

Whether to raise on validation warnings

False

Returns:

Type Description
RuntimeConfig

Initialized runtime configuration

Raises:

Type Description
TypeError

If source type is invalid

ConfigError

If validation fails in strict mode

Source code in src/llmling/config/utils.py
def prepare_runtime(
    runtime_cls: type[RuntimeConfig],
    source: JoinablePathLike | Config,
    *,
    validate: bool = True,
    strict: bool = False,
) -> RuntimeConfig:
    """Prepare runtime configuration from source.

    Args:
        runtime_cls: RuntimeConfig class to instantiate
        source: Path to configuration file or Config object
        validate: Whether to validate config
        strict: Whether to raise on validation warnings

    Returns:
        Initialized runtime configuration

    Raises:
        TypeError: If source type is invalid
        ConfigError: If validation fails in strict mode
    """
    match source:
        case str() | os.PathLike() | UPath():
            manager = ConfigManager.load(source, validate=validate, strict=strict)
            config = manager.config
        case Config():
            config = source
            if validate:
                manager = ConfigManager(config)
                if warnings := manager.validate():
                    if strict:
                        msg = "Config validation failed:\n" + "\n".join(warnings)
                        raise exceptions.ConfigError(msg)
                    logger.warning("Config warnings:\n%s", "\n".join(warnings))
        case _:
            msg = f"Invalid source type: {type(source)}"
            raise TypeError(msg)

    return runtime_cls.from_config(config)