Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ class ResponseAccumulator private constructor() {
* accumulated. A [ResponseAccumulator] can only be used to accumulate a single [Response].
*/
fun accumulate(event: ResponseStreamEvent): ResponseStreamEvent {
check(response == null) { "Response has already been completed." }
// The API may send non-terminal events (e.g. `response.rate_limits.updated`) after a
// terminal event (`response.completed`, `response.failed`, `response.incomplete`).
// These events do not carry response data and are safe to ignore. Throwing here would
// break callers that pass every streamed event to the accumulator.
if (response != null) {
return event
}

event.accept(
object : ResponseStreamEvent.Visitor<Unit> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ internal class ResponseAccumulatorTest {
}

@Test
fun accumulateAfterCompleted() {
fun accumulateAfterCompletedIgnoresPostCompletionEvents() {
val accumulator = ResponseAccumulator.create()

accumulator.accumulate(ResponseStreamEvent.ofCompleted(responseCompletedEvent()))

assertThatThrownBy {
accumulator.accumulate(ResponseStreamEvent.ofCompleted(responseCompletedEvent()))
}
.isExactlyInstanceOf(IllegalStateException::class.java)
.hasMessage("Response has already been completed.")
// Post-completion events (e.g. `response.rate_limits.updated`) should be silently
// ignored rather than throwing. The original response should remain intact.
assertThatNoException().isThrownBy {
accumulator.accumulate(ResponseStreamEvent.ofCreated(responseCreatedEvent()))
}
assertThat(accumulator.response().id()).isEqualTo("response-id")
}

@Test
Expand Down