From fc9525026a348cb3fcc78441cb2c3de728f50f7f Mon Sep 17 00:00:00 2001 From: Jah-yee <166608075+Jah-yee@users.noreply.github.com> Date: Fri, 24 Apr 2026 21:21:47 +0800 Subject: [PATCH] fix: resolve unbounded memory leak from TypeVar parameterized ParsedResponse types Root cause: ParsedResponse[TextFormatT] where TextFormatT is a free TypeVar causes model_rebuild to always return False, preventing MockCoreSchema._built_memo caching. This causes a new SchemaValidator/SchemaSerializer (heavy Rust object) to be allocated on every responses.parse() call, accumulating without bound. Fix: Use non-parameterized base classes (ParsedResponse, ParsedResponseOutputText, ParsedResponseOutputMessage) which are functionally identical at runtime but allow pydantic's model_rebuild to succeed and cache the built schemas. Fixes #3084 --- src/openai/lib/_parsing/_responses.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/openai/lib/_parsing/_responses.py b/src/openai/lib/_parsing/_responses.py index 8853a0749f..df48047d1c 100644 --- a/src/openai/lib/_parsing/_responses.py +++ b/src/openai/lib/_parsing/_responses.py @@ -68,7 +68,7 @@ def parse_response( content_list.append( construct_type_unchecked( - type_=ParsedResponseOutputText[TextFormatT], + type_=ParsedResponseOutputText, value={ **item.to_dict(), "parsed": parse_text(item.text, text_format=text_format), @@ -78,7 +78,7 @@ def parse_response( output_list.append( construct_type_unchecked( - type_=ParsedResponseOutputMessage[TextFormatT], + type_=ParsedResponseOutputMessage, value={ **output.to_dict(), "content": content_list, @@ -130,7 +130,7 @@ def parse_response( output_list.append(output) return construct_type_unchecked( - type_=ParsedResponse[TextFormatT], + type_=ParsedResponse, value={ **response.to_dict(), "output": output_list,