Skip to content

fix: tolerate post-completion stream events in ResponseAccumulator#719

Open
zklapow wants to merge 1 commit intoopenai:mainfrom
zklapow:fix-accumulator-post-completion-events
Open

fix: tolerate post-completion stream events in ResponseAccumulator#719
zklapow wants to merge 1 commit intoopenai:mainfrom
zklapow:fix-accumulator-post-completion-events

Conversation

@zklapow
Copy link
Copy Markdown

@zklapow zklapow commented Apr 16, 2026

Summary

The Responses API sends response.rate_limits.updated events after the terminal response.completed event. ResponseAccumulator.accumulate() throws IllegalStateException("Response has already been completed.") on any event received after a terminal event, which crashes callers that pass every streamed event through it.

This replaces the hard check with an early return so post-completion events are silently ignored and the accumulated response remains intact.

Reproduction

curl https://api.openai.com/v1/responses \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "What is the weather in San Francisco?",
    "model": "gpt-4.1",
    "tools": [{
      "name": "get_weather",
      "type": "function",
      "description": "Get weather",
      "parameters": {"type":"object","properties":{"location":{"type":"string"}},"required":["location"]}
    }],
    "stream": true
  }'

The stream ends with:

event: response.completed
data: {"type":"response.completed", ...}

event: response.rate_limits.updated
data: {"type":"response.rate_limits.updated", ...}

Passing these events to ResponseAccumulator throws:

java.lang.IllegalStateException: Response has already been completed.
    at com.openai.helpers.ResponseAccumulator.accumulate(ResponseAccumulator.kt:119)

Test plan

  • Updated accumulateAfterCompleted test to assert post-completion events are ignored rather than throwing
  • Verified accumulated response remains intact after receiving post-completion events
  • All existing ResponseAccumulatorTest tests pass

@zklapow zklapow requested a review from a team as a code owner April 16, 2026 20:11
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.

1 participant