diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py index 99910808f8..40e35cc0f6 100644 --- a/studio/backend/routes/inference.py +++ b/studio/backend/routes/inference.py @@ -1200,11 +1200,18 @@ async def openai_chat_completions( # "content" type -- cumulative text cumulative = event.get("text", "") - # Strip tool-call XML that may have leaked - # through the backend's content stream. - for pat in _tool_xml_strip: - cumulative = pat.sub("", cumulative) - cumulative = cumulative.rstrip() + # Strip closed tool-call XML pairs that may + # have leaked through the backend stream. + # Only strip closed pairs here (not open-ended) + # so legitimate text after a tool block is kept. + cumulative = _re.sub( + r".*?", "", + cumulative, flags = _re.DOTALL, + ) + cumulative = _re.sub( + r".*?", "", + cumulative, flags = _re.DOTALL, + ) new_text = cumulative[len(prev_text) :] prev_text = cumulative if not new_text: