forked from pydantic/pydantic-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
⚡️ Speed up function tool_from_langchain
by 64%
#30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codeflash-ai
wants to merge
90
commits into
try-refinement
Choose a base branch
from
codeflash/optimize-tool_from_langchain-mdexqq2r
base: try-refinement
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
⚡️ Speed up function tool_from_langchain
by 64%
#30
codeflash-ai
wants to merge
90
commits into
try-refinement
from
codeflash/optimize-tool_from_langchain-mdexqq2r
Conversation
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
…ydantic#2157) Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
) Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Alex Hall <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]> Co-authored-by: burtenshaw <[email protected]>
…#2210) Co-authored-by: Douwe Maan <[email protected]>
…pydantic#2196) Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
…2198) Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
Co-authored-by: Douwe Maan <[email protected]>
… media type (pydantic#2270) Co-authored-by: David <[email protected]>
…ient` arg (pydantic#2214) Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Douwe Maan <[email protected]>
…tic#2282) Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Alex Hall <[email protected]>
Co-authored-by: Douwe Maan <[email protected]>
… with output tools (pydantic#2314) Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Douwe Maan <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
This PR is now faster! 🚀 Saurabh Misra accepted my code suggestion above. |
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.
📄 64% (0.64x) speedup for
tool_from_langchain
inpydantic_ai_slim/pydantic_ai/ext/langchain.py
⏱️ Runtime :
406 microseconds
→248 microseconds
(best of158
runs)📝 Explanation and details
Here is an optimized version of your program. The main runtime cost in your code is in the
tool_from_langchain
function, especially dictionary copying and sorting for therequired
fields and defaults. I've minimized unnecessary work, memory allocations, and dict operations by.set
forrequired
names just to immediately sort it - instead, produce an ordered list once.required
,defaults
in a single iteration.schema['required'] = required
before setting'additionalProperties'
so overwrites don't happen.Summary of high-impact changes:
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
ext/test_langchain.py::test_langchain_tool_conversion
ext/test_langchain.py::test_langchain_tool_conversion_no_defaults
ext/test_langchain.py::test_langchain_tool_conversion_no_required
ext/test_langchain.py::test_langchain_tool_default_override
ext/test_langchain.py::test_langchain_tool_defaults
ext/test_langchain.py::test_langchain_tool_no_additional_properties
ext/test_langchain.py::test_langchain_tool_positional
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-tool_from_langchain-mdexqq2r
and push.