Skip to content

Converters

The liblaf.conf.converters module exposes lower-level helpers for cases where you want to pass a converter directly to Field(...) or Var(...).

Use these helpers when the higher-level field_* builders are not quite the right fit for your config model.

liblaf.conf.converters.identity

identity[T](x: T) -> T

Return a value unchanged.

This is the default converter for fields and variables that already receive values in their final Python form.

Source code in src/liblaf/conf/converters/_misc.py
def identity[T](x: T) -> T:
    """Return a value unchanged.

    This is the default converter for fields and variables that already receive
    values in their final Python form.
    """
    return x

liblaf.conf.converters.pydantic_model_validate

pydantic_model_validate[T: BaseModel](
    model: type[T],
) -> Converter[T]

Return a converter that validates Python objects against a model.

Parameters:

  • model (type[T]) –

    The Pydantic model class used to validate incoming objects.

Returns:

  • Converter[T]

    A callable suitable for Field(converter=...) or Var that

  • Converter[T]

    delegates to model.model_validate.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_model_validate[T: pydantic.BaseModel](model: type[T]) -> Converter[T]:
    """Return a converter that validates Python objects against a model.

    Args:
        model: The Pydantic model class used to validate incoming objects.

    Returns:
        A callable suitable for ``Field(converter=...)`` or ``Var`` that
        delegates to ``model.model_validate``.
    """
    return model.model_validate

liblaf.conf.converters.pydantic_model_validate_json

pydantic_model_validate_json[T: BaseModel](
    model: type[T],
) -> Converter[T]

Return a converter that validates JSON strings against a model.

Parameters:

  • model (type[T]) –

    The Pydantic model class used to parse JSON payloads.

Returns:

  • Converter[T]

    A callable that delegates to model.model_validate_json.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_model_validate_json[T: pydantic.BaseModel](model: type[T]) -> Converter[T]:
    """Return a converter that validates JSON strings against a model.

    Args:
        model: The Pydantic model class used to parse JSON payloads.

    Returns:
        A callable that delegates to ``model.model_validate_json``.
    """
    return model.model_validate_json

liblaf.conf.converters.pydantic_model_validate_strings

pydantic_model_validate_strings[T: BaseModel](
    model: type[T],
) -> Converter[T]

Return a converter that validates string inputs against a model.

Parameters:

  • model (type[T]) –

    The Pydantic model class used to coerce string-based inputs.

Returns:

  • Converter[T]

    A callable that delegates to model.model_validate_strings.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_model_validate_strings[T: pydantic.BaseModel](
    model: type[T],
) -> Converter[T]:
    """Return a converter that validates string inputs against a model.

    Args:
        model: The Pydantic model class used to coerce string-based inputs.

    Returns:
        A callable that delegates to ``model.model_validate_strings``.
    """
    return model.model_validate_strings

liblaf.conf.converters.pydantic_type_adapter_validate_json

pydantic_type_adapter_validate_json[T](
    type_: type[T],
) -> Converter[T]

Return a converter that validates JSON strings for an arbitrary type.

Parameters:

  • type_ (type[T]) –

    The target Python type validated by pydantic.TypeAdapter.

Returns:

  • Converter[T]

    A callable that parses JSON strings into type_ values.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_type_adapter_validate_json[T](type_: type[T]) -> Converter[T]:
    """Return a converter that validates JSON strings for an arbitrary type.

    Args:
        type_: The target Python type validated by ``pydantic.TypeAdapter``.

    Returns:
        A callable that parses JSON strings into ``type_`` values.
    """
    return pydantic.TypeAdapter(type_).validate_json

liblaf.conf.converters.pydantic_type_adapter_validate_python

pydantic_type_adapter_validate_python[T](
    type_: type[T],
) -> Converter[T]

Return a converter that validates Python objects for an arbitrary type.

Parameters:

  • type_ (type[T]) –

    The target Python type validated by pydantic.TypeAdapter.

Returns:

  • Converter[T]

    A callable that validates already-parsed Python objects.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_type_adapter_validate_python[T](type_: type[T]) -> Converter[T]:
    """Return a converter that validates Python objects for an arbitrary type.

    Args:
        type_: The target Python type validated by ``pydantic.TypeAdapter``.

    Returns:
        A callable that validates already-parsed Python objects.
    """
    return pydantic.TypeAdapter(type_).validate_python

liblaf.conf.converters.pydantic_type_adapter_validate_strings

pydantic_type_adapter_validate_strings[T](
    type_: type[T],
) -> Converter[T]

Return a converter that validates string inputs for an arbitrary type.

Parameters:

  • type_ (type[T]) –

    The target Python type validated by pydantic.TypeAdapter.

Returns:

  • Converter[T]

    A callable that coerces string inputs into type_ values.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_type_adapter_validate_strings[T](type_: type[T]) -> Converter[T]:
    """Return a converter that validates string inputs for an arbitrary type.

    Args:
        type_: The target Python type validated by ``pydantic.TypeAdapter``.

    Returns:
        A callable that coerces string inputs into ``type_`` values.
    """
    return pydantic.TypeAdapter(type_).validate_strings