From fa843fba910a3c1b26bed6667d380848af2c064a Mon Sep 17 00:00:00 2001 From: manoj-025 Date: Wed, 2 Dec 2026 01:59:18 +0530 Subject: [PATCH] fix(helpers): decode XML entities in ssml_to_deepgram --- src/deepgram/helpers/text_builder.py | 6 ++++-- tests/custom/test_client_construction_and_helpers.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/deepgram/helpers/text_builder.py b/src/deepgram/helpers/text_builder.py index 2e1d5251..e006686a 100644 --- a/src/deepgram/helpers/text_builder.py +++ b/src/deepgram/helpers/text_builder.py @@ -5,6 +5,7 @@ and speed controls for Deepgram's Text-to-Speech API. """ +import html import json 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) @@ -345,6 +346,7 @@ def replace_break(match): # Remove any remaining XML tags ssml_text = re.sub(r"<[^>]+>", "", ssml_text) + ssml_text = html.unescape(ssml_text) ssml_text = ssml_text.strip() pronunciation_count, pause_count = _control_counts(ssml_text) diff --git a/tests/custom/test_client_construction_and_helpers.py b/tests/custom/test_client_construction_and_helpers.py index eb72c9d3..032e9cd0 100644 --- a/tests/custom/test_client_construction_and_helpers.py +++ b/tests/custom/test_client_construction_and_helpers.py @@ -126,3 +126,13 @@ def test_add_pronunciation_and_ssml() -> None: def test_text_builder_from_ssml_updates_counts() -> None: builder = TextBuilder().from_ssml('Hi there') assert r"\{pause:500ms\}" in builder.build() + + +def test_ssml_to_deepgram_decodes_xml_entities() -> None: + result = ssml_to_deepgram("Call AT&T, it's open "now".") + assert result == 'Call AT&T, it\'s open "now".' + + +def test_ssml_to_deepgram_decodes_phoneme_entities() -> None: + result = ssml_to_deepgram('AT&T') + assert result == r'\{"word": "AT&T", "pronounce": "tÉ™"\}'