Skip to content

Client.Next cancellation via context + empty events fix#18

Merged
x2d7 merged 3 commits intodevfrom
17-error-empty-events
Feb 19, 2026
Merged

Client.Next cancellation via context + empty events fix#18
x2d7 merged 3 commits intodevfrom
17-error-empty-events

Conversation

@x2d7
Copy link
Copy Markdown
Owner

@x2d7 x2d7 commented Feb 19, 2026

Summary

This PR introduces context-aware cancellation to streaming and refines handling of empty chunks in the OpenAI connector. The Stream interface has been updated to accept context.Context in Next, 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 chat core and the connect/openai implementation, with corresponding test updates.


Changes

1. Stream interface update (breaking change)

type Stream[T any] interface {
    Next(ctx context.Context) bool
    Current() T
    Err() error
    Close() error
}

All implementations and call sites were updated accordingly.

2. OpenAIStream improvements

  • Next(ctx) now checks ctx.Done() before attempting to fetch the next SSE chunk.
  • On cancellation, the stream stops and sets s.err = ctx.Err().
  • Empty parsed event batches are skipped recursively instead of treated as errors.
  • Removed artificial "empty events" error from handleRawChunk.

This ensures:

  • Correct cancellation semantics.
  • No unexpected failures on valid but empty chunks.
  • Cleaner streaming behavior.

3. Chat integration

Chat.Complete now propagates ctx into stream.Next(ctx), allowing external cancellation to stop streaming immediately.


Tests

connect/openai

Removed

  • TestHandleRawChunkDecorator_EmptyEvents_ReturnsError
  • TestNext_EmptyChunk_StopsWithEmptyEventsError

These tests validated behavior that is no longer correct (empty chunks are not errors).

Added

  • TestNext_ContextCancellation_StopsStream
Test Expected Behavior
ContextCancellation When ctx is canceled, Next(ctx) stops and returns context.Canceled

All related mocks were updated to support the new Next(ctx) signature.

x2d7 added 3 commits February 19, 2026 10:53
Updates Next() to accept context.Context for improved streaming control.
- Update test calls with context.Background()
- Remove deprecated empty event error handling tests
@x2d7 x2d7 linked an issue Feb 19, 2026 that may be closed by this pull request
@x2d7 x2d7 changed the base branch from main to dev February 19, 2026 07:16
@x2d7 x2d7 merged commit 874e171 into dev Feb 19, 2026
1 check passed
@x2d7 x2d7 deleted the 17-error-empty-events branch February 19, 2026 07:18
@x2d7 x2d7 mentioned this pull request Feb 19, 2026
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.

error: empty events

1 participant