Skip to content

fix(openai-sdk-python): run the requests fallback off the event loop - #1660

Open
Cintu07 wants to merge 1 commit into
supermemoryai:mainfrom
Cintu07:fix/profile-search-fallback-off-loop
Open

Cintu07 wants to merge 1 commit into
supermemoryai:mainfrom
Cintu07:fix/profile-search-fallback-off-loop

Conversation

@Cintu07

@Cintu07 Cintu07 commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

supermemory_profile_search is async, but without aiohttp it falls back to requests.post, which holds the event loop for the whole request. aiohttp is only in the async extra and requests is a hard dependency, so a plain pip install supermemory-openai-sdk always lands on the fallback.

numbers

Local server that takes 2s to answer the profile search, with a ticker on the same loop that should fire every 50ms. Longest gap between ticks:

default install with aiohttp
calling supermemory_profile_search on main 2.10s
same call with this change 0.09s
AsyncOpenAI with #1503's dispatch fix 2.14s 0.08s
AsyncOpenAI with #1503's dispatch fix and this change 0.14s

#1503 fixes which path AsyncOpenAI takes and this fixes the path it lands on, so a default install needs both. #1503's branch is behind main, so I applied its dispatch check to main for those two rows.

fix

await asyncio.to_thread(requests.post, ...). to_thread is 3.9+, which matches requires-python.

test

test_requests_fallback_does_not_block_event_loop in tests/test_middleware.py forces the fallback by setting aiohttp to None in sys.modules, and makes requests.post wait on an event that only the loop can set. It fails on main with "requests.post blocked the event loop" and passes here. The rest of the package suite is unchanged, 32 passed and 11 skipped.

supermemory_profile_search is async, but without the optional aiohttp
extra it falls back to requests.post, which blocks whatever event loop
awaited it for the whole request. aiohttp is not installed by default,
so on a plain pip install every memory search on the async path stalls
the caller's loop.

The fallback now runs requests.post through asyncio.to_thread. The new
test forces the fallback and fails if the request holds the loop.
Copilot AI lite review requested due to automatic review settings September 11, 2026 11:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants