Skip to content

Workflow Streams samples #299

Closed
jssmith wants to merge 5 commits intomainfrom
pubsub-samples
Closed

Workflow Streams samples #299
jssmith wants to merge 5 commits intomainfrom
pubsub-samples

Conversation

@jssmith
Copy link
Copy Markdown
Contributor

@jssmith jssmith commented Apr 30, 2026

Add samples to accompany Workflow Streams

jssmith and others added 5 commits April 28, 2026 21:10
Mirrors the contrib rename in sdk-python (commit 5890c589) where
temporalio.contrib.pubsub became temporalio.contrib.workflow_stream and
PubSub/PubSubClient/PubSubState became WorkflowStream/WorkflowStreamClient/
WorkflowStreamState.

samples directory pubsub/ -> workflow_stream/, and the openai_agents
streaming sample updates its imports/usages. The carrier field on the
workflow input dataclass goes from pubsub_state to stream_state, and the
sample task queue and starter workflow id prefix lose the pubsub label.

The contrib branch reference (contrib/pubsub) on sdk-python stays as-is
per the rename doc — only the module/class/file names rotate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds a second scenario demonstrating the central Workflow Streams use
case: a consumer disconnects mid-stream and resumes later via
subscribe(from_offset=...), with no events lost or duplicated. The
existing OrderWorkflow finishes too quickly to make the pattern
visible, so this introduces a multi-stage PipelineWorkflow paced with
workflow.sleep between stages.

The runner reads a couple of events, persists item.offset + 1 to a
temp file, sleeps "disconnected" while the workflow keeps publishing,
then opens a fresh Client + WorkflowStreamClient and resumes from the
persisted offset — the same shape that works across actual process
restarts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds a third scenario covering the third publisher shape: a backend
service or scheduled job pushing events into a workflow it didn't
itself start. The earlier scenarios publish either from inside the
workflow or from one of its activities; this one uses
WorkflowStreamClient.create() externally.

HubWorkflow is a passive stream host — it does no work of its own and
just waits to be told to close, fitting the event-bus pattern. The
runner publishes a series of news headlines, runs a subscriber task
alongside, signals close, and exits when both tasks complete.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds a fourth scenario for long-running workflows that need to bound
their event log: the workflow publishes events at a fixed cadence and
calls self.stream.truncate(...) periodically to keep only the most
recent entries.

The runner subscribes twice — fast and slow — to make the trade
visible: the fast subscriber sees every offset in order; the slow one
falls behind a truncation, has its iterator transparently jump forward
to the new base offset, and shows the offset gap that intermediate
events fell into. This is the model for high-volume long-running
streams: bounded log size, slow consumers may miss intermediate events
but always see the most recent state.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…handles

- Directory and module path renamed to plural to match sdk-python
  `temporalio.contrib.workflow_streams` rename.
- Workflow-side: bind a typed topic handle in `@workflow.init` and call
  `topic.publish(value)` — the removed `WorkflowStream.publish` form is
  gone. Same change applied to the activity and external-publisher.
- Activity: `WorkflowStreamClient.from_activity()` →
  `from_within_activity()`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jssmith jssmith requested a review from a team as a code owner April 30, 2026 03:01
@jssmith jssmith changed the title Pubsub samples Workflow Streams samples Apr 30, 2026
@jssmith
Copy link
Copy Markdown
Contributor Author

jssmith commented Apr 30, 2026

Superseded by #300 (workflow_streams basics) and #301 (openai_agents streaming sample), which split this PR into two reviewable halves off a common parent.

@jssmith jssmith closed this Apr 30, 2026
@jssmith jssmith deleted the pubsub-samples branch April 30, 2026 16:34
@jssmith jssmith restored the pubsub-samples branch April 30, 2026 16:36
@jssmith jssmith deleted the pubsub-samples branch April 30, 2026 16:36
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