Fix responses.parse() memory leak from runtime generic schema rebuilds#3092
Open
erhan1209 wants to merge 1 commit intoopenai:mainfrom
Open
Fix responses.parse() memory leak from runtime generic schema rebuilds#3092erhan1209 wants to merge 1 commit intoopenai:mainfrom
erhan1209 wants to merge 1 commit intoopenai:mainfrom
Conversation
## Summary This fixes issue `openai#3084`. `responses.parse()` was constructing parameterized generic response models at runtime: - `ParsedResponseOutputText[TextFormatT]` - `ParsedResponseOutputMessage[TextFormatT]` - `ParsedResponse[TextFormatT]` Those unresolved generics can cause Pydantic to repeatedly rebuild schemas instead of reusing them, which leads to memory growth in long-running processes. ## What changed - Switched runtime construction in `src/openai/lib/_parsing/_responses.py` to use the non-parameterized classes: - `ParsedResponseOutputText` - `ParsedResponseOutputMessage` - `ParsedResponse` - Kept the generic return typing via `cast(...)`, so the public type experience stays the same. - Added a regression test to verify `parse_response()` only passes non-parameterized runtime response types into `construct_type_unchecked`. ## Why this is safe The generic parameter is only needed for static typing here. At runtime, the parsed response models already use non-parameterized field definitions outside `TYPE_CHECKING`, so constructing the base classes preserves behavior while avoiding repeated schema rebuilds. ## Verification Ran: - `$env:PYTHONPATH='src'; python -m pytest -q tests/lib/responses/test_responses.py -n 0` - `$env:PYTHONPATH='src'; python -m pytest -q tests/test_models.py tests/test_response.py -n 0`
afurm
reviewed
Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes being requested
Fix issue
#3084inresponses.parse().The parser was constructing parameterized generic response models at runtime:
ParsedResponseOutputText[TextFormatT]ParsedResponseOutputMessage[TextFormatT]ParsedResponse[TextFormatT]With Pydantic v2, feeding unresolved generics into runtime construction can trigger repeated schema rebuilds instead of stable reuse, which can cause memory growth in long-running processes using
responses.parse().This change switches runtime construction in
src/openai/lib/_parsing/_responses.pyto the non-parameterized classes:ParsedResponseOutputTextParsedResponseOutputMessageParsedResponseThe generic return typing is preserved with
cast(...), so the static typing behavior remains unchanged.A regression test was added to verify that
parse_response()only passes non-parameterized runtime response types intoconstruct_type_unchecked.Additional context & links
Fixes #3084
Verification:
$env:PYTHONPATH='src'; python -m pytest -q tests/lib/responses/test_responses.py -n 0$env:PYTHONPATH='src'; python -m pytest -q tests/test_models.py tests/test_response.py -n 0