Skip to content

fix(openai-sdk-python): isolate middleware clients - #1503

Closed
abhinav7x94 wants to merge 4 commits into
supermemoryai:mainfrom
abhinav7x94:fix/python-isolate-openai-wrappers
Closed

abhinav7x94 wants to merge 4 commits into
supermemoryai:mainfrom
abhinav7x94:fix/python-isolate-openai-wrappers

Conversation

@abhinav7x94

@abhinav7x94 abhinav7x94 commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • stop with_supermemory from mutating the supplied OpenAI client
  • create tenant-isolated chat/completions facades, including every raw and streaming response prefix
  • recover the pristine base client when a middleware facade is wrapped again
  • correctly route decorated AsyncOpenAI methods through async memory lookup and persistence
  • preserve the SDK's async streaming context-manager protocol while applying memory work on entry
  • add sync/async regressions for shared-client tenant isolation, conversation persistence, and response variants

Why

with_supermemory replaced client.chat.completions.create in place. Reusing one OpenAI client with different middleware options stacked the wrappers, so one request could retrieve and inject memories from multiple tenants and persist a conversation through multiple tenant configurations.

The wrapper now delegates unchanged SDK resources to the pristine client while overriding only memory-aware completion entry points. The caller's base client remains untouched.

OpenAI's decorated async create methods are not always reported as coroutine functions, and streaming variants intentionally return async context managers synchronously. The follow-up classifies the canonical client correctly and defers async middleware until streaming context entry, preventing skipped writes and unawaited-coroutine warnings.

Validation

  • tenant isolation and persistence regressions pass
  • real AsyncOpenAI 7-path matrix passes on OpenAI 1.102.0 and 3.1.0: normal, three raw-response prefixes, and three streaming-response prefixes
  • every path performs one lookup, one write, one request, one injection, and emits zero RuntimeWarnings
  • streaming body exceptions propagate and underlying responses close correctly
  • real sync normal/raw/streaming paths remain unchanged on both SDK versions
  • full Python SDK suite passes on both SDK versions
  • Black, isort, mypy baseline comparison, and git diff --check

@MaheshtheDev

Copy link
Copy Markdown
Member

🤖 AI-assisted triage, reviewed by @MaheshtheDev.

Thank you for this fix! It's now included in #1799, which bundles the community fixes for the Python wrappers (openai, cartesia, pipecat). Your commit is kept with you as the author, and you're credited as a co-author on the merge. Closing in favor of #1799.

graphite-app Bot pushed a commit that referenced this pull request Oct 14, 2026
…ppers (#1799)

Bundles 10 community fixes for the Python wrappers, each commit kept with its original author. Ships supermemory-openai-sdk 1.0.10, supermemory-cartesia 0.1.5 and supermemory-pipecat 0.1.5.

- openai-sdk-python: shared OpenAI clients no longer leak memories across tenants (#1503), memory search fails open instead of crashing the chat (#1288), the requests fallback runs off the event loop (#1660), get_profile works for users with no memories (#1759).
- cartesia: the wrapped agent runs exactly once per event (#1280, #1670), plain-string and chunk-only search results are kept (#1683, #1693).
- pipecat: memory text stays literal when replacing the injected block (#1647), `InputParams` is exported from the package root (#1575).
- #1503 was hand-ported onto main's newer middleware. #1751 is left out: the v5 agent-framework migration (#1786) appends to the conversation document by design.

Co-Authored-By: abhinav7x94 <204053250+abhinav7x94@users.noreply.github.com>
Co-Authored-By: shamAnimates <145093437+shamAnimates@users.noreply.github.com>
Co-Authored-By: aloktripathi1 <142301030+aloktripathi1@users.noreply.github.com>
Co-Authored-By: Cintu07 <178455858+Cintu07@users.noreply.github.com>
Co-Authored-By: Atharvsinh-codez <201381760+Atharvsinh-codez@users.noreply.github.com>
Co-Authored-By: abhay-codes07 <182421137+abhay-codes07@users.noreply.github.com>
Co-Authored-By: nkootstra <545768+nkootstra@users.noreply.github.com>
Co-Authored-By: Agnik47 <140933190+Agnik47@users.noreply.github.com>
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.

3 participants