Client.Next cancellation via context + empty events fix#18
Merged
Conversation
Updates Next() to accept context.Context for improved streaming control. - Update test calls with context.Background() - Remove deprecated empty event error handling tests
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.
Summary
This PR introduces context-aware cancellation to streaming and refines handling of empty chunks in the OpenAI connector. The
Streaminterface has been updated to acceptcontext.ContextinNext, enabling deterministic cancellation and improved control in concurrent scenarios.Empty SSE chunks no longer produce artificial errors. Instead, they are skipped transparently, preventing premature stream termination.
The changes affect the
chatcore and theconnect/openaiimplementation, with corresponding test updates.Changes
1. Stream interface update (breaking change)
All implementations and call sites were updated accordingly.
2. OpenAIStream improvements
Next(ctx)now checksctx.Done()before attempting to fetch the next SSE chunk.s.err = ctx.Err()."empty events"error fromhandleRawChunk.This ensures:
3. Chat integration
Chat.Completenow propagatesctxintostream.Next(ctx), allowing external cancellation to stop streaming immediately.Tests
connect/openai
Removed
TestHandleRawChunkDecorator_EmptyEvents_ReturnsErrorTestNext_EmptyChunk_StopsWithEmptyEventsErrorThese tests validated behavior that is no longer correct (empty chunks are not errors).
Added
TestNext_ContextCancellation_StopsStreamctxis canceled,Next(ctx)stops and returnscontext.CanceledAll related mocks were updated to support the new
Next(ctx)signature.