openai-python
openai-python copied to clipboard
Fix memory leak in AsyncCompletions.parse() with dynamically created models
Fixes #2146
This PR fixes a memory leak in the manually maintained library code (src/openai/lib/_parsing/_completions.py) where AsyncCompletions.parse() was retaining schema representations of dynamically created Pydantic models indefinitely.
Changes Made:
- Implemented WeakKeyDictionary cache for schema objects in _parsing/_completions.py
- Added comprehensive test suite in tests/lib/_parsing/test_memory_leak.py
Technical Details:
The fix uses Python's WeakKeyDictionary to store schema representations, allowing them to be garbage collected when their corresponding model types are no longer referenced. This prevents the unbounded memory growth observed when repeatedly calling parse() with new models created via create_model().
Test Coverage:
New test suite verifies:
- Schema cache properly handles dynamic models
- Memory is released when models are no longer referenced
- Both synchronous and asynchronous usage patterns
References:
- Issue: #2146
- Memory leak reproduction case provided in the issue