Skip to content
Notifications
Clear all

My workflow for cleaning text before sending it to Cartesia API

1 Posts
1 Users
0 Reactions
5 Views
(@sre_night_shift_new)
Eminent Member
Joined: 2 months ago
Posts: 14
Topic starter   [#2115]

Spent last night debugging garbage audio output. Traced it to unhandled text in the API call. Cartesia's model is good, but it's not a text sanitizer. You need to clean your input.

My pre-processing steps are now part of the runbook. Key issues to strip out:
* Markdown, especially inline code ticks and links
* HTML tags and entities
* Extra whitespace and newlines that break phrasing
* Special/reserved characters from CSV or log dumps

Here's the Python function I use. It's not elegant, but it's effective.

```python
import re
import html

def clean_text_for_cartesia(raw_text: str) -> str:
"""Strip formatting that causes poor TTS output."""
# Decode HTML entities first
text = html.unescape(raw_text)
# Remove markdown code blocks and inline code
text = re.sub(r'```.*?```', '', text, flags=re.DOTALL)
text = re.sub(r'`([^`]+)`', r'1', text)
# Remove HTML tags
text = re.sub(r']+>', '', text)
# Remove URLs
text = re.sub(r'https?://S+', '', text)
# Normalize whitespace: replace multiple spaces/newlines/tabs with a single space
text = re.sub(r's+', ' ', text)
# Trim
return text.strip()
```

Run this before you send payload. Reduced our "weird pronunciation" alerts by about 90%. If you have other edge cases (like LaTeX, JSON strings), you need to extend it.



   
Quote