Skip to content

Unclear how to use the new AsyncClient at scale - memory leaks or timeouts #874

Closed

Description

@rohan-mehta

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

I have a containerized FastAPI server.

As a result, its not clear how to use the new library at scale. It seems like the optimal solution would be to manage a pool of AsyncClients and scale them up/down as QPS goes up/down. Looking for suggestions/advice on how to handle this. Thanks!

To Reproduce

Use this as the client:

def get_openai_client() -> AsyncOpenAI:
    if not hasattr(get_openai_client, "client"):
        get_openai_client.client = AsyncOpenAI(
            api_key="...",
        )
    return get_openai_client.client

And then send a bunch of requests/see timeouts.

Code snippets

No response

OS

Linux

Python version

3.11

Library version

1.3.4

Activity

  1. added
    bug
    on Nov 23, 2023
  2. rattrayalex commented on Nov 23, 2023

    @rattrayalex
    Contributor

    I recommend instantiating the client once, using expanded connection pool limits, like those suggested here: #821 (comment)

    For example:

    from openai import AsyncOpenAI
    import httpx
    
    client = AsyncOpenAI(
      http_client=httpx.AsyncClient(
        limits=httpx.Limits(
          max_connections=1000,
          max_keepalive_connections=100
        )
      )
    )

    Details: https://www.python-httpx.org/advanced/#pool-limit-configuration

  3. richardknudsen commented on Feb 7, 2024

    @richardknudsen

    @rattrayalex for any other future readers that may stumble over the following as well:
    Shouldn't it be httpx.AsyncClient instead of httpx.Client?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions