fix(structured outputs): resolve memory leak in parse methods#2860
fix(structured outputs): resolve memory leak in parse methods#2860RobertCraigie merged 7 commits intoopenai:nextfrom
Conversation
a389776 to
690c4b1
Compare
690c4b1 to
d0c242a
Compare
|
memory leak tests are a bit flaky, tbh, that’s why I didn’t add it |
RobertCraigie
left a comment
There was a problem hiding this comment.
How does pydantic know what class to instantiate now that we aren't actually passing it?
| def clear_locals(string: str, *, stacklevel: int) -> str: | ||
| caller = get_caller_name(stacklevel=stacklevel + 1) | ||
| return string.replace(f"{caller}.<locals>.", "") | ||
| return re.sub(r"([A-Za-z_]\w*)\[[^\[\]]+\](?=\()", r"\1", string) |
There was a problem hiding this comment.
q: what does this do? can you add a comment
There was a problem hiding this comment.
ahh right, I'll add a comment. It's stripping out the generic name
There was a problem hiding this comment.
added a comment, also I've fixed the same leak for streaming too
We’re already instantiating the appropriate class here |
|
ahhh |
Resolves a memory leak in structured outputs parse methods when using Pydantic with runtime generics (openai/openai-python#2860). https://claude.ai/code/session_01Pxc3jHuwJGHxkGehMgQGcP
* Bump openai minimum version to >=2.21.0 to fix memory leak Resolves a memory leak in structured outputs parse methods when using Pydantic with runtime generics (openai/openai-python#2860). https://claude.ai/code/session_01Pxc3jHuwJGHxkGehMgQGcP * Update uv.lock for openai >=2.21.0 https://claude.ai/code/session_01Pxc3jHuwJGHxkGehMgQGcP --------- Co-authored-by: Claude <noreply@anthropic.com>
|
@karpetrosyan Would you please take a look at this other PR #3068 ? We experienced a memory leak after this change and would like your feedback on this |
This PR avoids using runtime generics with Pydantic, which can leak memory, by using a simpler implementation for parsing the response message.
It also fixes generic conflicts in the tests, which happen because of how Pydantic v1 and Pydantic v2 implement
__repr__.Leak results in the main
Leak results in #2860
Leak results in #2148
Running tests locally
To run the test locally, use the following PEP 723 compatible script:
Note that you should set the appropriate branch for the openai-python dependency in the script.
For the main branch, use:
openai @ git+https://github.com/openai/openai-python.git@main,