Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The connection is not returned to the httpx pool when using a stream #763

Closed
1 task done
sometastycake opened this issue Nov 9, 2023 · 4 comments
Closed
1 task done
Assignees
Labels
bug Something isn't working

Comments

@sometastycake
Copy link

sometastycake commented Nov 9, 2023

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

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

  1. Initialize an AsyncOpenAI object with a maximum number of connections equal to 2
  2. Send 2 requests (chat.completions) with stream=True and read the entire stream
  3. Send a third request
  4. 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

@sometastycake sometastycake added the bug Something isn't working label Nov 9, 2023
@RobertCraigie
Copy link
Collaborator

RobertCraigie commented Nov 9, 2023

Thanks for the bug report, I can reproduce the issue. Thanks for the minimal example! Looking into a fix.

@RobertCraigie
Copy link
Collaborator

While we're investigating this, you can workaround the issue by manually closing the response, e.g.

    stream = await client.chat.completions.create(
        messages=[{"role": "system", "content": "You are helpful assistant"}, {"role": "user", "content": "Hello"}],
        model="gpt-4",
        stream=True,
        timeout=10,
    )
    result = ""
    async for message in stream:
        if message.choices:
            result += message.choices[0].delta.content or ""
    await stream.response.aclose()

@RobertCraigie
Copy link
Collaborator

This will be fixed in the next release! https://github.com/openai/openai-python/pull/771/files

@RobertCraigie
Copy link
Collaborator

This has been released in v1.2.3!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants