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

Memory leak when using with_options() to configure options per request #865

Closed
1 task done
jose-speak opened this issue Nov 22, 2023 · 1 comment
Closed
1 task done
Assignees
Labels
bug Something isn't working

Comments

@jose-speak
Copy link

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

Using with_options() to configure options per request introduces a memory leak.

How to determine it's a memory leak?

The following code calls the completion API 10 times. On each iteration, it takes a snapshot of traces of memory blocks using tracemalloc, compares it to the snapshot of the previous iteration, and prints the top 2 differences. The memory required by the openai library increases on every iteration. In particular, the following lines have the most increases (using sync client):

.../env/lib/python3.11/site-packages/openai/_response.py:227: size=321 KiB (+26.0 KiB), count=2681 (+175), average=123 B
.../env/lib/python3.11/site-packages/openai/_response.py:226: size=214 KiB (+19.4 KiB), count=1830 (+165), average=120 B
import os
import tracemalloc
from openai import OpenAI

tracemalloc.start()

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

snapshot1 = tracemalloc.take_snapshot()
for _ in range(10):
    client.with_options(max_retries=5).chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": "How can I get the name of the current day in Node.js?",
            }
        ],
        model="gpt-3.5-turbo",
    )

    snapshot2 = tracemalloc.take_snapshot()
    top_stats = snapshot2.compare_to(snapshot1, 'lineno')
    print("[ Top 2 differences ]")
    for stat in top_stats[:2]:
        print(stat)
    
    snapshot1 = snapshot2

Potential cause

Looking at the library code, when using with_options(), a new client is created on every request (sync client and async client).

To Reproduce

Use with_options() and make multiple API calls.

Code snippets

No response

OS

macOS

Python version

Python v3.11.5

Library version

openai v1.3.3

@jose-speak jose-speak added the bug Something isn't working label Nov 22, 2023
@rattrayalex
Copy link
Collaborator

Thanks for reporting and sharing your debugging steps, we'll look into this soon!

stainless-bot added a commit to lithic-com/lithic-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to lithic-com/lithic-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to anthropics/anthropic-sdk-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to Finch-API/finch-api-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to anthropics/anthropic-sdk-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to Finch-API/finch-api-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to anthropics/anthropic-bedrock-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to orbcorp/orb-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to lithic-com/lithic-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to anthropics/anthropic-bedrock-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to Modern-Treasury/modern-treasury-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to Modern-Treasury/modern-treasury-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to Increase/increase-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to Increase/increase-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to Increase/increase-python that referenced this issue Dec 8, 2023
stainless-bot added a commit to anthropics/anthropic-bedrock-python that referenced this issue Dec 13, 2023
stainless-bot added a commit to anthropics/anthropic-sdk-python that referenced this issue Dec 13, 2023
stainless-bot added a commit to Finch-API/finch-api-python that referenced this issue Dec 18, 2023
stainless-bot added a commit to Modern-Treasury/modern-treasury-python that referenced this issue Jan 2, 2024
luckdev2350 added a commit to luckdev2350/openai_python that referenced this issue May 9, 2024
risingstar0225 added a commit to risingstar0225/openai_py that referenced this issue Jul 26, 2024
megamanics pushed a commit to devops-testbed/openai-python that referenced this issue Aug 14, 2024
Watcher919 added a commit to Watcher919/Python-openAI that referenced this issue Sep 4, 2024
cgayapr pushed a commit to cgayapr/openai-python that referenced this issue Dec 14, 2024
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

3 participants