Skip to content

Modality Transformer

Abstract base class for modality transformation in Gen AI applications.

This module defines the common interface for transforming media modalities into text or structured representations. Transformers differ from converters by operating on raw media bytes or strings and returning structured TransformResult objects.

BaseModalityTransformer

Bases: Component, ABC

Abstract base class for modality transformation in Gen AI applications.

This class defines the common interface for transforming media sources (images, audio, video) into text or structured representations. It extends Component to integrate with the GLLM component lifecycle.

Transformers follow a simple contract:

  1. Accept a source (bytes or string) and optional query for context.
  2. Transform the source into a structured result.
  3. Return a TransformResult containing the transformed content, metadata, and routing information.

Subclasses

Subclasses must implement the transform method. The _run method is called by the component lifecycle and delegates to transform.

Usage

class MyTransformer(BaseModalityTransformer):
    async def transform(
        self, source: bytes | str, query: str | None = None, **kwargs
    ) -> TransformResult:
        # implementation here
        ...

Attributes:

Name Type Description
_logger Logger

Logger instance scoped to the concrete class name (inherited).

transform(source, query=None, **kwargs) abstractmethod async

Transform a media source into a structured result.

This abstract method must be implemented by subclasses to define how the input source is transformed. The method receives the raw source and an optional query for context, and returns a TransformResult.

Parameters:

Name Type Description Default
source bytes | str

The media source to transform. Can be raw bytes or a string (e.g., file path or URL).

required
query str | None

Optional query string providing additional context for the transformation. For example, a prompt or instruction.

None
**kwargs Any

Additional keyword arguments for the transformation. Implementations may accept provider-specific options.

{}

Returns:

Type Description
TransformResult

A structured result containing the transformed content, metadata,

TransformResult

and routing information.

Raises:

Type Description
NotImplementedError

If the subclass does not override this method.