Skip to content

feat: add the Streams API for client, Activity and Workflow code - #2129

Draft
moetemp wants to merge 18 commits into
moe/streams-generated-codefrom
moe/streams-api
Draft

moetemp wants to merge 18 commits into
moe/streams-generated-codefrom
moe/streams-api

Conversation

@moetemp

@moetemp moetemp commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What changed?

This PR adds the hand-written part of the experimental Streams API. The public API is the code that nexgen generates in #2128, and this PR adds what that code calls.

A Stream is an ordered log of Records, such as the tokens of a model call.

from temporalio_streams_redis import RedisStreamStore

store = RedisStreamStore("redis://localhost:6379")
client = await Client.connect("localhost:7233", stream_config=StreamConfig(stores=[store]))

stream = await client.create_stream(id="chat-43", body_type=str)
await stream.append("Hello", " world")
async for record in stream.read():
    print(record.body)
await stream.close()

It needs Core's Stream PRs (temporalio/sdk-rust#1817, temporalio/sdk-rust#1801, temporalio/sdk-rust#1819 and temporalio/sdk-rust#1833), which have no release yet. The Workflow part is on an earlier revision of them, and it gets their current rules for a read with the next Core bump.

check-protos fails. scripts/gen_nexus_system_api.py doesn't run nexgen on the Stream WIT yet, so the generated package of #2128 comes from a local run. The fix needs a nexgen release with temporalio/nexgen#437.

Why?

An agent application shows partial output while the work still runs. That data doesn't belong in Workflow History, so a Stream keeps its Records in a Store outside it.

These points are open.

  • One commit turns Workflow Task chunking off for new Workflows (Add opt-in workflow task chunking shutoff sdk-rust#1659). It can move to a separate PR.
  • A Stream call of a client goes through start_nexus_operation of the client interceptors, so a tracing interceptor sees a Nexus operation for each append.

Reviewer's guide

About 4,000 of the ~7,100 added lines are tests.

  1. client/_stream_store.py, for StreamStore (the contract of a Store) and StreamConfig.
  2. nexus/system/_support.py, the helpers that the generated code calls.
  3. worker/_workflow_streams.py and bridge/src/streams.rs, for the Stream calls of Workflow code.
  4. converter/_payload_converter.py. A StreamRef[T] keeps its type as a dataclass field, so the JSON converter decodes by a type argument, only for StreamRef.
  5. contrib/streams and testing/_stream_store.py.

How did you test it?

tests/nexus/test_streams.py covers client and Activity code with seeded inputs against a model. tests/worker/test_workflow_streams.py and tests/nexus/test_streams_with_worker.py run Workflows on the test server, with a replay.

Link to a test plan if any -

  • Unit Tests
  • Staging
  • End to End Tests

@moetemp
moetemp added this pull request to stack #2130 November 14, 2026 01:00
@moetemp
moetemp removed this pull request from stack #2130 November 15, 2026 08:27
@moetemp moetemp changed the title feat: add the Streams API, with Stream Stores on Redis and in memory feat: add the Streams API for Activity and client code, with Stores on Redis and in memory Nov 17, 2026
@moetemp
moetemp added this pull request to stack #2139 November 18, 2026 01:19
@moetemp moetemp changed the title feat: add the Streams API for Activity and client code, with Stores on Redis and in memory feat: add the Streams API for Activity and client code Nov 18, 2026
@moetemp moetemp changed the title feat: add the Streams API for Activity and client code feat: add the Streams API for client, Activity and Workflow code Nov 22, 2026
@moetemp
moetemp force-pushed the moe/streams-api branch 2 times, most recently from 4f48bab to 066b280 Compare November 22, 2026 03:31
The Workflow path of Streams needs the Stream commands and jobs of this
Core. The payload visitor reaches their payloads, so its generator gets
the rule for a Stream body in this commit.
A Stream Store is the handler side of the Stream service, which the
generated code doesn't have. The memory Store lets a test use Streams
with no other service, behind the checks that Core runs for a Store of
Workflow code.
The server has no Streams yet, so the last client interceptor sends each
Stream call to a Store of the client's Stream config. The generated
Stream code also needs its helpers, with the three that build a
Producer.
Workflow code can't call a Store itself, since a replay must get the
same answers. Core makes the append and the close inside the Workflow
Task and records each result in a marker. A create and a describe have
no command in Core, so they run in a Local Activity of the SDK.
A read of Workflow code is one command of Core with one job, as a Local
Activity is, so no state of a read depends on where an activation ends.
A replay reads the Records again from the Store, so a replay Worker gets
the event loop that its Python Stores need.
An Activity that appends each model token pays one Store call for each
token. The appender batches them with one write in flight, and stops at
the first failed batch so that a reader sees no gap.
A `StreamRef[T]` lost its type as a dataclass field and a list item,
and came back as its proto message under `T | None`. Only `StreamRef`
takes the two new decoding paths, since a change for any other type
would alter the replay of existing Workflow Histories.
A Stream read that waits past the heartbeat of its Workflow Task
completes that task with no command, and with chunking a replay takes
that task and the next one as one. Workflow code then gets two results
in one activation that the first run gave in two, and can run in another
order, as it can with Local Activities alone. Core records the choice in
the first completed Workflow Task of a Workflow, so a Workflow that
started before keeps chunking.
The generated Workflow `append` and `close` take no `producer`. The
`StreamConfig` docs say which Producer a Workflow write has, in place of
a note on an argument that is gone.
Plain client code has no target, so its selector couldn't pick a Store
by namespace. Each path passes the namespace of its client or Worker,
as external storage does for `StorageDriverSelectContext`.
Core's Stream crate takes a ref in each request and the request id of a
create or an append next to it, keeps the Stream calls of a Workflow
Task on the task's completion, and gives an Activity task the id of its
scheduled event. The bridge builds on it with the next commit.
The Stream API has a ref in each request and keeps the request id of a
call out of it. A Store gets the request id as an argument, which client
code makes for each call and Core makes for each append of Workflow
code. The bridge turns a call that can't reach Python into an error,
since Core doesn't catch a panic of a Store.
The generated Stream code converts a ref itself and sends no request id,
so `_support` has no use for these four helpers.
A run can use an Activity id again, and the id of the scheduled event
makes the two Activities separate Producers. Core reads it from the task
token, and `activity.Info` keeps it in a private field next to the
Workflow Task of a Local Activity.
Core gives each page the offset where the next read goes on, also a
page with no Record, so Workflow code reads on from there as client
code does.
A Store can leave the last_offset of a read page unset. Client and
Activity code then get the offset of the last Record, or the
from_offset of a page with no Record, as Workflow code does from Core.
The rules of each operation are now in the doc of its method. The test
of a limit below one only repeated the check of the config, so it goes.

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.

1 participant