Commit 2abb91b
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 36f40c4 commit 2abb91b
1 file changed
+28
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
247 | | - | |
248 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
249 | 252 | | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
| 253 | + | |
| 254 | + | |
255 | 255 | | |
256 | 256 | | |
257 | 257 | | |
| |||
609 | 609 | | |
610 | 610 | | |
611 | 611 | | |
612 | | - | |
613 | | - | |
614 | | - | |
615 | | - | |
616 | | - | |
617 | | - | |
618 | | - | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
619 | 620 | | |
620 | 621 | | |
621 | 622 | | |
| |||
1276 | 1277 | | |
1277 | 1278 | | |
1278 | 1279 | | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
1279 | 1283 | | |
1280 | 1284 | | |
1281 | 1285 | | |
| |||
1284 | 1288 | | |
1285 | 1289 | | |
1286 | 1290 | | |
| 1291 | + | |
| 1292 | + | |
1287 | 1293 | | |
1288 | | - | |
| 1294 | + | |
1289 | 1295 | | |
1290 | 1296 | | |
1291 | | - | |
| 1297 | + | |
1292 | 1298 | | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
1293 | 1305 | | |
1294 | 1306 | | |
1295 | 1307 | | |
| |||
0 commit comments