feat(hooks): add BeforeStreamChunkEvent for stream chunk interception#1761
Open
fede-kamel wants to merge 1 commit intostrands-agents:mainfrom
Open
feat(hooks): add BeforeStreamChunkEvent for stream chunk interception#1761fede-kamel wants to merge 1 commit intostrands-agents:mainfrom
fede-kamel wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…ption Add BeforeStreamChunkEvent hook that fires BEFORE each stream chunk is processed, enabling true interception capabilities: - Monitor streaming progress in real-time - Modify chunk content before processing (affects final message) - Skip chunks entirely by setting skip=True (excluded from final message) - Implement content transformation (e.g., redaction, translation) Implementation details: - Added ChunkInterceptor callback type to streaming.py - Modified process_stream() to invoke interceptor BEFORE processing - Modified stream_messages() to accept chunk_interceptor parameter - event_loop.py creates interceptor that invokes BeforeStreamChunkEvent When skip=True: - The chunk is not processed at all - No events (ModelStreamChunkEvent, TextStreamEvent) are yielded - The chunk does not contribute to the final message When chunk is modified: - The modified chunk is used for all downstream processing - TextStreamEvent will contain the modified text - The final message will contain the modified content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
There's currently no way to intercept stream chunks before they're processed. Hooks fire after processing, so modifications don't affect the TextStreamEvent or final message content.
The current hook lifecycle has a gap during streaming:
BeforeModelCallEventfires before chunks exist.AfterModelCallEventfires after chunks have already been streamed to the user. Neither allows chunk interception.This adds
BeforeStreamChunkEventthat fires before each chunk is processed, allowing:skip=True(excludes from all events and final message)invocation_statefor contextResolves #1760
Use Cases
skip=True)Public API Changes
New event:
BeforeStreamChunkEventProperties:
chunk(writable) - the raw stream chunkskip(writable) - set True to exclude chunk from processinginvocation_state(read-only) - context dict from invocationReproduction and Demonstration
Issue reproduction (main branch): https://gist.github.com/fede-kamel/9d93429a3d6d7676906e41eb60890d46
Working demonstration (feature branch): https://gist.github.com/fede-kamel/a6a78d8d35eb9707d80c2327cd17b462
Type of Change
New feature
Testing
hatch run prepare(lint, format, tests all pass)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.