make Twins.chat() async to prevent event loop blocking#268
Open
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
Open
make Twins.chat() async to prevent event loop blocking#268verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
Conversation
Signed-off-by: verseon0980 <klokrc74@gmail.com>
kylexqian
approved these changes
Apr 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Twins.chat() is a synchronous function that uses a blocking httpx.post()
call with a 60 second timeout.
LLM.chat() in the same SDK is correctly defined as async. Twins.chat()
is not, which creates a serious problem when both are used together in
any async application such as FastAPI, LangChain agents, or any asyncio
based service.
When Twins.chat() is called from inside an async context, the blocking
httpx.post() call freezes the entire event loop for up to 60 seconds.
During that time no other async task in the application can run. Other
requests, background jobs, and inference calls all stall completely.
For a chat API that handles many concurrent users this is a full
availability failure.
Fix
Two changes to src/opengradient/client/twins.py:
Changed def chat() to async def chat() so it works correctly
inside async applications.
Replaced the blocking httpx.post() call with a non-blocking
httpx.AsyncClient used as an async context manager so the event
loop is never blocked.
The behavior and return value are identical. Only the execution model
changed from blocking to non-blocking.
Files Changed
replaced httpx.post() with httpx.AsyncClient