Streaming with OpenAI Responses API breaks on unexpected post-completion events
## Problem
OpenAI's Responses API has started emitting a `response.rate_limits.updated` event **after** the `response.completed` event during streaming. This causes `ChatOpenAI()` to crash with:
```
AttributeError: 'NoneType' object has no attribute 'output'
```
## Minimal reproducible example
```python
from chatlas import ChatOpenAI
chat = ChatOpenAI()
chat.chat("What is 1 + 1?")
# AttributeError: 'NoneType' object has no attribute 'output'
```
## Root cause
In `OpenAIProvider.stream_merge_chunks()`, the method correctly captures the `Response` object when it sees `response.completed`, but its fallthrough path for unhandled event types returns `cast(Response, None)`:
```python
def stream_merge_chunks(self, completion, chunk):
if chunk.type == "response.completed":
return chunk.response
elif chunk.type == "response.failed":
...
elif chunk.type == "error":
...
# This overwrites a valid Response with None!
return cast(Response, None)
```
When `response.rate_limits.updated` arrives after `response.completed`, the valid `Response` is overwritten with `None`, which then gets passed to `stream_turn()` → `_response_as_turn()`, causing the crash.
## Context
This appears to be a server-side change from OpenAI (the new event is emitted regardless of SDK version). The event appeared during testing on `openai>=2.30.0` but may roll out intermittently.
## Fix
Change the fallthrough to `return completion`, preserving whatever value has already been accumulated. This also makes the code resilient to any future unknown event types.
关闭于 2026-04-16 0 条评论