Skip to content

Commit 8708fc4

Browse files
committed
Fix streaming render bugs in chat and query
Two issues in the Live-based markdown streaming: 1. After a tool call, the new Live instance re-rendered the entire response so far because `collected` was never reset. Text before and after the tool call would be rendered together as one markdown block in the new Live region, with no separator between the two parts. Track the current segment separately from the full answer. 2. The tool_call_item handler started a new Live immediately after printing the tool line, which then had to be stopped in the text handler before the blank line. The empty Live start/stop plus the explicit `print()` produced two blank lines. Drop the premature start and let the next text delta create the new Live.
1 parent 8cdc5a7 commit 8708fc4

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

openkb/agent/chat.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ async def _run_turn(
238238

239239
print()
240240
collected: list[str] = []
241+
segment: list[str] = []
241242
last_was_text = False
242243
need_blank_before_text = False
243244

@@ -266,18 +267,18 @@ def _start_live() -> Any:
266267
text = event.data.delta
267268
if text:
268269
if need_blank_before_text:
269-
if live:
270-
live.stop()
271-
live = None
270+
if console is not None:
272271
print()
272+
segment = []
273273
live = _start_live()
274274
else:
275275
sys.stdout.write("\n")
276276
need_blank_before_text = False
277277
collected.append(text)
278+
segment.append(text)
278279
last_was_text = True
279280
if live:
280-
live.update(Markdown("".join(collected), code_theme="monokai"))
281+
live.update(Markdown("".join(segment), code_theme="monokai"))
281282
else:
282283
sys.stdout.write(text)
283284
sys.stdout.flush()
@@ -299,7 +300,6 @@ def _start_live() -> Any:
299300
live.stop()
300301
live = None
301302
_fmt(style, ("class:tool", _format_tool_line(name, args) + "\n"))
302-
live = _start_live()
303303
need_blank_before_text = True
304304
finally:
305305
if live:

openkb/agent/query.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,17 @@ def _start_live() -> Live | None:
142142

143143
result = Runner.run_streamed(agent, question, max_turns=MAX_TURNS)
144144
collected: list[str] = []
145+
segment: list[str] = []
145146
try:
146147
async for event in result.stream_events():
147148
if isinstance(event, RawResponsesStreamEvent):
148149
if isinstance(event.data, ResponseTextDeltaEvent):
149150
text = event.data.delta
150151
if text:
151152
collected.append(text)
153+
segment.append(text)
152154
if live:
153-
live.update(Markdown("".join(collected), code_theme="monokai"))
155+
live.update(Markdown("".join(segment), code_theme="monokai"))
154156
else:
155157
sys.stdout.write(text)
156158
sys.stdout.flush()
@@ -164,6 +166,7 @@ def _start_live() -> Live | None:
164166
live = None
165167
sys.stdout.write(f"\n[tool call] {raw.name}({args})\n\n")
166168
sys.stdout.flush()
169+
segment = []
167170
live = _start_live()
168171
elif item.type == "tool_call_output_item":
169172
pass

0 commit comments

Comments
 (0)