Commit c30059a
Optimize tool call conversions to eliminate redundant API lookups
## 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]>1 parent 8b18374 commit c30059a
1 file changed
+28
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
250 | | - | |
251 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
252 | 255 | | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
| 256 | + | |
| 257 | + | |
258 | 258 | | |
259 | 259 | | |
260 | 260 | | |
| |||
622 | 622 | | |
623 | 623 | | |
624 | 624 | | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | | - | |
631 | | - | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
632 | 633 | | |
633 | 634 | | |
634 | 635 | | |
| |||
1375 | 1376 | | |
1376 | 1377 | | |
1377 | 1378 | | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
1378 | 1382 | | |
1379 | 1383 | | |
1380 | 1384 | | |
| |||
1383 | 1387 | | |
1384 | 1388 | | |
1385 | 1389 | | |
| 1390 | + | |
| 1391 | + | |
1386 | 1392 | | |
1387 | | - | |
| 1393 | + | |
1388 | 1394 | | |
1389 | 1395 | | |
1390 | | - | |
| 1396 | + | |
1391 | 1397 | | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
1392 | 1404 | | |
1393 | 1405 | | |
1394 | 1406 | | |
| |||
0 commit comments