Skip to content

Repacker

Modules for repacking, a process of incorporating retrieved chunks into a prompt as context.

Repacker(method='forward', mode='chunk', delimiter='\n\n', size_func=lambda chunk: len(chunk.content), size_limit=None, context_config=None, pack='truncate', batch_size=None)

Bases: Component

Repacks chunks by running the PACK -> ORDER -> SHAPE stages (see the module docstring).

Attributes:

Name Type Description
method RepackMethod

The ORDER stage strategy - how chunks are arranged within each batch.

mode RepackerMode

The SHAPE stage output - how the ordered batches collapse into the return value ("chunk", "context").

pack RepackStrategy

The PACK stage strategy - how chunks are fitted against size_limit. Resolved per repack() call, so reassigning it after construction takes effect on the next call.

delimiter str

The delimiter the SHAPE stage joins chunk contents with.

size_func Callable[[Chunk], int]

Function the PACK stage uses to measure a single chunk.

size_limit int | None

The maximum total chunk size the PACK stage fits into a batch.

batch_size int | None | Literal['auto']

The PACK-stage batching knob. See the constructor's docstring for the None/int/"auto" cases.

strategy BaseRepackStrategy

The resolved ORDER stage strategy for method.

context_config ContextConfig | None

Context field selection and rendering configuration.

Initialize the Repacker component.

Parameters:

Name Type Description Default
method str | RepackMethod

The ORDER stage strategy - how chunks are arranged within each batch ("forward", "reverse", "sides"). Defaults to "forward".

'forward'
mode str | RepackerMode

The SHAPE stage output ("chunk", "context"). Defaults to "chunk".

'chunk'
delimiter str

The delimiter the SHAPE stage joins chunk contents with. Defaults to "\n\n".

'\n\n'
size_func Callable[[Chunk], int]

Function used to measure the size of a single chunk. Defaults to the length of the chunk content.

lambda chunk: len(content)
size_limit int | None

The maximum allowed total size for a batch. Defaults to None. Note: The size limit only accounts for the chunks, not including the delimiter.

None
context_config ContextConfig | None

Configuration for rendering context text from chunks. Defaults to None, in which case legacy str(chunk.content) rendering is used.

None
pack str | RepackStrategy

The PACK stage strategy. Defaults to "truncate".

'truncate'
batch_size int | None | Literal['auto']

The PACK-stage batching knob: 1. None (default): the whole input is a single batch. mode="chunk" returns list[Chunk], mode="context" returns a single joined str. 2. Positive integer: chunks are count-partitioned into fixed-size batches before size_limit is applied per batch. mode="chunk" returns list[list[Chunk]], mode="context" returns list[str] (one joined string per batch). 3. "auto": chunks are grouped with a next-fit strategy driven by size_limit alone, with no count limit. Same per-batch return shapes as the positive-integer case.

None

repack(chunks) async

Repack the input chunks based on the chosen pack, method, mode, and batch_size.

The return type depends on mode and batch_size together:

  1. mode="chunk" with batch_size=None (default): list[Chunk].
  2. mode="chunk" with batch_size a positive int or "auto": list[list[Chunk]].
  3. mode="context" with batch_size=None (default): str.
  4. mode="context" with batch_size a positive int or "auto": list[str].

Parameters:

Name Type Description Default
chunks list[Chunk]

The input chunks to repack.

required

Returns:

Type Description
list[Chunk] | list[list[Chunk]] | str | list[str]

list[Chunk] | list[list[Chunk]] | str | list[str]: The repacked chunks, shaped per the combinations above.

Raises:

Type Description
GenerationValueError

If chunks is not a list of Chunk objects.