replacements

User-defined text replacements for TTS preprocessing.

pipecat.utils.text.transforms.replacements.replace_text(replacements: list[tuple[str, str]]) Callable[[str, str | AggregationType], object][source]

Return a transform that applies a list of find-and-replace rules.

Each rule is a (pattern, replacement) tuple. Patterns are treated as regular expressions; use re.escape(pattern) for literal string matching.

Rules are applied in the order provided. Whether the resulting transform is alphanumeric-preserving depends on the replacements supplied.

Patterns are compiled at construction time so invalid regex patterns raise re.error immediately rather than during live TTS processing.

Parameters:

replacements – Ordered list of (regex_pattern, replacement_string) pairs.

Returns:

An async transform callable compatible with text_transforms.

Example:

transform = replace_text([
    (r"\bDr\.", "Doctor"),
    (r"\bSt\.", "Street"),
    (r"\bvs\b", "versus"),
])
tts = CartesiaTTSService(text_transforms=[("*", transform)])