Repository navigation
feat(helpers): preserve SSML breaks with additional attributes, fix matcher backtracking - #809
Merged
Merged
Conversation
Apeksha9389
requested review from
GregHolmes,
deepgram-kiley and
dg-coreylweathers
as code owners
October 22, 2026 08:05
GregHolmes
force-pushed
the
fix/ssml-break-attributes
branch
from
November 30, 2026 11:16
2a160a6 to
b23e2d8
Compare
The wider `<break>` matcher added in the previous commit put three whitespace-capable quantifiers in a row (`\s+`, `[^<>]*?`, `\s*`). On an unterminated tag the match backtracks cubically, about 8x per doubling of the whitespace run: a `<break ` followed by 2000 spaces took 2.49s against 0.0002s before the change. `ssml_to_deepgram()` has no input limit of its own and the 2000-character guard lives in `build()`, which runs afterwards, so nothing bounded it. Drop the overlapping quantifiers from both tag patterns. The `<phoneme>` pattern has carried the same shape since the attribute-order fix and is affected in released versions: on 7.20.0 it takes about 22s on a 4 KB input, so any caller passing user-supplied SSML can be stalled by a few kilobytes of text. The attribute lookups are anchored on `(?:^|\s)` and `(?=\s|$)`, so the captured group keeps its surrounding whitespace without changing which attributes match. Scope: this covers the long-whitespace-run shape. Two milder pre-existing shapes are untouched and remain quadratic, needing 40x to 140x more input to stall: the `ph` lookup's `(.*?)\1(?=\s|$)` and the trailing `re.sub(r"<[^>]+>", "", ...)` tag stripper. Behavior is unchanged: 16 `<break>` spellings and 4 `<phoneme>` spellings produce identical output before and after, and the fixed shape is flat at 0.0003s out to n=20000. Also add the coverage the wider matcher needs. The four original cases asserted only success, while an attributed break can now reach the duration floor, ceiling and 100ms grid, the eight-pause limit and the pronunciation/pause exclusion that a bare break already hit. Move them onto `TestSsmlToDeepgram` with their peers, and record the known limitation that the `time` lookup is not quote-aware, so a `time=` inside another attribute's quoted value is read as the real one.
GregHolmes
force-pushed
the
fix/ssml-break-attributes
branch
from
November 30, 2026 12:19
2a4b1f9 to
700fb30
Compare
GregHolmes
approved these changes
Nov 30, 2026
GregHolmes
added a commit
that referenced
this pull request
Dec 2, 2026
🤖 I have created a release *beep* *boop* --- ## [7.21.0](v7.20.0...v7.21.0) (2026-11-30) ### Features * **helpers:** `ssml_to_deepgram()` and `TextBuilder.from_ssml()` now keep a `<break>` pause when the tag carries other attributes or spaces around `=`, such as `<break strength="medium" time="500ms"/>` or `<break time = "500ms"/>`. These spellings previously failed to match and were deleted by the generic tag stripper, so the pause silently vanished from the text sent for synthesis. Behavior change: a `<break>` with extra attributes is now validated like a bare `<break time="...">`, and raises `ValueError` when the duration is below 500ms, above 3000ms or off the 100ms grid, when a document holds more than eight pauses, or when it is combined with a pronunciation. Also fixes a hang present in 7.20.0 and earlier: the `<break>` and `<phoneme>` SSML matchers backtracked cubically on an unterminated tag (about 22 seconds on a 4 KB input), which could stall any caller passing user-supplied SSML; both now return in under a millisecond at 20,000 characters. Known limitation: a `time=` written inside another attribute's quoted value is still read as the real one. ([#809](#809)) ([dc7e585](dc7e585)) ### Bug Fixes * **helpers:** `TextBuilder.text()` with an inline pronunciation, and `TextBuilder.from_ssml()`, now count the spoken word of a pronunciation toward the 2,000-character limit, matching the fluent `pronunciation()` call. Previously the whole control, including the word, was removed before counting, so the same text could be rejected through the fluent API but accepted at 2,001 characters through inline or SSML input. The word is counted by its decoded length, so Unicode and JSON-escaped quotes and backslashes are measured as spoken; pause controls still contribute no text. Inputs just over the limit that were previously accepted now raise `ValueError`. ([#807](#807)) ([373d43c](373d43c)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Greg Holmes <greg.holmes@deepgram.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ssml_to_deepgram()matched<break>only whentimewas the tag's sole attribute and was written without spaces around the=. Anything else, including<break strength="medium" time="500ms"/>and<break time = "500ms"/>, failed to match and was then deleted by the generic tag stripper, so the pause silently vanished from the text sent for synthesis.TextBuilder.from_ssml()inherited the loss. The matcher now findstimewherever it sits, as the<phoneme>matcher has done since #741.Behavior change
SSML that previously lost its pause without comment is now validated. A
<break>carrying extra attributes now raisesValueErrorwhen its duration is below 500ms, above 3000ms or off the 100ms grid, when a document holds more than eight pauses, or when it is combined with a pronunciation. This is what a bare<break time="...">already did, and whatdocs/FluxTtsControls.mdalready documents; attributed breaks simply never reached the check.Also fixes a hang present in released versions
Both SSML tag matchers backtracked cubically on an unterminated tag, about 8x per doubling of a trailing whitespace run. The
<phoneme>pattern has carried this since #741 and is affected in 7.20.0 and earlier: roughly 22 seconds on a 4 KB input.ssml_to_deepgram()applies no length limit of its own and the 2000-character guard lives inbuild(), which runs afterwards, so any caller passing user-supplied SSML can be stalled by a few kilobytes of text. Both patterns now return in under a millisecond at n=20000.Scope: this covers the long-whitespace-run shape. Two milder pre-existing shapes are untouched and remain quadratic, needing 40x to 140x more input to stall: the
phlookup's(.*?)\1(?=\s|$), and the trailingre.sub(r"<[^>]+>", "", ...)tag stripper.Known limitation, recorded in a test
The
timelookup is not quote-aware, so atime=written inside another attribute's quoted value is read as the real one (<break foo="x time='600ms' y" time="500ms"/>yields a 600ms pause). The markup is malformed, no valid SSML is affected, and the same shape already applies tophon<phoneme>. The obvious quote-aware regex is itself quadratic, so a correct fix needs a real tokenizer and belongs in its own change.test_break_reads_a_decoy_time_inside_another_attribute_valuerecords the current behavior so that future change has a case to flip.Verification
<break>spellings and 4<phoneme>spellings produce identical output before and after the backtracking fix.ValueErrorpaths, three near-miss attribute names, the decoy, and a backtracking guard on both tags.pytest tests/custom: 1116 passed, 1 skipped, 1 xfailed.mypy src/: 955 files clean.mypy tests/typecheckclean.ruff checkclean on both changed files.Both files are permanently frozen in
.fernignore, so no Fern regeneration interaction applies.The original fix and its four regression cases are @Apeksha9389's work, prepared with OpenAI Codex assistance per their disclosure. The backtracking fix, the
<phoneme>pattern change and the additional test coverage were added on top by a maintainer.