Streaming responses example
This walkthrough consumes incremental stream events from the SDK during workflow execution. You iterate run_stream() (Python) or runStream() (TypeScript) and handle token, step start, and step complete events. Server-side SSE remains deferred (streaming deferred); this is in-process SDK streaming only.
Scripts: Streaming responses walkthrough, Streaming responses walkthrough.
What this example demonstrates
Streaming exposes partial output and step lifecycle events before the final WorkflowResult is assembled. Useful for CLI chat UX and local tooling. Both language samples use the stub provider so no API keys are required.
Prerequisites
| Item | Python | TypeScript |
|---|---|---|
| SDK built | maturin develop in sdk-python/ | npm run build in sdk-typescript/ |
| API keys | Not required (stub) | Not required (stub) |
| Async runtime | asyncio (stdlib) | Node 20+ with tsx or compiled output |
| Reading | SDK streaming | Same |
Step 1: Python streaming run
Core loop:
Step 2: TypeScript streaming run
From repository root after TypeScript SDK build:
Equivalent pattern:
Step 3: Verify event count
Both scripts raise if zero events arrive. Expect at least one step_start and one step_complete on the stub path, plus token events when the provider emits them.
Expected output
Exact token text varies. Pass criteria: non-zero event count, no uncaught exceptions, terminal Done. line.
Trace events you should see
Streaming complements but does not replace trace export. After the stream finishes, call workflow.run() synchronously only if you have not already consumed the run, or inspect trace on the same workflow instance if your SDK version attaches trace after stream completion.
Typical post-run trace kinds:
| Event kind | When |
|---|---|
WorkflowStarted | Run begins |
StepStarted | Step begins (mirrors stream step_start) |
StepCompleted | Step ends (mirrors stream step_complete) |
WorkflowCompleted | Success |
Stream iterators may surface token deltas before StepCompleted appears in trace export.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Expected at least one stream event | Provider or binding without stream support | Confirm stub path; rebuild SDK |
| TypeScript import error | SDK not built | Run npm run build in sdk-typescript/ |
| Empty token events on stub | Normal for some stub versions | Step events still validate streaming plumbing |
| Expecting HTTP SSE from server | Server SSE not implemented | Use SDK streaming or poll run status |
Related
| Resource | Link |
|---|---|
| SDK streaming guide | sdk-streaming |
| Browser streaming note | streaming in the browser |
| Server streaming gap | maturity and known gaps |