From 766ff878403a2808d0c6a6e418219eb3b7b0659a Mon Sep 17 00:00:00 2001 From: Michael Lip <51033404+theluckystrike@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:20:58 +0800 Subject: [PATCH] fix(helpers) decode XML entities in ssml_to_deepgram --- src/deepgram/helpers/text_builder.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/deepgram/helpers/text_builder.py b/src/deepgram/helpers/text_builder.py index 2e1d5251..c3f329df 100644 --- a/src/deepgram/helpers/text_builder.py +++ b/src/deepgram/helpers/text_builder.py @@ -6,6 +6,7 @@ """ import json +import html import re from typing import Tuple @@ -301,12 +302,12 @@ def ssml_to_deepgram(ssml_text: str) -> str: def replace_phoneme(match): attributes = match.group(1) - word = match.group(2) + word = html.unescape(match.group(2)) alphabet_match = re.search(r'(?:^|\s)alphabet\s*=\s*(["\'])ipa\1(?=\s|$)', attributes) ph_match = re.search(r'(?:^|\s)ph\s*=\s*(["\'])(.*?)\1(?=\s|$)', attributes) if alphabet_match is None or ph_match is None: return word - ipa = ph_match.group(2) + ipa = html.unescape(ph_match.group(2)) return _pronunciation_control(word, ipa) ssml_text = re.sub(phoneme_pattern, replace_phoneme, ssml_text) @@ -346,6 +347,13 @@ def replace_break(match): # Remove any remaining XML tags ssml_text = re.sub(r"<[^>]+>", "", ssml_text) + # Decode XML entities in plain text segments without altering inline controls + control_split = re.compile(r'(\\\{(?:\s*"word"\s*:.*?|pause:\d+ms)\\\})') + parts = control_split.split(ssml_text) + ssml_text = "".join( + part if i % 2 == 1 else html.unescape(part) for i, part in enumerate(parts) + ) + ssml_text = ssml_text.strip() pronunciation_count, pause_count = _control_counts(ssml_text) if pronunciation_count > 500: