Skip to content

Context Enricher

Modules concerning the context enrichers used in Gen AI applications.

MetadataContextEnricher(metadata_fields, position=MetadataPosition.PREFIX, separator='\n---\n', field_template='- {field}: {value}', skip_empty=True, binary_handling=BinaryHandlingStrategy.BASE64)

Bases: BaseContextEnricher

A metadata context enricher that adds metadata to the chunk content.

This enricher formats metadata fields into a string and appends it to the chunk content based on the specified position (prefix or suffix).

Attributes:

Name Type Description
metadata_fields list[str]

List of metadata fields to include in the enriched content.

position MetadataPosition

Position of the metadata in the content. Valid values are defined in the MetadataPosition enum: - PREFIX: Metadata block is placed before content - SUFFIX: Metadata block is placed after content

separator str

Separator between the metadata and the content.

field_template str

Template for formatting each metadata field.

skip_empty bool

Whether to skip fields with empty values.

binary_handling BinaryHandlingStrategy

Strategy for handling binary data. Valid values are defined in the BinaryHandlingStrategy: - BASE64: Binary data is converted to base64 (default) - HEX: Binary data is converted to hexadecimal - NONE: Binary data is not included in the metadata block

Initialize the metadata context enricher.

Parameters:

Name Type Description Default
metadata_fields list[str]

List of metadata field names to include.

required
position MetadataPosition

Where to place metadata block. Valid values are defined in the MetadataPosition enum: 1. "prefix": Metadata block is placed before content 2. "suffix": Metadata block is placed after content

PREFIX
separator str

String to separate metadata from content.

'\n---\n'
field_template str

Template for formatting each metadata field. Available fields: - {field}: Field name - {value}: Field value

'- {field}: {value}'
skip_empty bool

Whether to skip empty fields.

True
binary_handling BinaryHandlingStrategy

Strategy for handling binary data. Valid values are defined in the BinaryHandlingStrategy: 1. "base64": Binary data is converted to base64 (default) 2. "hex": Binary data is converted to hexadecimal 3. "none": Binary data is not included in the metadata block

BASE64

TranscriptMode

Bases: StrEnum

Modes for transcript rendering in video context enrichment.

VideoContextEnricher(transcript=TranscriptMode.PLAIN, group_by=None, format_timestamps=_format_seconds_to_time)

Bases: BaseContextEnricher

A context enricher specialized for video content.

This strategy transforms raw video metadata into a structured, human-readable format ideal for LM context. It supports two main operational modes:

  1. Single Mode: Processes a single chunk containing a full video summary and multiple segments.
  2. Grouped Mode: Aggregates multiple chunks (e.g., individual segments) sharing the same identifier (like file_id) into a single comprehensive chunk to avoid header duplication.
Output Format
Video: [video_<file_id>]
    Summary: <video_summary>

    [<MM:SS-MM:SS>]
    Caption: <description of the visual content>
    Transcript: <spoken words in the segment>
Key Features
  1. Consolidation: Use group_by to merge segment-level chunks.
  2. Dynamic Formatting: Toggles transcripts and normalizes timestamps (e.g., 70s -> 01:10).

Examples:

enricher = VideoContextEnricher(group_by="file_id")
enriched_chunks = await enricher.enrich(chunks)
print("\n".join(enriched_chunks[0].additional_context))
# Output:
# Video: [my_video.mp4]
# Summary: A chef preparing a meal.
#
# [00:00-00:05]
# Caption: The chef chops onions.
# Transcript: First, we dice the onions finely.

Attributes:

Name Type Description
transcript TranscriptMode

Controls transcript rendering.

group_by str | tuple[str, ...] | None

Metadata key(s) used to identify chunks belonging to the same video.

format_timestamps Callable[[float | int], str]

Converts seconds to MM:SS / HH:MM:SS.

Initializes the Video Context Enricher.

Parameters:

Name Type Description Default
transcript TranscriptMode

Controls transcript rendering. 1. TranscriptMode.PLAIN: includes transcript text. 2. TranscriptMode.TIMESTAMPS: prefixes each line with the segment timestamp. 3. TranscriptMode.NONE: omits transcripts entirely. Defaults to TranscriptMode.PLAIN.

PLAIN
group_by str | tuple[str, ...] | None

Metadata key(s) to use for grouping chunks. Crucial for consolidating individual segment chunks into one video summary. 1. None: No grouping; each chunk enriched independently. 2. "file_id": Merge chunks sharing the same file_id. 3. "video_id": Merge chunks sharing the same video_id. 4. "source": Merge chunks sharing the same source path. 5. ("source_type", "file_id"): Merge chunks sharing the same combination of source_type and file_id (composite key). Video chunks missing any grouping key (or with empty/falsy values) are enriched as singletons. Non-video chunks pass through unchanged.

None
format_timestamps Callable[[float | int], str]

A callable that converts seconds to a formatted string. Defaults to _format_seconds_to_time (MM:SS / HH:MM:SS).

_format_seconds_to_time