From e451d2dae468f958db5261f6b2d71333ccc8ebe4 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 27 Mar 2026 14:15:54 +0000 Subject: [PATCH] fix: only strip closed tool-call XML pairs in SSE output The open-ended .*$ regex was stripping everything from to end of string, including legitimate model text that followed a tool block. This caused the stream to appear stuck since the cumulative text never grew past the strip point. Switch to only stripping closed ... pairs so text after a tool block is preserved. --- studio/backend/routes/inference.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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: