supermemory icon indicating copy to clipboard operation
supermemory copied to clipboard

add openai middleware functionality for python sdk

Open nexxeln opened this issue 2 months ago • 3 comments

add openai middleware functionality

fix critical type errors and linting issues

update readme with middleware documentation

nexxeln avatar Oct 31 '25 15:10 nexxeln


How to use the Graphite Merge Queue

Add the label Main to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

nexxeln avatar Oct 31 '25 15:10 nexxeln

Pull Request Review - OpenAI Middleware for Python SDK

Summary

This PR adds automatic memory injection middleware functionality to the Python OpenAI SDK. The implementation is well-structured and the API design is intuitive. However, there are several important issues that should be addressed before merging.


Critical Issues

1. Event Loop Management in Sync Client (middleware.py:335-349)

Issue: Creating a new event loop for each synchronous request is problematic and can cause resource leaks.

Problems:

  • Setting the global event loop can interfere with other async code
  • Creating/closing event loops on every request is inefficient
  • Can cause issues with existing event loops in async contexts
  • May leave dangling tasks from asyncio.create_task() in line 299

Recommendation: Use asyncio.run() instead or maintain a single thread-local event loop.

2. Fire-and-Forget Task May Be Lost (middleware.py:299-307)

Issue: Background task for memory storage may not complete before program exit.

Problems:

  • No task tracking or cleanup
  • Task may be cancelled/lost if the event loop closes
  • No guarantee the memory is actually saved
  • Silent failures won't be noticed

Recommendation: Maintain a set of background tasks and await them on cleanup, or make memory storage synchronous with a timeout.

3. Generic Exception Handling

Issue: Catching bare Exception is too broad and may hide unexpected errors.

Recommendation: Create custom exception classes like SupermemoryAPIError.


Major Issues

4. Hardcoded API URL (middleware.py:59, 80)

Issue: API URL is hardcoded, making testing difficult.

Recommendation: Extract to a constant or environment variable.

5. Unused Utility Functions (utils.py:241-309)

Issue: Several functions are defined but not used: filter_out_supermemories, validate_api_key, safe_json_loads, truncate_text.

Recommendation: Remove unused code.


Security Concerns

6. Logging Sensitive Data (middleware.py:163-169)

Issue: Full memory content logged in debug mode could expose PII.

Recommendation: Truncate or redact sensitive content in logs.


Test Coverage

Strengths:

  • Comprehensive test suite with good coverage
  • Tests for sync/async compatibility
  • Error handling tests

Gaps:

  1. No integration tests
  2. Missing edge cases (empty messages, multimodal content, timeouts)
  3. No tests for utils.py functions
  4. No concurrent request testing

Recommendation: Add integration tests and expand edge case coverage.


Performance Considerations

7. API Call on Every Request

Issue: Every chat completion makes a synchronous API call, adding 50-200ms+ latency.

Recommendations:

  • Add caching layer for profile memories
  • Consider configurable timeout
  • Document performance impact

Overall Assessment

Rating: 7.5/10

Solid implementation with clean API design and good documentation. Critical issues around event loop management and background task handling need to be addressed before production use.

Required Before Merge:

  1. Fix event loop management in sync client
  2. Fix background task management
  3. Replace generic exceptions with custom exceptions
  4. Remove unused utility functions

Recommended:

  • Add integration tests
  • Extract API URL to configuration
  • Add retry logic and caching

Great work on the comprehensive documentation and test coverage!

claude[bot] avatar Oct 31 '25 15:10 claude[bot]

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
supermemory-app ea9bf13d Nov 11 2025, 02:26 AM

Merge activity

graphite-app[bot] avatar Nov 11 '25 02:11 graphite-app[bot]

Video gif. An elderly man in suspenders smiles as he holds up two thumbs as if saying good luck.  (Added via Giphy)

graphite-app[bot] avatar Nov 11 '25 02:11 graphite-app[bot]