Memory leak in parse_response usage of pydantic#3068
Memory leak in parse_response usage of pydantic#3068avilaton wants to merge 1 commit intoopenai:mainfrom
Conversation
|
This is a real issue |
|
|
No preference, we are just trying to contain a memory leak that caused us a lot of headaches which are much worse than any type checking error we might have wanted to avoid. Any solution to the problem works, we are in fact only consumers of the library and wanted to see if we could raise this issue. Thanks for replying, we don't have a preference on how it gets resolved but did see at least 3 memory related issues. This made it to production for us before we noticed the oomkilled errors on our pods. |
|
I tried this PR and it didn't seem to work for me. Perhaps I did something incorrectly. But there are definitely memory leak issues with the Responses async .parse() |
Changes being requested
Address a memory leak in parse_response detected using the OpenAI client in a webserver context (gunicorn, gevent)
Additional context & links
Problem
client.responses.parse()can trigger sustained memory growth on pydantic >= 2.11 (see #1181).The issue is that
parse_response()used subscripted runtime generic aliases (for exampleParsedResponse[T]) when callingconstruct_type_unchecked(). In pydantic v2, this can cause repeated runtime generic specialization/schema work in a hot path.Fix
Use the non-subscripted runtime classes in
parse_response()when callingconstruct_type_unchecked():ParsedResponseOutputText[TextFormatT]→ParsedResponseOutputTextParsedResponseOutputMessage[TextFormatT]→ParsedResponseOutputMessageParsedResponse[TextFormatT]→ParsedResponseWhy this is safe
construct_type_unchecked()constructs models loosely and does not require runtime generic specialization for correctness here.parse_text()still produces the typedparsedpayload, and the return type for callers remainsParsedResponse[TextFormatT].Tests
tests/lib/responses/test_parsing.pythat fails ifparse_response()routes through_validate_non_model_type.OPENAI_RUN_MEMORY_TESTS=1) intests/lib/responses/test_parsing.py.