Skip to content

Overview

Builder utilities for constructing GLLM Multimodal components.

This package provides factory functions for building modality converters, transformers, media toolkits, and related components from configuration.

Factory Functions

Usage

from gllm_multimodal.builder import (
    build_modality_converter,
    build_modality_transformer,
    build_media_toolkit,
)

build_caption_output_formatter(formatting_strategy)

Build a caption output formatter based on the provided formatting strategy.

Parameters:

Name Type Description Default
formatting_strategy CaptionOutputFormattingStrategy

The formatting strategy to use.

required

Returns:

Name Type Description
BaseCaptionOutputFormatter BaseCaptionOutputFormatter

The caption output formatter.

Raises:

Type Description
ComponentConfigError

If the formatting strategy is not a string or is not a registered strategy.

build_media_toolkit(class_name, backend=None, processor_backends=None, **kwargs)

Build one media toolkit component by registered class name.

This is the primary factory function for creating media toolkit components. It resolves the class name against the MediaToolkit registry and instantiates the component with the provided arguments.

For composite components (segmenters, keyframe extractors), you can additionally supply processor_backends to pre-configure per-family backend overrides without needing to call MediaToolkit.set_processor_backend manually afterwards.

The request is checked before anything is constructed: 1. The class name must be a registered class, and abstract classes must be processor families. 2. The backend must be one that the class or processor family accepts. 3. The processor backends must apply to a composite and map registered processor families to their backends.

Constructor errors other than TypeError, such as invalid config values or an unavailable backend, pass through unchanged. A TypeError from the constructor, such as an unknown option or a value of the wrong type, is reported as a ComponentConfigError that names the kwargs parameter.

Parameters:

Name Type Description Default
class_name str

Registered class name, e.g. "AudioExtractionProcessor" or "FixedDurationSegmenter".

required
backend MediaBackend | str | None

Backend selector. None delegates to the family's registered default backend. Pass an explicit key (e.g. "gstreamer" or "ffmpeg") to override. Rejected when the class has no backend choice, or when the backend is not registered for the processor family. For a composite, any backend that some registered processor family offers is accepted, and a nested family that lacks it falls back to its default backend at process time. Defaults to None.

None
processor_backends dict[str, str] | None

Per-family backend overrides applied after construction for composite components, e.g. {"VideoClipProcessor": "ffmpeg"}. Rejected for non-composites, even when empty, and for families or backends that are not registered. Defaults to None.

None
**kwargs Any

Constructor keyword arguments forwarded to the component.

{}

Returns:

Name Type Description
MediaToolkit MediaToolkit

An instance of the requested MediaToolkit component.

Raises:

Type Description
ComponentConfigError

If class_name, backend, or processor_backends cannot apply to the class, or if the constructor rejects kwargs with a TypeError.

BaseError

Any GLLM error raised by the component constructor, such as MediaBackendUnavailableError.

Examples:

Default backend
from gllm_multimodal.builder.media_toolkit_builder import build_media_toolkit
from gllm_inference.schema import Attachment

processor = build_media_toolkit("AudioExtractionProcessor")
audio = await processor.process(Attachment.from_path("clip.mp4"))
Explicit backend + process result
processor = build_media_toolkit(
    "FixedDurationSegmenter",
    backend="gstreamer",
    config={"segment_durations": [2.0]},
)
clips = await processor.process(Attachment.from_path("video.mp4"))
# clips is a list[Attachment]

build_media_toolkit_bulk(specs)

Build media toolkit components from declarative specs.

This function accepts a list of specification dictionaries and builds each component using build_media_toolkit. All specs are validated before any component is instantiated, so errors are reported immediately.

Spec format

Each spec dictionary must have a "name" key and optionally "kwargs" and "processor_backends".

Use case: loading a list of component specs from a YAML or JSON configuration file, where all components must be validated before any is instantiated (fail-fast, all errors reported at once).

[
    {"name": "AudioExtractionProcessor", "kwargs": {"backend": "gstreamer"}},
    {"name": "FixedDurationSegmenter", "kwargs": {
        "backend": "gstreamer",
        "config": {"segment_durations": [2.0]}
    }}
]

For composite components you may additionally supply "processor_backends" to pre-configure per-family backend overrides:

{
    "name": "FixedDurationSegmenter",
    "kwargs": {"backend": "gstreamer", "config": {"segment_durations": [2.0]}},
    "processor_backends": {"VideoClipProcessor": "ffmpeg"}
}

Notes

  • name must be a registered class name (family base, concrete, or composite).
  • kwargs.backend is consumed by the factory and not passed as a constructor argument to leaf backend classes. For composites it is stored for nested processor resolution, and any backend that some registered processor family offers is accepted.
  • processor_backends is rejected for non-composite components, even when empty, since it would have no effect on them.
  • All specs are checked before the error is raised. Each spec reports all of its shape problems, such as unknown keys, plus the first build rule it breaks. The problems are reported in one ComponentConfigError, and the error does not include spec values, so secrets in kwargs are not shown.

Parameters:

Name Type Description Default
specs list[dict[str, Any]]

A list of component specification dictionaries.

required

Returns:

Type Description
list[MediaToolkit]

list[MediaToolkit]: A list of instantiated MediaToolkit components in the

list[MediaToolkit]

same order as the input specs.

Raises:

Type Description
ComponentConfigError

If specs is not a list, or if any spec is invalid. A spec is invalid when it is not a dict, lacks name, has unknown keys or non-dict kwargs, or breaks a rule that the single build also enforces.

BaseError

Any GLLM error raised by a component constructor.

Examples:

from gllm_multimodal.builder.media_toolkit_builder import build_media_toolkit_bulk

specs = [
    {"name": "AudioExtractionProcessor", "kwargs": {"backend": "gstreamer"}},
    {"name": "FixedDurationSegmenter", "kwargs": {"config": {"segment_durations": [2.0]}}},
]
components = build_media_toolkit_bulk(specs)

build_modality_converter(source_modality, target_modality, task_type=ModalityConverterTask.AUTO, approach_type=None, preset=None, model_id=None, strategy=None, **kwargs)

Build and initialize a modality converter instance for a given configuration.

The factory looks up the converter class based on the combination of
  • source_modality: input modality (e.g., Modality.IMAGE, Modality.AUDIO)
  • target_modality: output modality (e.g., Modality.TEXT)
  • task_type: conversion task (e.g., CAPTIONING, TRANSCRIPT, MERMAID, OCR, or AUTO)
  • approach_type: the converter's algorithmic approach; required for non-AUTO tasks, must be None for AUTO

Audio transcript approaches are separated into canonical LM_BASED, ASR, and TRANSCRIPT_FETCH values. For audio transcription, model_id selects the provider implementation and is forwarded to the LM invoker when the resolved approach is LM_BASED. Use this factory directly for audio transcription.

preset has the same meaning as for image converters: a named from_preset config.

Parameters:

Name Type Description Default
source_modality Modality

The source modality.

required
target_modality Modality

The output modality.

required
task_type ModalityConverterTask

The conversion task. Defaults to ModalityConverterTask.AUTO.

AUTO
approach_type ModalityConverterApproach | None

The approach for the conversion. Required for non-AUTO tasks; must be None for task_type=AUTO.

None
preset str | None

Named from_preset config (same semantics as image-to-text). Defaults to None.

None
model_id str | ModelId | None

Provider-qualified audio model identifier used to infer the provider implementation when applicable. Defaults to None, which selects the default provider for the requested approach. For transcript fetching, omit this argument to select YouTube, which does not require a model name.

None
strategy ModalityConverterBuildStrategy | None

The build strategy to use. If None, the strategy is determined automatically based on the provided parameters.

None
**kwargs Any

Additional keyword arguments passed to the converter, including: 1. lmrp_config (dict[str, Any]): Configuration to build an LMRP instance. Should follow the same structure as build_lm_request_processor. 2. Any other parameters supported by the converter's initialization method.

{}

Returns:

Name Type Description
BaseModalityConverter BaseModalityConverter

An instance of the matching converter class.

Raises:

Type Description
ComponentConfigError

If the configuration is invalid or not registered, or the selected audio provider SDK is not installed.

BaseError

If model parsing or configuration preparation raises a GLLM error, preserved unchanged.

build_modality_transformer(source_modality=Modality.IMAGE, target_modality=Modality.TEXT, transformer_type=ModalityTransformerType.STANDARD, router_config=None, converter_config=None, **kwargs)

Build and initialize a modality transformer instance for a given configuration.

The factory looks up the converter class based on the combination of
  • source_modality: input modality (e.g., Modality.IMAGE, Modality.AUDIO)
  • target_modality: output modality (e.g., Modality.TEXT)
  • transformer_type: transformer type (e.g., ModalityTransformerType.STANDARD)

The factory then delegates construction to each class's from_config() classmethod.

Supported combinations of (source_modality, target_modality, transformer_type):

Parameters:

Name Type Description Default
source_modality Modality

The source modality. Defaults to Modality.IMAGE.

IMAGE
target_modality Modality

The output modality. Defaults to Modality.TEXT.

TEXT
transformer_type ModalityTransformerType

The transformer type. Defaults to ModalityTransformerType.STANDARD.

STANDARD
router_config RouterConfig | None

Additional router configuration. Defaults to None.

None
converter_config dict[str, ConverterConfig] | None

Additional converter configuration. Defaults to None.

None
**kwargs Any

Additional keyword arguments. None are supported; any option supplied is rejected.

{}

Returns:

Name Type Description
BaseModalityTransformer BaseModalityTransformer

An instance of the matching transformer class.

Raises:

Type Description
ComponentConfigError

If the configuration is not registered, or an unsupported option is supplied. This includes an unregistered source_modality, target_modality, or transformer_type, and a router_config with the generic transformer type.

Examples:

Default routers and converters
from gllm_multimodal.constants import Modality, ModalityTransformerType
from gllm_multimodal.builder.modality_transformer_builder import build_modality_transformer

transformer = build_modality_transformer(
    source_modality=Modality.IMAGE,
    target_modality=Modality.TEXT,
    transformer_type=ModalityTransformerType.STANDARD,
)
Custom router and default converters
from gllm_multimodal.constants import Modality, ModalityTransformerType
from gllm_multimodal.modality_transformer.schema.router_config import RouterConfig
from gllm_multimodal.builder.modality_transformer_builder import build_modality_transformer

transformer = build_modality_transformer(
    source_modality=Modality.IMAGE,
    target_modality=Modality.TEXT,
    transformer_type=ModalityTransformerType.STANDARD,
    router_config=RouterConfig(
        modality=Modality.IMAGE,
        preset="multimodal",
        model_id="openai/text-embedding-3-small",
        es_url="http://localhost:9200",
        es_index_name="gllm_multimodal",
        route_mapping={"chart": "captioning", "diagram": "mermaid"},
    ),
)
Custom converters with preset
from gllm_multimodal.constants import (
    Modality, ModalityConverterTask, ModalityConverterApproach, ModalityTransformerType,
)
from gllm_multimodal.modality_transformer.schema.converter_config import ConverterConfig
from gllm_multimodal.builder.modality_transformer_builder import build_modality_transformer

transformer = build_modality_transformer(
    source_modality=Modality.IMAGE,
    target_modality=Modality.TEXT,
    transformer_type=ModalityTransformerType.STANDARD,
    converter_config={
        "captioning": ConverterConfig(
            source_modality=Modality.IMAGE,
            target_modality=Modality.TEXT,
            task_type=ModalityConverterTask.CAPTIONING,
            approach_type=ModalityConverterApproach.LM_BASED,
            preset="default",
        ),
        "mermaid": ConverterConfig(
            source_modality=Modality.IMAGE,
            target_modality=Modality.TEXT,
            task_type=ModalityConverterTask.MERMAID,
            approach_type=ModalityConverterApproach.LM_BASED,
            preset="default",
        ),
    },
)
Custom converters with custom LMRP
from gllm_multimodal.constants import (
    Modality, ModalityConverterTask, ModalityConverterApproach, ModalityTransformerType,
)
from gllm_multimodal.modality_transformer.schema.converter_config import ConverterConfig
from gllm_multimodal.builder.modality_transformer_builder import build_modality_transformer

transformer = build_modality_transformer(
    source_modality=Modality.IMAGE,
    target_modality=Modality.TEXT,
    transformer_type=ModalityTransformerType.STANDARD,
    converter_config={
        "captioning": ConverterConfig(
            source_modality=Modality.IMAGE,
            target_modality=Modality.TEXT,
            task_type=ModalityConverterTask.CAPTIONING,
            approach_type=ModalityConverterApproach.LM_BASED,
            lmrp_config={
                "model_id": "google/gemini-3-flash-preview",
                "system_template": "Describe this image...",
                "user_template": "What is in this image?",
                "output_parser_type": "json",
            },
        ),
        "mermaid": ConverterConfig(
            source_modality=Modality.IMAGE,
            target_modality=Modality.TEXT,
            task_type=ModalityConverterTask.MERMAID,
            approach_type=ModalityConverterApproach.LM_BASED,
            lmrp_config={
                "model_id": "openai/gpt-5.2-latest",
                "system_template": "Generate mermaid...",
                "user_template": "Convert to mermaid...",
            },
        ),
    },
)