transforms
Built-in text transformers for TTS voice formatting.
Transformers are async callables with signature:
async def transform(text: str, text_type: str) -> str
They are registered with a TTS service via text_transforms:
tts = CartesiaTTSService(
text_transforms=[("*", strip_markdown), ("*", expand_currency)],
)
Or use VoiceFormatter for a single configurable bundle:
tts = CartesiaTTSService(
text_transforms=[("*", VoiceFormatter(expand_currency=True))],
)
- async pipecat.utils.text.transforms.normalize_acronyms(text: str, text_type: str | TextType) str[source]
Insert spaces between letters of uppercase acronyms.
This transformer is alphanumeric-preserving: the same letters are kept, only spaces are added between them, so the
WordCompletionTrackerrequires no segment-map overhead.- Parameters:
text – Input text possibly containing acronyms like API or HTTP.
text_type – Text type of the text frame (unused).
- Returns:
Text with acronyms letter-spaced (e.g.
"API"→"A P I").
Example:
result = await normalize_acronyms("Use the API or HTTP endpoint", "*") # "Use the A P I or H T T P endpoint"
- async pipecat.utils.text.transforms.expand_currency(text: str, text_type: str | TextType) str[source]
Expand currency amounts to their spoken form.
- Parameters:
text – Input text possibly containing currency expressions.
text_type – Text type of the text frame (unused).
- Returns:
Text with currency amounts replaced by spoken equivalents.
Example:
result = await expand_currency("Your balance is $42.50", "*") # "Your balance is forty-two dollars and fifty cents"
- async pipecat.utils.text.transforms.normalize_dates(text: str, text_type: str | TextType) str[source]
Expand date expressions to their spoken form.
Handles ISO format (
YYYY-MM-DD) and US format (MM/DD/YYYYorMM-DD-YYYY).- Parameters:
text – Input text possibly containing date expressions.
text_type – Text type of the text frame (unused).
- Returns:
Text with date expressions replaced by spoken equivalents.
Example:
result = await normalize_dates("Meeting on 2023-05-10", "*") # "Meeting on May 10th, two thousand and twenty three"
- async pipecat.utils.text.transforms.email_to_speech(text: str, text_type: str | TextType) str[source]
Transform email addresses into their spoken form.
- Parameters:
text – Input text possibly containing email addresses.
text_type – Text type of the text frame (unused).
- Returns:
Text with email addresses replaced by spoken equivalents.
Example:
result = await email_to_speech("Contact user@example.com today", "*") # "Contact user at example dot com today"
- pipecat.utils.text.transforms.expand_numbers(digit_cutoff: int | None = 2025) Callable[[str, str | TextType], Awaitable[str]][source]
Return a transform that expands numbers to their spoken form.
When digit_cutoff is set, numbers above it are read digit-by-digit (e.g.
"2026"→"2 0 2 6"). Numbers at or below the cutoff are expanded as quantities (e.g."42"→"forty two"). PassNoneto expand all numbers as words regardless of magnitude.Decimals are read as the whole part followed by their fractional digits, one at a time, so every written digit is spoken (
"1.0"→"one point zero").- Parameters:
digit_cutoff – Numbers larger than this value are read digit-by-digit.
Nonedisables the cutoff so every number is expanded as a word.- Returns:
An async transform callable compatible with
text_transforms.
Example:
transform = expand_numbers(digit_cutoff=2025) result = await transform("Room 42 has 1234 seats and opens in 2026", "*") # "Room forty two has one thousand two hundred thirty four seats and opens in 2 0 2 6"
- async pipecat.utils.text.transforms.expand_percentages(text: str, text_type: str | TextType) str[source]
Expand percentage expressions to their spoken form.
- Parameters:
text – Input text possibly containing percentage expressions.
text_type – Text type of the text frame (unused).
- Returns:
Text with percentages replaced by spoken equivalents.
Example:
result = await expand_percentages("50% off today", "*") # "fifty percent off today"
- async pipecat.utils.text.transforms.expand_phone_numbers(text: str, text_type: str | TextType) str[source]
Space out phone number digits so TTS reads them individually.
This transformer is alphanumeric-preserving: digits are kept, only separators change to spaces, so the
WordCompletionTrackerrequires no segment-map overhead.- Parameters:
text – Input text possibly containing phone numbers.
text_type – Text type of the text frame (unused).
- Returns:
Text with phone numbers replaced by space-separated digit sequences.
Example:
result = await expand_phone_numbers("Call 123-456-7890 now", "*") # "Call 1 2 3 4 5 6 7 8 9 0 now"
- pipecat.utils.text.transforms.replace_text(replacements: list[tuple[str, str]]) Callable[[str, str | TextType], Awaitable[str]][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; usere.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.errorimmediately 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)])
- class pipecat.utils.text.transforms.StripMarkdown(*, strip_urls: bool = True)[source]
Bases:
objectTransform that removes Markdown formatting symbols with no spoken equivalent.
Strips bold/italic markers, backtick code spans, fenced code blocks, ATX headers, list bullets, blockquote markers and horizontal rules. Struck-through text (
~~like this~~) is removed along with its markers, since it is text the reply takes back. A link or image is reduced to its label or alt text, so the URL is not read out, unlessstrip_urlsis off.An asterisk that is content stays: one between digits (
2*3), one escaped as\*, or one standing alone between spaces (Press * then #).Outside of fenced code, struck-through text and link URLs, this transformer is alphanumeric-preserving: the normalized alnum sequence of the output is identical to the input, so the
WordCompletionTrackerrequires no segment-map overhead.strip_markdownis an instance with the default options, for registering directly.Example:
tts = CartesiaTTSService( text_transforms=[("*", StripMarkdown(strip_urls=False))], )
- async pipecat.utils.text.transforms.expand_units(text: str, text_type: str | TextType) str[source]
Expand unit abbreviations to their full spoken form.
A quantity of exactly one takes the singular form of the unit.
- Parameters:
text – Input text possibly containing unit expressions.
text_type – Text type of the text frame (unused).
- Returns:
Text with unit abbreviations replaced by spoken equivalents.
Example:
result = await expand_units("Run 5km at 100kph", "*") # "Run 5 kilometers at 100 kilometers per hour" result = await expand_units("Only 1km left", "*") # "Only 1 kilometer left"
- class pipecat.utils.text.transforms.VoiceFormatter(*, strip_markdown: bool = True, strip_urls: bool = True, expand_phone_numbers: bool = True, normalize_acronyms: bool = True, expand_currency: bool = True, expand_numbers: bool = False, number_digit_cutoff: int | None = None, expand_percentages: bool = True, expand_units: bool = True, email_to_speech: bool = True, normalize_dates: bool = True, custom_replacements: list[tuple[str, str]] | None = None)[source]
Bases:
objectConfigurable bundle that applies a pipeline of voice-formatting transforms.
Each option enables or disables one transform. Transforms are applied in a deliberate order: structural cleanup first, language expansions second, user replacements last.
Example:
formatter = VoiceFormatter( strip_markdown=True, expand_currency=True, number_digit_cutoff=2025, custom_replacements=[(r"\bDr\.", "Doctor")], ) tts = CartesiaTTSService( text_transforms=[("*", formatter)], )
- __init__(*, strip_markdown: bool = True, strip_urls: bool = True, expand_phone_numbers: bool = True, normalize_acronyms: bool = True, expand_currency: bool = True, expand_numbers: bool = False, number_digit_cutoff: int | None = None, expand_percentages: bool = True, expand_units: bool = True, email_to_speech: bool = True, normalize_dates: bool = True, custom_replacements: list[tuple[str, str]] | None = None)[source]
Initialize the voice formatter.
- Parameters:
strip_markdown – Strip Markdown formatting symbols (bold, italic, headers, code spans). Enabled by default.
strip_urls – Reduce a Markdown link or image to its label or alt text so the URL is not spoken. Enabled by default; only used when
strip_markdown=True.expand_phone_numbers – Space out phone number digits for individual pronunciation. Enabled by default.
normalize_acronyms – Space out uppercase acronyms (e.g.
"API"→"A P I"). Enabled by default.expand_currency – Expand currency amounts to spoken form (e.g.
"$42.50"→"forty two dollars and fifty cents"). Requiresnum2words.expand_numbers – Expand numeric digits to spoken words. Disabled by default since it can affect numbers that are better read as digits. Requires
num2words.number_digit_cutoff – Numbers above this value are read digit-by-digit instead of as a quantity. Defaults to
None(expand all numbers as words). Only used whenexpand_numbers=True.expand_percentages – Expand percentage expressions (e.g.
"50%"→"fifty percent"). Requiresnum2words.expand_units – Expand unit abbreviations (e.g.
"5km"→"5 kilometers"). Enabled by default.email_to_speech – Transform email addresses to spoken form. Enabled by default.
normalize_dates – Expand date expressions to spoken form. Requires
num2words.custom_replacements – List of
(regex_pattern, replacement)pairs applied after all other transforms.