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 <douwe@pydantic.dev>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Alex Hall <alex.mojaki@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com> Co-authored-by: burtenshaw <ben.burtenshaw@gmail.com>
…#2210) Co-authored-by: Douwe Maan <me@douwe.me>
…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 <douwe@pydantic.dev>
… media type (pydantic#2270) Co-authored-by: David <david@inspectify.com>
…ient` arg (pydantic#2214) Co-authored-by: Douwe Maan <douwe@pydantic.dev>
Co-authored-by: Douwe Maan <douwe@pydantic.dev>
…tic#2282) Co-authored-by: Douwe Maan <douwe@pydantic.dev>
Co-authored-by: Alex Hall <alex.mojaki@gmail.com>
Co-authored-by: Douwe Maan <douwe@pydantic.dev>
… with output tools (pydantic#2314) Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Douwe Maan <DouweM@users.noreply.github.com>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Douwe Maan <DouweM@users.noreply.github.com>
Signed-off-by: Saurabh Misra <misra.saurabh1@gmail.com>
| 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_langchaininpydantic_ai_slim/pydantic_ai/ext/langchain.py⏱️ Runtime :
406 microseconds→248 microseconds(best of158runs)📝 Explanation and details
Here is an optimized version of your program. The main runtime cost in your code is in the
tool_from_langchainfunction, especially dictionary copying and sorting for therequiredfields and defaults. I've minimized unnecessary work, memory allocations, and dict operations by.setforrequirednames just to immediately sort it - instead, produce an ordered list once.required,defaultsin a single iteration.schema['required'] = requiredbefore 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_conversionext/test_langchain.py::test_langchain_tool_conversion_no_defaultsext/test_langchain.py::test_langchain_tool_conversion_no_requiredext/test_langchain.py::test_langchain_tool_default_overrideext/test_langchain.py::test_langchain_tool_defaultsext/test_langchain.py::test_langchain_tool_no_additional_propertiesext/test_langchain.py::test_langchain_tool_positional🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-tool_from_langchain-mdexqq2rand push.