From ba16e9ca27a0af1eebd13a87bf877ae496664e15 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Fri, 7 Aug 2026 20:04:34 -0400 Subject: [PATCH] Fix speech text truncation bug where Assistant: prefix caused multi-line responses to drop remaining sentences --- apple_llm.py | 6 ++++-- ollama_llm.py | 6 ++++-- opencode_llm.py | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/apple_llm.py b/apple_llm.py index 7bba0aa..a790d96 100644 --- a/apple_llm.py +++ b/apple_llm.py @@ -92,8 +92,10 @@ def _clean_spoken_text(text: str) -> str: """Clean text for speech output and truncate fake turn generations.""" if not text: return "" - # Truncate if model hallucinates fake turn markers - for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): + # Strip leading or inline role headers (e.g. "Assistant:") + 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: text = text.split(marker)[0] # Remove markdown code blocks diff --git a/ollama_llm.py b/ollama_llm.py index e33f758..7ee220f 100644 --- a/ollama_llm.py +++ b/ollama_llm.py @@ -42,8 +42,10 @@ def _clean_spoken_text(text: str) -> str: """Clean text for speech output and truncate fake turn generations.""" if not text: return "" - # Truncate if model hallucinates fake turn markers - for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): + # Strip leading or inline role headers (e.g. "Assistant:") + 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: text = text.split(marker)[0] # Remove markdown code blocks diff --git a/opencode_llm.py b/opencode_llm.py index 70fd1f2..872f680 100644 --- a/opencode_llm.py +++ b/opencode_llm.py @@ -46,8 +46,10 @@ def _clean_spoken_text(text: str) -> str: """Clean text for speech output and truncate fake turn generations.""" if not text: return "" - # Truncate if model hallucinates fake turn markers - for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): + # Strip leading or inline role headers (e.g. "Assistant:") + 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: text = text.split(marker)[0] # Remove markdown code blocks