Skip to content

feat(helpers): preserve SSML breaks with additional attributes, fix matcher backtracking - #809

Merged
GregHolmes merged 2 commits into
deepgram:mainfrom
Apeksha9389:fix/ssml-break-attributes
Nov 30, 2026
Merged

GregHolmes merged 2 commits into
deepgram:mainfrom
Apeksha9389:fix/ssml-break-attributes

Conversation

@Apeksha9389

@Apeksha9389 Apeksha9389 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

ssml_to_deepgram() matched <break> only when time was 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 finds time wherever 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 raises ValueError when 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 what docs/FluxTtsControls.md already 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 in build(), 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 ph lookup's (.*?)\1(?=\s|$), and the trailing re.sub(r"<[^>]+>", "", ...) tag stripper.

Known limitation, recorded in a test

The time lookup is not quote-aware, so a time= 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 to ph on <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_value records the current behavior so that future change has a case to flip.

Verification

  • 16 <break> spellings and 4 <phoneme> spellings produce identical output before and after the backtracking fix.
  • 13 full test cases added, covering the four accepted spellings, the five newly reachable ValueError paths, 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/typecheck clean. ruff check clean 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.

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
GregHolmes force-pushed the fix/ssml-break-attributes branch from 2a4b1f9 to 700fb30 Compare November 30, 2026 12:19
@GregHolmes GregHolmes changed the title fix(helpers): preserve SSML breaks with additional attributes feat(helpers): preserve SSML breaks with additional attributes, fix matcher backtracking Nov 30, 2026
@GregHolmes
GregHolmes merged commit dc7e585 into deepgram:main Nov 30, 2026
11 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants