Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
The connections is not returned to the pool when using a stream
In this case (see "To reproduce"), the error occurs in venv/lib/python3.11/site-packages/httpcore/_async/connection_pool.py in the wait_for_connection() method of the RequestStatus() class
And if after each end of reading the stream you call AsyncStream.response.aclose(), then everything will work correctly
To Reproduce
- Initialize an AsyncOpenAI object with a maximum number of connections equal to 2
- Send 2 requests (chat.completions) with stream=True and read the entire stream
- Send a third request
- We will receive an APITimeoutError error
Code snippets
import asyncio
import httpx
from openai import AsyncOpenAI
async def stream(client: AsyncOpenAI):
response = await client.chat.completions.create(
messages=[
{'role': 'system', 'content': 'You are helpful assistant'},
{'role': 'user', 'content': 'Hello'}
],
model='gpt-4',
stream=True,
timeout=30,
)
result = ''
async for message in response:
result += message.choices[0].delta.content or ""
async def main():
client = AsyncOpenAI(
api_key='',
http_client=httpx.AsyncClient(
limits=httpx.Limits(
max_connections=2,
)
)
)
for _ in range(3):
await stream(client)
if __name__ == '__main__':
asyncio.run(main())
OS
macOS, Ubuntu 22.04
Python version
Python v3.11.6
Library version
openai 1.2.0
Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
The connections is not returned to the pool when using a stream
In this case (see "To reproduce"), the error occurs in venv/lib/python3.11/site-packages/httpcore/_async/connection_pool.py in the wait_for_connection() method of the RequestStatus() class
And if after each end of reading the stream you call AsyncStream.response.aclose(), then everything will work correctly
To Reproduce
Code snippets
OS
macOS, Ubuntu 22.04
Python version
Python v3.11.6
Library version
openai 1.2.0