fix(parsing): drop TextFormatT parameterization in parse_response to fix memory leak (#3084)#3088
Open
MukundaKatta wants to merge 1 commit intoopenai:mainfrom
Open
Conversation
…enai#3084) Closes openai#3084. parse_response() called construct_type_unchecked with three types parameterized by a free TypeVar (ParsedResponseOutputText[TextFormatT], ParsedResponseOutputMessage[TextFormatT], ParsedResponse[TextFormatT]). Pydantic cannot resolve a free TypeVar, so model_rebuild(raise_errors=False) returns False on every invocation. MockCoreSchema._built_memo only caches the rebuilt schema when model_rebuild succeeds; when it fails, the cache is never set and a new Rust-backed SchemaValidator/SchemaSerializer is allocated on every parse_response() call. Result: heavy pydantic-core objects leak without bound whenever AsyncResponses.parse() is called in a long-lived process. This is observable as a flame-graph spike (see openai#3084 for the screenshot). Fix (per issue author's diagnosis): drop the [TextFormatT] parameterization at the runtime type_= argument. At runtime Python's generics are erased anyway — the constructed object's type is identical either way. Only the pydantic schema rebuild path differs: with the non-parameterized generic, model_rebuild succeeds and the schema is cached in _built_memo. Tests: all 5 existing tests in tests/lib/responses/test_responses.py continue to pass (verified locally). No behavior change for callers. Signed-off-by: Mukunda Katta <mukunda.vjcs6@gmail.com>
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.
Why
Closes #3084.
AsyncResponses.parse()leaks pydantic schema objects without bound. The issue author captured it with a flame graph — every call allocates a fresh Rust-backedSchemaValidator/SchemaSerializerthat never gets freed.Root cause
parse_response()calledconstruct_type_uncheckedwith three types parameterized by a free module-levelTypeVar:Pydantic can't resolve a free
TypeVar, somodel_rebuild(raise_errors=False)returnsFalse.MockCoreSchema._built_memoonly caches the rebuilt schema whenmodel_rebuildsucceeds — so the cache is never populated and a new schema is allocated on every call.Fix
Drop the
[TextFormatT]parameterization at thetype_=argument. At runtime Python's generics are erased anyway — the constructed object's type is identical either way. Only the pydantic schema rebuild path differs:type_argParsedResponse[TextFormatT]ParsedResponsemodel_rebuildFalse_built_memoThis is exactly what the issue author diagnosed.
Verification
tests/lib/responses/test_responses.pysuite locally: 5/5 passing — no functional regression.Scope
Minimal — 3 sites in one file, inline comments pointing back to #3084 for maintainability. No public API change, no test changes needed (existing tests cover the parse path).