> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pipecat.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LLMTextProcessor

> A processor for aggregating LLMTextFrames into logical units before passing them to downstream services

## Overview

`LLMTextProcessor` is a processor designed to aggregate `LLMTextFrame`s into coherent text units before passing them to downstream services, such as TTS. By utilizing text aggregators, it ensures that text is properly segmented and structured, enhancing the quality of subsequent processing. This processor expects `LLMTextFrame`s as input and outputs `AggregatedTextFrame`s containing the aggregated text.

<Note>
  When an `LLMTextProcessor` is in use, text aggregation is handled upstream
  and the TTS service's built-in aggregator is bypassed.
</Note>

The benefit of pre-aggregating LLM text frames is that it allows for more controlled and meaningful text synthesis. Downstream services can operate on complete sentences or logical text blocks. For TTS services, this means being able to customize how certain types of text are spoken (e.g., spelling out phone numbers, stripping out url protocols, or inserting other tts-specific annotations) or even skipping over certain text segments entirely (e.g., code snippets or markup). For other services, such as RTVI, it allows for sending these logical text units as separate `bot-output` messages, supporting custom client-side handling and rendering (e.g. collapsible code blocks, clickable links, etc.).

<CardGroup cols={2}>
  <Card title="LLMTextProcessor API Reference" icon="code" href="https://reference-server.pipecat.ai/en/latest/api/pipecat.processors.aggregators.llm_text_processor.html">
    Pipecat's API methods for LLMTextProcessor
  </Card>

  <Card title="Example Implementation" icon="play" href="https://github.com/pipecat-ai/pipecat-examples/blob/main/code-helper">
    Complete example with LLMTextProcessor and custom TTS and RTVI handling
  </Card>
</CardGroup>

## Constructor Parameters

<ParamField path="text_aggregator" type="BaseTextAggregator" default="None">
  An instance of a text aggregator (e.g., `PatternPairAggregator` or a custom
  aggregator type) used to aggregate incoming text from `LLMTextFrame`s. If
  `None`, a `SimpleTextAggregator` will be used by default, aggregating text
  based on sentence boundaries.
</ParamField>

## Usage

The `LLMTextProcessor` should be integrated into your pipeline after the LLM service and before any services that consume text, such as TTS. It processes incoming `LLMTextFrame`s, aggregates their text content, and outputs `AggregatedTextFrame`s.

<Tip>
  For more usage examples, check out the docs for the
  [PatternPairAggregator](/api-reference/server/utilities/text/pattern-pair-aggregator#usage-examples).
</Tip>

```python theme={null}
from pipecat.processors.aggregators.llm_text_processor import LLMTextProcessor

...

llm_text_aggregator = PatternPairAggregator()
llm_text_aggregator.add_pattern(
    type="code",
    start_pattern="<code>",
    end_pattern="</code>",
    action=MatchAction.AGGREGATE,
)
llm_text_processor = LLMTextProcessor(text_aggregator=llm_text_aggregator)

...

# Pipeline - The following pipeline is typical for a STT->LLM->TTS bot + RTVI
#            with the addition of the LLMTextProcessor to handle special text segments.
pipeline = Pipeline(
    [
        transport.input(),
        rtvi,
        stt,
        transcript_processor.user(),
        context_aggregator.user(),
        llm,
        llm_text_processor,
        tts,
        transport.output(),
        transcript_processor.assistant(),
        context_aggregator.assistant(),
    ]
)
```
