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
{{ message }}
Repository navigation
Memory leak in the chat completion create method #820
You shouldn't be instantiating a new client for every request, if you move the client outside of the request handler you shouldn't see any memory leaks.
I did manage to replicate the memory leak you're seeing with your example and moving the client instantiation outside of the function resulted in stable memory usage.
Reacted by Rafael Pierre and Toni Alatalo
Reacted by AmeroHan, Sergey Lyapustin, snickk and Xiangyu Han
For what it's worth, I still see this issue when wrapping @CodePalAI's OpenAI client in a with block, which suggests there is some deeper problem here. I've been trying to track down a leak in my app (which existed with the pre-1.0 API, and has gotten much more aggressive after upgrading to 1.3) and I also ran into this issue.
Wouldn't "moving the client outside of the request handler" be the same thing as a module level client?
From the README, I figured the network resources are getting cleaned up when the OpenAI client gets garbage collected. Sounds like this isn't necessarily true?
This might be outside of the scope of this library but I'm also trying to understand how this fits in your typical backend app.
It seems like using a module scoped client is heavily discouraged but so is instantiating a new client with every request (ie a FastAPI dependency). What is the other option here?
Reacted by Liam van der Viven, AmeroHan, Ben Zhu, Brian Goo, Sergey Filimonov, Hasmasan Dragos, snickk, Tim Reilly , TasseDeCafe, Anthony Ho and 1 more
Another problem is that we have multiple api keys like below.
How can we handle this nicely if we have to instantiate a client once as mentioned in this issue #874?
@brian-goo you can create a single client and use FastAPI app events to close it when the server is stopped and then use client.with_options(api_key='foo') to use different API keys for different requests.
Reacted by Toni Alatalo, Tofu Lim and Miroslav Štěpánek
@Filimoa what are you trying to achieve that requires you to instantiate a new client for every request?
If you really want to do so you currently need to use client.with_options() or create a custom http client and pass that to the constructor every time to avoid resource leaks.
Instantiating a new http client for each request is very bad for performance regardless of whether or not it has any resource leaks as the client can not reuse connections.
@abi are you explicitly closing the client using client.close()? If you are then I would consider that a bug.
Additionally, is there a reason that client.with_options(api_key=user_api_key) doesn't work for you? That should give you all of the benefit of allowing users to specify their own API keys without creating a ton of unnecessary connections.
The main reason client.with_options(api_key=user_api_key) is non-ideal even though it would work is that it's a bit harder to debug and error-prone when you're not starting from a clean, empty state. Just now, I even tried doing an empty AsyncOpenAI() on the module level, and setting api_key using client.with_options(api_key=user_api_key). For some reason, even before client.with_options(api_key=user_api_key), client.api_key had a value and then, I realized it was pulling an initial value from my .env. Much more straightforward to do AsyncOpenAI(api_key) and know for sure that which api_key the client is associated with. It's a bit scary to potentially serve 1 user's request with another user's api key, even if that can only happen due to a bug in my code. The ergonomics of AsyncOpenAI(api_key=) are just nicer but not a big deal if .close() fixes my memory leak.
@abiAsyncOpenAI() on the module level, and setting api_key using client.with_options(api_key=user_api_key). For some reason, even before client.with_options(api_key=user_api_key), client.api_key had a value and then, I realized it was pulling an initial value from my .env. Much more straightforward to do AsyncOpenAI(api_key) and know for sure that which api_key the client is associated with. It's a bit scary to potentially serve 1 user's request with another user's api key, even if that can only happen due to a bug in my code.
Interesting point. Maybe there could be an option to disable the use of of that default option (what about others?), so that the client would err if you forget to provide the key using with_options in some place. Perhaps a bit awkward but would work to safeguard against such mistakes quite ok, no?
It's a bit scary to potentially serve 1 user's request with another user's api key, even if that can only happen due to a bug in my code
This is a fair point, I do think there are ways you could structure your code to make this practically impossible to happen though. Even for anyone making changes to your codebase for the first time.
For example, instead of using .with_options() yourself, you could define a helper function that you use whenever you need to make an OpenAI request, e.g.
# utils/openai_client.pyfromopenaiimportAsyncOpenAI_client=AsyncOpenAI(
# provide a dummy API key so that requests made directly will always failapi_key='<this client should never be used directly!>',
)
defget_openai(user: User) ->AsyncOpenAI:
return_client.with_options(api_key=user.openai_api_key)
# backend.pyasyncdefhandler():
user=get_user()
completion=awaitget_openai(user).chat.completions.create(...)
# ...
It's also worth noting that you could use your existing pattern and avoid memory leaks entirely by sharing the http client instance if you really don't want to use with_options, the only risk here is that you could instantiate a client and forget to pass in your http client which then gives you memory leaks again.
I would argue though that the first snippet in this comment is less likely to result in accidentally using the incorrect API key because you could accidentally just write AsyncOpenAI() and then it will use your API key from your environment.
However with a helper function similar to get_openai() you're encoding the setup of your system directly into it so instead of having to know to pass api_key=user_api_key you're forced to by your own function definition.
The only way you'll accidentally use the wrong API key is by forgetting to use this helper function, which it may be possible to define lint rules for (I don't know of an easy way to do this off the top of my head).
Started noticing a weird behaviour in our api server and after digging a bit deeper I think I found the underlaying issue with memory leaks. The way classes uses references to self won't allow for resources to be freed as classes have objects that in turn have references to the class it self.
After testing a bit with forced close functions that frees resources, it runs deinit and frees memory as it should.
Thanks for the investigation there, @gintsmurans ! That makes sense to me. We'll take a look soon and see if we can demonstrate that this prevents the issue.
In our case its an api backend server where openai is just a small part of it, so we don't keep one global openai instance. But as requests comes in, in a week or so, api server's memory is all used up.
In the example I am just trying to show how the leak is happening.
In our case its an api backend server where openai is just a small part of it, so we don't keep one global openai instance.
Is there a technical reason for this? Our backend also uses many APIs and OpenAI is just a small part of it, but still we use and keep a client per process.
There wasn't a definite reason, just that we made it that way, but now when I think about it - we are using async request handlers. If we would be using the same instance for all openai requests, that could lead to conflicts and even deadlocks. Learned it the hard way. :)
Anyhow, any library should cleanup after it self, or give option to do it - you never know how people are gonna use it.
There wasn't a definite reason, just that we made it that way, but now when I think about it - we are using async request handlers. If we would be using the same instance for all openai requests, that could lead to conflicts and even deadlocks. Learned it the hard way. :)
What do you mean?
We also use async request handlers, and the async support in the OpenAI client, to scale. I'm not aware of any problems it would give, conflicts or deadlocks or anything. I think those would be a serious bug in the library. I guess you encountered something, as you learned it the hard way?
Anyhow, any library should cleanup after it self, or give option to do it - you never know how people are gonna use it.
Sure, but I think it's also good to know the good patterns for usage, and possible problems.
As far as my understanding goes .. lets say one api request starts async task, but as it waits for results, another request can be handled. At the same time the class instance that handles requests is the same one for every request.
We had serveral issues with this:
We had some properties we set on the handler class for each request, like auth token, gathered from request headers. Now first request sets this property and calls async method that takes some time to answer - like openai api request. While that is executing, another request comes in and sets class property to a new auth token. Now if first requests, after the prolonged task is finished, would use that token in any way, it will have second request's token, which is not perfect. :)
On each request we were taking a postgres and redis connection from a pool and were using those in subsequent method calls via class properties. At some point first request starts a database transaction, but another request changes connection instance to a new connection from a pool. Now first request handler will continue with this new instance, but old instance just hangs somewhere in memory with a started transaction, maybe it is cleaned up with python's gc, maybe its not, but transaction stays open on the database server. This one lead to deadlocks as two async tasks whats to use same instance, postgres transaction issues, etc.
Global instances would not solve this, but would make it even worse. For example, one request starts a transaction, then does something prolonged, while another request comes in and tries to start a new transaction on the same connection object.
We solved these by using ContextVar class to make sure, only one context - request uses same object instance.
In case of openai - not sure how it would react to multiple simultaneous requests. I would not use global instances for anything that I can be sure its thread / context safe. Which leads to openai not releasing memory correctly. :)
@gintsmurans Yes, the idea of async request handlers is that you can have many running concurrently, within a single process and thread. And indeed, you should not use class attributes or other mechanisms to mix up request specific data then. I've usually just passed the request context as parameters to the business logic functions, like the session object when using postgres via sqlalchemy.
Anyway, that's not related to this OpenAI client lib, which AFAIK does no such mixups, and where reusing a single client is the recommended way for multiple concurrent async request handlers.
Again, sure, it's good to fix the cleanup, but it's also good to be clear about what is good practice and why. I've understood that sharing the client cross multiple handlers is good so that you are not opening and closing connections to OpenAI in vain all the time.
Haven't looked, but I would guess that open ai does not create a "new connection" with each instance, but internally does basic http requests when asked to - for example on completions.create.
In my subjective opinion, using same instances between coroutines / threads is just a bad practice, will lead to weird behavior sooner or later, thus should be avoided - you never know what a third party library does internally, nor you can check each one of them.
Anyway, it really does not matter as its not in the scope of this issue. Focus is that there seems to be memory leaks, regardless of how the library is used.
Haven't looked, but I would guess that open ai does not create a "new connection" with each instance, but internally does basic http requests when asked to - for example on completions.create.
On the other hand, a Client instance uses HTTP connection pooling. This means that when you make several requests to the same host, the Client will reuse the underlying TCP connection, instead of recreating one for every single request.
In my subjective opinion, using same instances between coroutines / threads is just a bad practice, will lead to weird behavior sooner or later, thus should be avoided - you never know what a third party library does internally, nor you can check each one of them.
I think you are wrong here, and both the OpenAI and HTTPX libraries are made so that it can be a good idea to reuse connections across async tasks. With threads it would indeed lead to problems I think, but the point of async is to not need threads for this kind of things.
These are all nice things, but we've had issues with the connection becoming corrupt after some kinda of failures. This is something a fresh connection never does, so we'd prefer if a fresh connection did not have memory leaks. It seems risky to rely upon the same http client to be perfect forever globally.
@pseudotensor as I said just above, please open a new issue if you see further problems instead of commenting here. It's not clear to me whether #1181 describes the entirety of the issues you're seeing.
Activity
RobertCraigie commentedon Nov 15, 2023
You shouldn't be instantiating a new client for every request, if you move the client outside of the request handler you shouldn't see any memory leaks.
I did manage to replicate the memory leak you're seeing with your example and moving the client instantiation outside of the function resulted in stable memory usage.
qxcv commentedon Nov 19, 2023
For what it's worth, I still see this issue when wrapping @CodePalAI's
OpenAIclient in awithblock, which suggests there is some deeper problem here. I've been trying to track down a leak in my app (which existed with the pre-1.0 API, and has gotten much more aggressive after upgrading to 1.3) and I also ran into this issue.Filimoa commentedon Nov 23, 2023
Wouldn't "moving the client outside of the request handler" be the same thing as a module level client?
From the README, I figured the network resources are getting cleaned up when the OpenAI client gets garbage collected. Sounds like this isn't necessarily true?
This might be outside of the scope of this library but I'm also trying to understand how this fits in your typical backend app.
It seems like using a module scoped client is heavily discouraged but so is instantiating a new client with every request (ie a FastAPI dependency). What is the other option here?
brian-goo commentedon Nov 30, 2023
@RobertCraigie
How can we implement an async client with fastapi correctly?
Is it safe to implement like this (singleton approach) ?
fastapi/fastapi#8301 (comment)
Another problem is that we have multiple api keys like below.
How can we handle this nicely if we have to instantiate a client once as mentioned in this issue #874?
RobertCraigie commentedon Nov 30, 2023
@brian-goo you can create a single client and use FastAPI app events to close it when the server is stopped and then use
client.with_options(api_key='foo')to use different API keys for different requests.RobertCraigie commentedon Nov 30, 2023
@Filimoa what are you trying to achieve that requires you to instantiate a new client for every request?
If you really want to do so you currently need to use
client.with_options()or create a custom http client and pass that to the constructor every time to avoid resource leaks.Instantiating a new http client for each request is very bad for performance regardless of whether or not it has any resource leaks as the client can not reuse connections.
abi commentedon Dec 3, 2023
@RobertCraigie one use case I have is with https://github.com/abi/screenshot-to-code where users put in their Open AI key on the hosted version (that's how the app is free to use). I understand that performance will be worse with instantiating a new client on each request but a memory leak is even worse.
Here's a fun memory leak graph from my server :) Not 100% sure it's because of this issue but very likely.
RobertCraigie commentedon Dec 3, 2023
@abi are you explicitly closing the client using
client.close()? If you are then I would consider that a bug.Additionally, is there a reason that
client.with_options(api_key=user_api_key)doesn't work for you? That should give you all of the benefit of allowing users to specify their own API keys without creating a ton of unnecessary connections.abi commentedon Dec 3, 2023
@RobertCraigie I'm not closing with
client.close(): https://github.com/abi/screenshot-to-code/blob/main/backend/llm.py#L26 Will try that now and report back. Thanks for the help.The main reason
client.with_options(api_key=user_api_key)is non-ideal even though it would work is that it's a bit harder to debug and error-prone when you're not starting from a clean, empty state. Just now, I even tried doing an emptyAsyncOpenAI()on the module level, and setting api_key usingclient.with_options(api_key=user_api_key). For some reason, even beforeclient.with_options(api_key=user_api_key), client.api_key had a value and then, I realized it was pulling an initial value from my .env. Much more straightforward to do AsyncOpenAI(api_key) and know for sure that which api_key the client is associated with. It's a bit scary to potentially serve 1 user's request with another user's api key, even if that can only happen due to a bug in my code. The ergonomics of AsyncOpenAI(api_key=) are just nicer but not a big deal if .close() fixes my memory leak.antont commentedon Dec 3, 2023
Interesting point. Maybe there could be an option to disable the use of of that default option (what about others?), so that the client would err if you forget to provide the key using
with_optionsin some place. Perhaps a bit awkward but would work to safeguard against such mistakes quite ok, no?RobertCraigie commentedon Dec 3, 2023
This is a fair point, I do think there are ways you could structure your code to make this practically impossible to happen though. Even for anyone making changes to your codebase for the first time.
For example, instead of using
.with_options()yourself, you could define a helper function that you use whenever you need to make an OpenAI request, e.g.It's also worth noting that you could use your existing pattern and avoid memory leaks entirely by sharing the http client instance if you really don't want to use
with_options, the only risk here is that you could instantiate a client and forget to pass in your http client which then gives you memory leaks again.I would argue though that the first snippet in this comment is less likely to result in accidentally using the incorrect API key because you could accidentally just write
AsyncOpenAI()and then it will use your API key from your environment.However with a helper function similar to
get_openai()you're encoding the setup of your system directly into it so instead of having to know to passapi_key=user_api_keyyou're forced to by your own function definition.The only way you'll accidentally use the wrong API key is by forgetting to use this helper function, which it may be possible to define lint rules for (I don't know of an easy way to do this off the top of my head).
abi commentedon Dec 3, 2023
@RobertCraigie that's super helpful, both of those patterns. Didn't realize you could pass in the http_client for the client.
I pushed
client.close()a couple of hours ago BTW, and that solved my memory leak issues. Thanks for your help on this!gintsmurans commentedon Dec 5, 2023
Hi!
Started noticing a weird behaviour in our api server and after digging a bit deeper I think I found the underlaying issue with memory leaks. The way classes uses references to self won't allow for resources to be freed as classes have objects that in turn have references to the class it self.
After testing a bit with forced close functions that frees resources, it runs deinit and frees memory as it should.
rattrayalex commentedon Dec 5, 2023
Thanks for the investigation there, @gintsmurans ! That makes sense to me. We'll take a look soon and see if we can demonstrate that this prevents the issue.
2 remaining items
antont commentedon Dec 10, 2023
@gintsmurans Why do you call the init many times? Maybe it's good to improve your case, but anyhow, am curious as usually AFAIK there is no reason to.
gintsmurans commentedon Dec 10, 2023
In our case its an api backend server where openai is just a small part of it, so we don't keep one global openai instance. But as requests comes in, in a week or so, api server's memory is all used up.
In the example I am just trying to show how the leak is happening.
antont commentedon Dec 11, 2023
Is there a technical reason for this? Our backend also uses many APIs and OpenAI is just a small part of it, but still we use and keep a client per process.
gintsmurans commentedon Dec 11, 2023
There wasn't a definite reason, just that we made it that way, but now when I think about it - we are using async request handlers. If we would be using the same instance for all openai requests, that could lead to conflicts and even deadlocks. Learned it the hard way. :)
Anyhow, any library should cleanup after it self, or give option to do it - you never know how people are gonna use it.
antont commentedon Dec 11, 2023
What do you mean?
We also use async request handlers, and the async support in the OpenAI client, to scale. I'm not aware of any problems it would give, conflicts or deadlocks or anything. I think those would be a serious bug in the library. I guess you encountered something, as you learned it the hard way?
Sure, but I think it's also good to know the good patterns for usage, and possible problems.
gintsmurans commentedon Dec 11, 2023
As far as my understanding goes .. lets say one api request starts async task, but as it waits for results, another request can be handled. At the same time the class instance that handles requests is the same one for every request.
We had serveral issues with this:
We had some properties we set on the handler class for each request, like auth token, gathered from request headers. Now first request sets this property and calls async method that takes some time to answer - like openai api request. While that is executing, another request comes in and sets class property to a new auth token. Now if first requests, after the prolonged task is finished, would use that token in any way, it will have second request's token, which is not perfect. :)
On each request we were taking a postgres and redis connection from a pool and were using those in subsequent method calls via class properties. At some point first request starts a database transaction, but another request changes connection instance to a new connection from a pool. Now first request handler will continue with this new instance, but old instance just hangs somewhere in memory with a started transaction, maybe it is cleaned up with python's gc, maybe its not, but transaction stays open on the database server. This one lead to deadlocks as two async tasks whats to use same instance, postgres transaction issues, etc.
Global instances would not solve this, but would make it even worse. For example, one request starts a transaction, then does something prolonged, while another request comes in and tries to start a new transaction on the same connection object.
We solved these by using ContextVar class to make sure, only one context - request uses same object instance.
In case of openai - not sure how it would react to multiple simultaneous requests. I would not use global instances for anything that I can be sure its thread / context safe. Which leads to openai not releasing memory correctly. :)
antont commentedon Dec 11, 2023
@gintsmurans Yes, the idea of async request handlers is that you can have many running concurrently, within a single process and thread. And indeed, you should not use class attributes or other mechanisms to mix up request specific data then. I've usually just passed the request context as parameters to the business logic functions, like the session object when using postgres via sqlalchemy.
Anyway, that's not related to this OpenAI client lib, which AFAIK does no such mixups, and where reusing a single client is the recommended way for multiple concurrent async request handlers.
Again, sure, it's good to fix the cleanup, but it's also good to be clear about what is good practice and why. I've understood that sharing the client cross multiple handlers is good so that you are not opening and closing connections to OpenAI in vain all the time.
gintsmurans commentedon Dec 11, 2023
Haven't looked, but I would guess that open ai does not create a "new connection" with each instance, but internally does basic http requests when asked to - for example on
completions.create.In my subjective opinion, using same instances between coroutines / threads is just a bad practice, will lead to weird behavior sooner or later, thus should be avoided - you never know what a third party library does internally, nor you can check each one of them.
gintsmurans commentedon Dec 11, 2023
Anyway, it really does not matter as its not in the scope of this issue. Focus is that there seems to be memory leaks, regardless of how the library is used.
antont commentedon Dec 12, 2023
I think the http connections are pooled, by the underlying httpx library. https://www.python-httpx.org/advanced/ says:
On the OpenAI side, the httpx client is kept and reused by requests, so I think it gets to use the pool, and does not create new connections for new requests. https://github.com/openai/openai-python/blob/main/src/openai/_base_client.py#L872
I think you are wrong here, and both the OpenAI and HTTPX libraries are made so that it can be a good idea to reuse connections across async tasks. With threads it would indeed lead to problems I think, but the point of async is to not need threads for this kind of things.
rattrayalex commentedon Dec 15, 2023
We believe this has been fixed in
1.3.9! Please open a new issue if you see further problems.pseudotensor commentedon Feb 23, 2024
@RobertCraigie
#820 (comment)
These are all nice things, but we've had issues with the connection becoming corrupt after some kinda of failures. This is something a fresh connection never does, so we'd prefer if a fresh connection did not have memory leaks. It seems risky to rely upon the same http client to be perfect forever globally.
rattrayalex commentedon Feb 23, 2024
@pseudotensor as I said just above, please open a new issue if you see further problems instead of commenting here. It's not clear to me whether #1181 describes the entirety of the issues you're seeing.
pseudotensor commentedon Feb 23, 2024
Yes, you can see I opened a new issue, what's your question here?
FYI I'm just responding to one particular comment here, so others know as well when they search for issues.