Make NodeRunStreamChunkEvent immutable

Open
#438 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
50/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start with the NodeRunStreamChunkEvent definition in the graphon engine_events node module, then read how ResponseStreamFilter buffers and forwards events in src/graphon/engine/filter/builtin/response_stream/template_stream.py. Run the reproduction script against tests/engine/test_response_stream_filter.py helpers to confirm the shared-reference checkpoint. Done when assigning to event.chunk fails, the downstream copy pattern still works, and the existing response-filter tests pass.

Written by the indexing model from the issue text.

Description

project#graphon

AI disclosure — I discussed this behavior with Codex (gpt-6.2-astra), which verified the example and helped draft this report. I am responsible for the report.

Description

ResponseStreamFilter can yield a NodeRunStreamChunkEvent while keeping a reference to that same object in its root stream buffer. A later filter can assign a new string to event.chunk. The next checkpoint of the earlier filter then contains the downstream edit. Python strings are immutable, but the event allows its chunk field to be reassigned. Buffer insertion · Forwarding the event · Serializing the buffer

Minimal reproduction

Run from the Graphon repository root. The example uses the existing response-filter test graph and one non-final source chunk. EscapeText is a custom filter added for this reproduction. It consumes the same public filter API a host can use.

PYTHONPATH=.:src uv run python - <<'PY'
import json
from html import escape

from graphon.engine.filter import ResponseStreamFilter, filter_engine_events
from graphon.engine_events.graph import GraphRunStartedEvent
from graphon.engine_events.node import NodeRunStreamChunkEvent
from tests.engine.test_response_stream_filter import (
    _context, _stream_chunk, _variable_response_graph,
)

class EscapeText:
    def initialize(self, context):
        pass

    def on_event(self, event):
        if isinstance(event, NodeRunStreamChunkEvent):
            event.chunk = escape(event.chunk)
        return [event]

    def flush(self):
        return []

selector = ["source", "text"]
response = ResponseStreamFilter()
events = filter_engine_events(
    [GraphRunStartedEvent(), _stream_chunk("&", selector=selector, is_final=False)],
    context=_context(_variable_response_graph(selector=selector)),
    filters=[response, EscapeText()],
)
print("display:", [e.chunk for e in events if isinstance(e, NodeRunStreamChunkEvent)])
buffer = json.loads(response.dumps())["stream_buffers"][0]
print("checkpoint:", buffer["events"][0]["chunk"])
PY

Expected and actual behavior

The source emits &. A display filter should be able to produce &amp; while the upstream response filter retains the & it observed. For a control, change just the assignment in EscapeText.on_event to event = event.model_copy(update={"chunk": escape(event.chunk)}).

Downstream transformation Displayed chunk Chunk in the upstream checkpoint
Assign to event.chunk &amp; &amp;
Return an updated copy &amp; &

Verified both rows on Python 3.12.12 at main (b79228b) and on #395 (d4032bf). The checkpoint example works with a single reference on main. Replaying a reference makes the same effect visible in the reply on #395: for source text &a and template {{#llm.text#}}/{{#llm.text#}}, the custom in-place filter produced &amp;a/&amp;amp;a. Returning a copy produced &amp;a/&amp;a.

Proposed change

I propose making NodeRunStreamChunkEvent immutable from construction. A filter can forward the event it received or return a new event when it transforms the text, for example event.model_copy(update={"chunk": escape(event.chunk)}). An attempt to assign to event.chunk should fail at the point of assignment.

With this contract, ResponseStreamFilter can retain the source event for checkpointing and replay. In the example above, the downstream consumer receives &amp; and the upstream checkpoint still contains &. This proposal can be handled independently of #395.

Dominant language
Python
Stars
90
Forks
47
Avg merge
3d 23h
Merged PRs (30d)
19

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from langgenius/graphon

All issues in langgenius/graphon

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.