You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
There might be a memory leak when using the method .parse() on AsyncCompletions with Pydantic models created with create_model. When submitting several calls, the memory usage keeps on rising. I haven't found any plateau yet, which could mean the parsers built upon these models might not be garbage collected.
To Reproduce
Have a function that creates a Pydantic model with create_model
Have several calls where the response_format param always gets a new model from the function above
Monitor the memory
We do have a work-around though. The leaking scenario will be called leaking and the safe one non_leaking in the snippets.
Please let me know if you need more info. Thanks a lot.
Code snippets
importasyncioimportgcimportosfromtypingimportListfrommemory_profilerimportprofilefromopenaiimportAsyncOpenAIfromopenai.lib._parsingimporttype_to_response_format_paramfrompydanticimportField, create_modelStepModel=create_model(
"Step",
explanation=(str, Field()),
output=(str, Field()),
)
defcreate_new_model():
"""This sounds useless as it is. In our business case, I'm generating a model that slightly different at each call, hence the use of create_model. This illustrates of a model that seems to always be the same keeps on adding up in the memory."""returncreate_model(
"MathResponse",
steps=(List[StepModel], Field()),
final_answer=(str, Field()),
)
@profile()asyncdefleaking_call(client, new_model):
awaitclient.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "solve 8x + 31 = 2"},
],
response_format=new_model,
)
asyncdefnon_leaking_call(client, new_model):
awaitclient.chat.completions.create(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "solve 8x + 31 = 2"},
],
response_format=type_to_response_format_param(new_model),
)
asyncdefmain():
client=AsyncOpenAI()
for_inrange(200):
# You can switch to `non_leaking_call` and see that the memory is correctly emptiedawaitleaking_call(client, create_new_model())
# We wanted to thoroughly check the memory usage, hence memory profiler + gcgc.collect()
print(len(gc.get_objects()))
if__name__=="__main__":
asyncio.run(main())
OS
macOS
Python version
Python 3.11.9
Library version
openai v1.64.0
The text was updated successfully, but these errors were encountered:
Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
There might be a memory leak when using the method
.parse()
onAsyncCompletions
with Pydantic models created withcreate_model
. When submitting several calls, the memory usage keeps on rising. I haven't found any plateau yet, which could mean the parsers built upon these models might not be garbage collected.To Reproduce
create_model
We do have a work-around though. The leaking scenario will be called
leaking
and the safe onenon_leaking
in the snippets.Please let me know if you need more info. Thanks a lot.
Code snippets
OS
macOS
Python version
Python 3.11.9
Library version
openai v1.64.0
The text was updated successfully, but these errors were encountered: