Fix speech text truncation bug where Assistant: prefix caused multi-line responses to drop remaining sentences

This commit is contained in:
Adolfo Reyna
2026-08-07 20:04:34 -04:00
parent af15e64e0a
commit ba16e9ca27
3 changed files with 12 additions and 6 deletions
+4 -2
View File
@@ -92,8 +92,10 @@ def _clean_spoken_text(text: str) -> str:
"""Clean text for speech output and truncate fake turn generations.""" """Clean text for speech output and truncate fake turn generations."""
if not text: if not text:
return "" return ""
# Truncate if model hallucinates fake turn markers # Strip leading or inline role headers (e.g. "Assistant:")
for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): text = re.sub(r"(?i)\b(Assistant|assistant|Bot|bot):\s*", "", text)
# Truncate if model hallucinates fake subsequent user turns
for marker in ("\nUser:", "\nHuman:", "\nUser", "\nHuman"):
if marker in text: if marker in text:
text = text.split(marker)[0] text = text.split(marker)[0]
# Remove markdown code blocks # Remove markdown code blocks
+4 -2
View File
@@ -42,8 +42,10 @@ def _clean_spoken_text(text: str) -> str:
"""Clean text for speech output and truncate fake turn generations.""" """Clean text for speech output and truncate fake turn generations."""
if not text: if not text:
return "" return ""
# Truncate if model hallucinates fake turn markers # Strip leading or inline role headers (e.g. "Assistant:")
for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): text = re.sub(r"(?i)\b(Assistant|assistant|Bot|bot):\s*", "", text)
# Truncate if model hallucinates fake subsequent user turns
for marker in ("\nUser:", "\nHuman:", "\nUser", "\nHuman"):
if marker in text: if marker in text:
text = text.split(marker)[0] text = text.split(marker)[0]
# Remove markdown code blocks # Remove markdown code blocks
+4 -2
View File
@@ -46,8 +46,10 @@ def _clean_spoken_text(text: str) -> str:
"""Clean text for speech output and truncate fake turn generations.""" """Clean text for speech output and truncate fake turn generations."""
if not text: if not text:
return "" return ""
# Truncate if model hallucinates fake turn markers # Strip leading or inline role headers (e.g. "Assistant:")
for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): text = re.sub(r"(?i)\b(Assistant|assistant|Bot|bot):\s*", "", text)
# Truncate if model hallucinates fake subsequent user turns
for marker in ("\nUser:", "\nHuman:", "\nUser", "\nHuman"):
if marker in text: if marker in text:
text = text.split(marker)[0] text = text.split(marker)[0]
# Remove markdown code blocks # Remove markdown code blocks