generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 13
Optimize tool call conversions to eliminate redundant API lookups #53
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
fede-kamel
wants to merge
3
commits into
oracle:main
Choose a base branch
from
fede-kamel:optimize-tool-call-conversions
base: main
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.
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
Contributor
Author
✅ Live Integration Testing CompleteAll 4 integration tests PASSED with live OCI GenAI API calls: Test Configuration:
Validation Results:
Performance Confirmed:
The optimization is production-ready and performs as expected with live API traffic. |
## Problem Tool call processing had significant redundancy: - chat_tool_calls(response) was called 3 times per request - Tool calls were formatted twice (once in chat_generation_info(), once in _generate()) - For requests with 3 tool calls: 9 total lookups instead of 3 (200% overhead) ## Solution 1. Cache raw_tool_calls in _generate() to fetch once 2. Remove tool call formatting from Provider.chat_generation_info() methods 3. Centralize tool call conversion and formatting in _generate() 4. Add try/except for mock compatibility in hasattr checks ## Performance Impact - Before: 3 calls to chat_tool_calls() per request - After: 1 call to chat_tool_calls() per request - Reduction: 66% fewer API lookups for typical tool-calling workloads - No wasted UUID generation or JSON serialization ## Testing All tool-related unit tests pass: - test_meta_tool_calling ✓ - test_cohere_tool_choice_validation ✓ - test_meta_tool_conversion ✓ - test_ai_message_tool_calls_direct_field ✓ - test_ai_message_tool_calls_additional_kwargs ✓ ## Backward Compatibility ✓ Same additional_kwargs format maintained ✓ Same tool_calls field structure preserved ✓ No breaking changes to public API ✓ All existing tests pass 🤖 Generated with Claude Code Co-Authored-By: Claude <[email protected]>
- Created test_tool_call_optimization.py with 4 test cases - Tests basic tool calling, multiple tools, optimization verification, and Cohere provider - Added detailed PR_DESCRIPTION.md with: - Performance analysis and metrics - Code examples showing before/after - Complete unit test results - Integration test details and requirements - Backward compatibility guarantees
6cea04b to
72cda73
Compare
fede-kamel
added a commit
to fede-kamel/langchain-oracle
that referenced
this pull request
Oct 30, 2025
The test was failing after rebase because it used non-existent OCI SDK classes (models.Tool) and had incorrect expectations about when tool_choice is set to 'none'. Changes: 1. Replace OCI SDK mock objects with Python function (following pattern from other tests in the file) 2. Update test to trigger actual tool_choice=none behavior by exceeding max_sequential_tool_calls limit (3 tool calls) 3. Fix _prepare_request call signature (add stop parameter) 4. Pass bound model kwargs to _prepare_request (required for tools) 5. Update docstring to accurately describe what's being tested The test now correctly validates that tool_choice is set to ToolChoiceNone when the max_sequential_tool_calls limit is reached, preventing infinite tool calling loops. Related to PR oracle#50 (infinite loop fix) and PR oracle#53 (tool call optimization). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
fede-kamel
added a commit
to fede-kamel/langchain-oracle
that referenced
this pull request
Oct 30, 2025
The test was failing after rebase because it used non-existent OCI SDK classes (models.Tool) and had incorrect expectations about when tool_choice is set to 'none'. Changes: 1. Replace OCI SDK mock objects with Python function (following pattern from other tests in the file) 2. Update test to trigger actual tool_choice=none behavior by exceeding max_sequential_tool_calls limit (3 tool calls) 3. Fix _prepare_request call signature (add stop parameter) 4. Pass bound model kwargs to _prepare_request (required for tools) 5. Update docstring to accurately describe what's being tested The test now correctly validates that tool_choice is set to ToolChoiceNone when the max_sequential_tool_calls limit is reached, preventing infinite tool calling loops. Related to PR oracle#50 (infinite loop fix) and PR oracle#53 (tool call optimization). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
72cda73 to
9cc8d4d
Compare
Contributor
Author
✅ Rebased on Latest MainSuccessfully rebased this PR on the latest Picked up commits from main:
Integration Tests: ✅ All 4 tests passing ✓ PASSED: Basic Tool Calling
✓ PASSED: Multiple Tools
✓ PASSED: Optimization Verification
✓ PASSED: Cohere ProviderThis PR is ready for review! 🎉 |
YouNeedCryDear
pushed a commit
that referenced
this pull request
Oct 31, 2025
…#57) The test was failing after rebase because it used non-existent OCI SDK classes (models.Tool) and had incorrect expectations about when tool_choice is set to 'none'. Changes: 1. Replace OCI SDK mock objects with Python function (following pattern from other tests in the file) 2. Update test to trigger actual tool_choice=none behavior by exceeding max_sequential_tool_calls limit (3 tool calls) 3. Fix _prepare_request call signature (add stop parameter) 4. Pass bound model kwargs to _prepare_request (required for tools) 5. Update docstring to accurately describe what's being tested The test now correctly validates that tool_choice is set to ToolChoiceNone when the max_sequential_tool_calls limit is reached, preventing infinite tool calling loops. Related to PR #50 (infinite loop fix) and PR #53 (tool call optimization). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
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.
Performance Optimization: Eliminate Redundant Tool Call Conversions
Overview
This PR optimizes tool call processing in
ChatOCIGenAIby eliminating redundant API lookups and conversions, reducing overhead by 66% for tool-calling workloads.Problem Analysis
Before Optimization
The tool call conversion pipeline had significant redundancy:
Impact:
Root Cause
The `format_response_tool_calls()` output went into `additional_kwargs` (metadata), but the actual `tool_calls` field used a different conversion path (`convert_oci_tool_call_to_langchain`). Both did similar work but neither reused the other's output.
Solution
1. Cache Raw Tool Calls in `_generate()`
2. Remove Redundant Formatting from Providers
3. Centralize Tool Call Processing
4. Improve Mock Compatibility
Performance Impact
Testing
Unit Tests (All Passing ✓)
```bash
$ .venv/bin/python -m pytest tests/unit_tests/chat_models/test_oci_generative_ai.py -k "tool" -v
tests/unit_tests/chat_models/test_oci_generative_ai.py::test_meta_tool_calling PASSED
tests/unit_tests/chat_models/test_oci_generative_ai.py::test_cohere_tool_choice_validation PASSED
tests/unit_tests/chat_models/test_oci_generative_ai.py::test_meta_tool_conversion PASSED
tests/unit_tests/chat_models/test_oci_generative_ai.py::test_ai_message_tool_calls_direct_field PASSED
tests/unit_tests/chat_models/test_oci_generative_ai.py::test_ai_message_tool_calls_additional_kwargs PASSED
================= 5 passed, 7 deselected, 7 warnings in 0.33s ==================
```
Test Coverage:
Integration Test Script
Created `test_tool_call_optimization.py` with 4 comprehensive test cases:
Test 1: Basic Tool Calling
Test 2: Multiple Tools
Test 3: Optimization Verification
Test 4: Cohere Provider
Note: Integration tests require OCI credentials. During development, we encountered expected 401 authentication errors when attempting live API calls. Recommendation: Oracle team should run `test_tool_call_optimization.py` with proper OCI credentials before merging to verify end-to-end functionality.
Backward Compatibility
✅ No Breaking Changes
✅ Code Structure
Files Changed
`libs/oci/langchain_oci/chat_models/oci_generative_ai.py` (+28, -16 lines)
`libs/oci/test_tool_call_optimization.py` (NEW, +300 lines)
Reviewers
This optimization affects the hot path for tool-calling workloads. Please verify:
Testing Checklist:
Deployment Notes: