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
httpx client has very poor performance for concurrent requests compared to aiohttp #1596
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 API client uses httpx, which has very poor performance when making concurrent requests compared to aiohttp. Open issue for httpx here
This is forcing us to swap out the OpenAI SDK for our own implementation, which is a pain.
I suspect it is the root cause of the difference between node.js and Python demonstrated here
I'm not massively familiar with the development of this SDK, and whether there is a key reason for picking httpx over aiohttp. From my reading it was switched over for V1 in order to create consistency between sync and async clients, but I'm not sure how vital it is to achieve this. However for our high concurrency async use cases this renders the SDK useless.
To Reproduce
To reproduce, run chat completion requests in parallel with 20+ concurrent requests, benchmarking the openai API client against an implementation using aiohttp. Example code can be found in the linked issue in httpx.
Code snippets
No response
OS
Linux/MacOs
Python version
v3.12
Library version
1.12.0
Reacted by Shubham Maddhashiya, Ryan Marten, kamillle, Choe Hwal, Sumer Sao, 辐无真言, zq.deer, Oleg Lavrovsky, Grant, Vitor Savedra and 3 more
Interesting, I was not aware there was such a performance discrepancy between aiohttp and httpx.
From skimming the linked issue it thankfully seems like there's a lot of inflight work that would bring httpx up to par performance-wise.
I'm not massively familiar with the development of this SDK, and whether there is a key reason for picking httpx over aiohttp. From my reading it was switched over for V1 in order to create consistency between sync and async clients, but I'm not sure how vital it is to achieve this.
Yes, consistency here is very important, using different clients would make everything much more complicated/confusing for little gain, especially as this performance discrepancy can be fixed.
However for our high concurrency async use cases this renders the SDK useless.
Sorry about this, hopefully the httpx PRs can be merged soon.
In the meantime it might be less work for you to use a patched version of httpx with the performance fixes included in the linked issue.
I'm going to close this as we have no plans to move away from httpx. In the future we may offer a more extensible custom http client API which would allow you to use any http library as long as you implement the interface. However this isn't likely to happen anytime soon unfortunately.
I'll see if we can help land the httpx / httpcore PRs faster.
Reacted by Will Hayes, Kent Matsuura, Sergio Castillo, Tommy He, 叶子, alexi and Oleg Lavrovsky
I'm working on a chatbot and ran some load testing and profiling under high concurrent users. It appears that the performance of httpx in asynchronous mode, which is used by the OpenAI Python SDK, isn't meeting expectations.
Are there any updates or planned improvements on this? Or would it be worth benchmarking my application with aiohttp to see if it performs better under high concurrency?
^ Likewise, to +1 this, our aiohttp impl is substantially faster, sometimes even twice as much with aiohttp - I go from about 100 requests per second to 200+ on my local machine, so not sure what the discrepancy is caused by.
We've moved from using httpx to aiohttp and it solved a lot of our concurrency issues - it means we don't use the openai client which I was initially concerned about due to the theoretical connection sharing benefit of using the client but this pales in comparison. I wish i'd swapped sooner.
In my case, after profiling, I identified a performance overhead in the AsyncConnectionPool.handle_async_request method. It appears this is mainly due to inefficient handling of idle or expired connections, as revealed in the profiling. We could reduce time complexity by using a more optimized data structure, rather than repeatedly iterating over the connections.
I came across these pull requests addressing similar issues, but they haven't been merged yet: Issue #3215 comment.
I tested the optimizations by installing the patches from the all-optimizations branch by @MarkusSintonen:
If performance is still a concern here, please try aiohttp 3.11.x+ as we have smoothed out some more of the concurrency delays and cancellation races in this version.
@RobertCraigie : I see you have closed this marking it as not planned here.
Shouldn't this be a priority item for OpenAI SDK team to make this SDK usable for use cases to scale ? Doesn't this discourage users to use this SDK at scale ?
I am a developer of an AI character service that provides interactive conversations, and it uses large language models (LLMs) to generate the characters' responses. The service has used openai package v0.27.4 to request to LLMs.
After upgrading openai package to v1.59.4, I noticed a decrease in the server's throughput and an increase in the latency of LLM hosting servers. I suspect that httpx is the cause, as discussed in related conversations, and I would like to explore potential solutions.
Sorry about this – we're tracking primarily at encode/httpx#3215 but I agree it makes sense to leave this open, as one way or another I do agree this needs to get solved for users of this SDK.
Reacted by Rachit Chauhan, kamillle and Ryan Marten
We also encounter problems with the httpx client, implemented by v1, when we share the client across multiple threads (sync). The pod freezes because the connection is not released or something similar. We got this issue for months, so curious if anyone also have this issue with sync client.
Update: with many thanks for support from @rattrayalex and team, and apologies for diverting from the performance discussion, I've mostly ruled out that the issue is with this package or with httpx. In my case an upgrade to poetry 2.1.2 seems to have resolved a dependency conflict. Up and running with:
Activity
RobertCraigie commentedon Aug 5, 2024
Interesting, I was not aware there was such a performance discrepancy between
aiohttpandhttpx.From skimming the linked issue it thankfully seems like there's a lot of inflight work that would bring
httpxup to par performance-wise.Yes, consistency here is very important, using different clients would make everything much more complicated/confusing for little gain, especially as this performance discrepancy can be fixed.
Sorry about this, hopefully the httpx PRs can be merged soon.
In the meantime it might be less work for you to use a patched version of
httpxwith the performance fixes included in the linked issue.I'm going to close this as we have no plans to move away from httpx. In the future we may offer a more extensible custom http client API which would allow you to use any http library as long as you implement the interface. However this isn't likely to happen anytime soon unfortunately.
I'll see if we can help land the
httpx/httpcorePRs faster.willthayes commentedon Aug 5, 2024
Understood, thanks for the quick reply!
ShubhamMaddhashiya-bidgely commentedon Oct 29, 2024
Hi everyone,
I'm working on a chatbot and ran some load testing and profiling under high concurrent users. It appears that the performance of
httpxin asynchronous mode, which is used by the OpenAI Python SDK, isn't meeting expectations.Are there any updates or planned improvements on this? Or would it be worth benchmarking my application with
aiohttpto see if it performs better under high concurrency?Thanks in advance for your insights!
opalrose-510 commentedon Oct 31, 2024
^ Likewise, to +1 this, our aiohttp impl is substantially faster, sometimes even twice as much with aiohttp - I go from about 100 requests per second to 200+ on my local machine, so not sure what the discrepancy is caused by.
Tom-Standen commentedon Nov 2, 2024
We've moved from using httpx to aiohttp and it solved a lot of our concurrency issues - it means we don't use the openai client which I was initially concerned about due to the theoretical connection sharing benefit of using the client but this pales in comparison. I wish i'd swapped sooner.
iliazintchenko commentedon Nov 2, 2024
this is the aiohttp class we are using to maximize throughput while this issue is open:
ShubhamMaddhashiya-bidgely commentedon Nov 4, 2024
In my case, after profiling, I identified a performance overhead in the
AsyncConnectionPool.handle_async_requestmethod. It appears this is mainly due to inefficient handling of idle or expired connections, as revealed in the profiling. We could reduce time complexity by using a more optimized data structure, rather than repeatedly iterating over the connections.I came across these pull requests addressing similar issues, but they haven't been merged yet: Issue #3215 comment.
I tested the optimizations by installing the patches from the
all-optimizationsbranch by @MarkusSintonen:With these changes, the performance overhead in
AsyncConnectionPool.handle_async_requestwas significantly reduced.It would be very helpful if the OpenAI SDK supports a drop-in replacement for
http_client, allowing us to swaphttpxwithaiohttpeasily.bdraco commentedon Nov 14, 2024
If performance is still a concern here, please try
aiohttp3.11.x+ as we have smoothed out some more of the concurrency delays and cancellation races in this version.https://docs.aiohttp.org/en/stable/changes.html
rachitchauhan43 commentedon Jan 3, 2025
@RobertCraigie : I see you have closed this marking it as not planned here.
Shouldn't this be a priority item for OpenAI SDK team to make this SDK usable for use cases to scale ? Doesn't this discourage users to use this SDK at scale ?
kamillle commentedon Jan 8, 2025
Same here... :(
I am a developer of an AI character service that provides interactive conversations, and it uses large language models (LLMs) to generate the characters' responses. The service has used openai package v0.27.4 to request to LLMs.
After upgrading openai package to v1.59.4, I noticed a decrease in the server's throughput and an increase in the latency of LLM hosting servers. I suspect that httpx is the cause, as discussed in related conversations, and I would like to explore potential solutions.
I look forward to responses from contributors.
rattrayalex commentedon Jan 13, 2025
Sorry about this – we're tracking primarily at encode/httpx#3215 but I agree it makes sense to leave this open, as one way or another I do agree this needs to get solved for users of this SDK.
1 remaining item
rattrayalex commentedon Mar 7, 2025
rattrayalex commentedon Mar 9, 2025
Update: For anyone running into async performance issues with this library, I recommend using https://github.com/karpetrosyan/httpx-aiohttp, which is published by an
httpxmaintainer, like so:gfx commentedon Mar 25, 2025
Deployed a solution with
httpx_aiohttp, but a lot of warnings are raised such as:Not sure what exactly is happing, but probably because of issues in
httpx_aiohttp.(update)
Issued: karpetrosyan/httpx-aiohttp#4
thsunkid commentedon Mar 25, 2025
We also encounter problems with the httpx client, implemented by v1, when we share the client across multiple threads (sync). The pod freezes because the connection is not released or something similar. We got this issue for months, so curious if anyone also have this issue with sync client.
loleg commentedon Apr 29, 2025
On top of this, the relative sluggish releases of httpx are currently blocking a critical security issue with
h11viahttpcore.rattrayalex commentedon Apr 30, 2025
@loleg I don't want this thread to get off-topic; would you mind messaging me and cc'ing [email protected] with more details?
EDIT: it looks like you're talking about https://github.com/encode/httpcore/releases/tag/1.0.9 which was released a week ago and can be used with httpx already (httpx specifies
httpcore==0.*).Any further discussion of this should take place in a separate GitHub issue or over email.
loleg commentedon May 3, 2025
Update: with many thanks for support from @rattrayalex and team, and apologies for diverting from the performance discussion, I've mostly ruled out that the issue is with this package or with httpx. In my case an upgrade to
poetry 2.1.2seems to have resolved a dependency conflict. Up and running with:markwitt1 commentedon May 19, 2025
Is this issue solved now in the latest version?
ishaan-jaff commentedon May 23, 2025
This suggestion was great, we see a 97% lower median latency on load tests
RobertCraigie commentedon Jun 20, 2025
We now have builtin support for
aiohttpusage, please try it out and let us know if you run into any issues!https://github.com/openai/openai-python#with-aiohttp
(There are still some more things to come such as more detailed docs + making it easier to instantiate your own
ClientSession())MarkusSintonen commentedon Oct 12, 2025
I have just released a new high-perf http library which is fully Rust-reqwest based. Batteries are included, including unit testing support. Go check it out: https://github.com/MarkusSintonen/pyreqwest :)
fix: configure httpx connection limits to prevent pool exhaustion
fix: configure httpx connection limits to prevent pool exhaustion
fix: stop injecting shared HTTP/2 client into OpenAI-based models (#7328