|
| 1 | +# Async Streaming Migration Guide |
| 2 | + |
| 3 | +This document helps teams migrate from the legacy async streaming hooks to the current streaming architecture introduced in `pipeline_ex` v0.3. |
| 4 | + |
| 5 | +## 1. Migration Checklist |
| 6 | + |
| 7 | +- Audit pipelines for `Pipeline.AsyncStreaming.Handler` usage. |
| 8 | +- Replace handler modules with the new `Pipeline.Streaming.*` namespace. |
| 9 | +- Confirm each LLM step sets `stream: true` in YAML or step configs. |
| 10 | +- Run `mix test --only streaming` to validate the new flow. |
| 11 | + |
| 12 | +## 2. Architectural Changes |
| 13 | + |
| 14 | +| Area | Legacy System | Current System | Migration Notes | |
| 15 | +| --- | --- | --- | --- | |
| 16 | +| Event shape | `{:chunk, binary}` | `{:delta, %{content: binary}}` | Update pattern matches to extract `%{content: chunk}`. | |
| 17 | +| Final callback | `{:complete, result}` | `{:done, response}` | Final responses now carry metadata such as tokens and finish reason. | |
| 18 | +| Handler state | Global Agent | Lightweight module functions | Move mutable state into per-run processes (e.g., Task or GenServer). | |
| 19 | +| Configuration | `async_stream: true` | `stream: true` | Remove the `async_` prefix everywhere. | |
| 20 | + |
| 21 | +## 3. Updating Elixir Steps |
| 22 | + |
| 23 | +```elixir |
| 24 | +# BEFORE |
| 25 | +Pipeline.AsyncStreaming.invoke!( |
| 26 | + prompt: ctx.prompt, |
| 27 | + handler: Pipeline.AsyncStreaming.ConsoleHandler |
| 28 | +) |
| 29 | + |
| 30 | +# AFTER |
| 31 | +Pipeline.LLM.invoke!( |
| 32 | + ctx, |
| 33 | + prompt: ctx.prompt, |
| 34 | + stream: true, |
| 35 | + on_event: &Pipeline.Streaming.ConsoleHandler.handle_event/1 |
| 36 | +) |
| 37 | +``` |
| 38 | + |
| 39 | +- Use `Pipeline.Streaming.Handler` behaviour for shared handlers. |
| 40 | +- Prefer function captures (`&Module.handle_event/1`) over anonymous functions so the handler remains testable. |
| 41 | + |
| 42 | +## 4. YAML Workflow Changes |
| 43 | + |
| 44 | +```yaml |
| 45 | +# BEFORE |
| 46 | +- id: draft_report |
| 47 | + type: llm |
| 48 | + provider: claude |
| 49 | + async_stream: true |
| 50 | + async_handler: Pipeline.AsyncStreaming.ConsoleHandler |
| 51 | + |
| 52 | +# AFTER |
| 53 | +- id: draft_report |
| 54 | + type: llm |
| 55 | + provider: claude |
| 56 | + stream: true |
| 57 | + stream_handler: Pipeline.Streaming.ConsoleHandler |
| 58 | +``` |
| 59 | +
|
| 60 | +Make sure the handler module is compiled and available when running `mix pipeline.run`. |
| 61 | + |
| 62 | +## 5. Testing Strategy |
| 63 | + |
| 64 | +- **Unit tests**: use the `Pipeline.TestMode.StreamingRecorder` helper to capture deltas without hitting real providers. |
| 65 | +- **Integration tests**: tag scenarios that depend on live streaming with `@tag :streaming` and execute via `mix test --only streaming`. |
| 66 | +- **Diagnostics**: set `PIPELINE_STREAM_DEBUG=1` to print raw events during runs. |
| 67 | + |
| 68 | +## 6. Common Issues |
| 69 | + |
| 70 | +- **Handler not invoked**: confirm the module implements `handle_event/1` and is referenced correctly in YAML. |
| 71 | +- **Duplicate output**: most often caused by reusing the same handler process; ensure each run starts a fresh handler. |
| 72 | +- **Fallback to sync execution**: some providers disable streaming for certain models; verify the chosen model supports it. |
| 73 | + |
| 74 | +## 7. Further Reading |
| 75 | + |
| 76 | +- `examples/STREAMING_GUIDE.md` for a quick-start implementation. |
| 77 | +- `docs/ASYNC_STREAMING_ASSESSMENT.md` for evaluation notes on the old system. |
| 78 | +- `docs/ASYNC_STREAMING_EVALUATION_REPORT.md` for design trade-offs and roadmap. |
0 commit comments