Skip to content

fix: async client timeout in streaming requests#2986

Open
BillionClaw wants to merge 1 commit intoopenai:mainfrom
BillionClaw:clawoss/fix/async-timeout-1773835799
Open

fix: async client timeout in streaming requests#2986
BillionClaw wants to merge 1 commit intoopenai:mainfrom
BillionClaw:clawoss/fix/async-timeout-1773835799

Conversation

@BillionClaw
Copy link

Fixes #2373

Problem

When a timeout occurs during streaming iteration (not just during the initial request), the underlying httpx.TimeoutException or asyncio.TimeoutError was not being properly converted to APITimeoutError. This meant that users catching APITimeoutError would not catch timeouts that occur while iterating over a stream.

Solution

Add try-except blocks in both Stream.__stream__() and AsyncStream.__stream__() to catch timeout exceptions and convert them to APITimeoutError, consistent with how timeouts are handled in the base client.

Changes

  • Import asyncio and APITimeoutError in _streaming.py
  • Add httpx.TimeoutException handling in Stream.__stream__()
  • Add httpx.TimeoutException and asyncio.TimeoutError handling in AsyncStream.__stream__()

Testing

All existing tests pass, including the timeout-related tests in test_client.py.

Add proper timeout exception handling to Stream and AsyncStream classes.
When a timeout occurs during streaming iteration, it is now properly
converted to APITimeoutError instead of leaking the underlying
httpx.TimeoutException or asyncio.TimeoutError.

Fixes openai#2373
@BillionClaw BillionClaw requested a review from a team as a code owner March 18, 2026 12:10
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