fix(structured outputs): resolve memory leak in parse methods#2860
Open
karpetrosyan wants to merge 7 commits intoopenai:nextfrom
Open
fix(structured outputs): resolve memory leak in parse methods#2860karpetrosyan wants to merge 7 commits intoopenai:nextfrom
karpetrosyan wants to merge 7 commits intoopenai:nextfrom
Conversation
a389776 to
690c4b1
Compare
690c4b1 to
d0c242a
Compare
Collaborator
Author
|
memory leak tests are a bit flaky, tbh, that’s why I didn’t add it |
Collaborator
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) |
Collaborator
There was a problem hiding this comment.
q: what does this do? can you add a comment
Collaborator
Author
There was a problem hiding this comment.
ahh right, I'll add a comment. It's stripping out the generic name
Collaborator
Author
There was a problem hiding this comment.
added a comment, also I've fixed the same leak for streaming too
Collaborator
Author
We’re already instantiating the appropriate class here |
Collaborator
|
ahhh |
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.
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,