Skip to content

fix(response-stream): replay closed stream for subsequent root Answer nodes - #449

Open
tang-ontheway wants to merge 1 commit into
langgenius:mainfrom
tang-ontheway:fix/second-root-answer-replay-stream
Open

tang-ontheway wants to merge 1 commit into
langgenius:mainfrom
tang-ontheway:fix/second-root-answer-replay-stream

Conversation

@tang-ontheway

Copy link
Copy Markdown

Summary

Ensure a second root Answer node referencing a previously streamed variable replays the buffered text instead of omitting it when the first Answer already consumed the closed stream.

Fixes #435

Root Cause

_TemplateStream._read_root_variable_chunks() only replayed buffered_events on a closed stream when selector in session.repeated_selectors (i.e., the same selector appeared twice inside a single Answer template). A subsequent root Answer node referencing the same selector once has selector not in session.repeated_selectors, saw the exhausted selector cursor, and skipped its placeholder.

Changes

  • src/graphon/engine/filter/builtin/response_stream/template_stream.py: Track per-segment reads in session.segment_positions[session.index] inside _read_root_variable_chunks() and replay buffered_events whenever the stream is closed, no new unread events were popped, and this segment has not yet consumed any chunks (session.segment_positions.get(session.index, 0) == 0).
  • tests/engine/test_response_stream_filter.py: Update test_response_stream_filter_repeats_variable_text_within_one_reply so streamed and non-streamed paths both assert "value/value!next:value", and add test_response_stream_filter_replays_closed_stream_for_second_root_answer covering LLM -> First Answer -> Second Answer.

Verification

  • uv run ruff format --check & uv run ruff check (passed)
  • uv run pytest tests/ -k response_stream -q (152 passed)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Second root Answer omits LLM text already streamed by the first Answer

1 participant