Fix empty content deltas and leaked section markers in streaming

Tool parser:
- Case 3/4: return None instead of DeltaMessage(content='') when
  inside an open tool section with no parseable content yet.
  Empty-string content deltas pollute the response and break the
  content=null vs content='' contract with non-streaming.

Reasoning parser:
- Suppress tool-calls section markers from content forwarding.
  The tool parser detects them via current_text re-parsing; forwarding
  them as content causes double-handling.
- Already-past-reasoning path: strip section markers from content
  for the same reason.
This commit is contained in:
2026-04-14 06:46:19 +00:00
parent fcf8fd134e
commit a404735b2d
2 changed files with 19 additions and 9 deletions

View File

@@ -522,12 +522,17 @@ class KimiK2ToolParser(ToolParser):
pre_section = current_text[len(previous_text):section_start_in_text]
if pre_section.strip():
return DeltaMessage(content=pre_section)
return DeltaMessage(content="")
# No real content before the section — return None instead of
# an empty-string delta. Empty content deltas confuse clients
# that distinguish content=null from content="".
return None
# Case 4: Inside an open tool section but tool calls aren't
# parseable yet — emit empty delta to keep the stream alive.
# parseable yet — return None. The serving layer will emit
# its own keep-alive if needed; we should not emit empty-string
# content deltas that pollute the response.
if in_open_section:
return DeltaMessage(content="")
return None
# Case 5: Section is closed and we're past it — forward any
# new content that appeared after the section end marker.