Commit 2ce0bf5
authored
Fix infinite tool calling loop with Meta Llama models (#50)
* Fix: Enhance tool calling to support multi-step orchestration
## Problem
Meta Llama and other models were stuck in infinite tool calling loops
after receiving tool results. The previous fix set tool_choice="none"
unconditionally after any tool result, which prevented legitimate
multi-step tool orchestration patterns.
## Solution
Implemented intelligent tool_choice management that:
1. Allows models to continue calling tools for multi-step workflows
2. Prevents infinite loops via max_sequential_tool_calls limit (default: 8)
3. Detects infinite loops by identifying repeated tool calls with identical arguments
## Changes
- Added max_sequential_tool_calls parameter to OCIGenAIBase (default: 8)
- Enhanced GenericProvider.messages_to_oci_params() with _should_allow_more_tool_calls()
- Loop detection checks for same tool called with same args in succession
- Safety limit prevents runaway tool calling beyond configured maximum
## Backward Compatibility
✅ Fully backward compatible - no breaking changes
- New parameter is optional with sensible default (8)
- Existing code continues to work without modifications
- Previous infinite loop fix remains active as fallback
## Technical Details
The fix passes max_sequential_tool_calls from ChatOCIGenAI to Provider
via kwargs, allowing the provider to determine whether to set
tool_choice="none" (force stop) or tool_choice="auto" (allow continuation).
* Add comprehensive integration tests for tool calling
## Test Coverage
### 1. Basic Tool Calling Tests (test_tool_calling_no_infinite_loop)
Tests 4 models to ensure basic tool calling works without infinite loops:
- meta.llama-4-scout-17b-16e-instruct
- meta.llama-3.3-70b-instruct
- cohere.command-a-03-2025
- cohere.command-r-plus-08-2024
Verifies:
- Tool is called when needed
- Model stops after receiving tool results
- No infinite loops occur
### 2. Model-Specific Tests
- test_meta_llama_tool_calling: Validates Meta Llama models specifically
- test_cohere_tool_calling: Validates Cohere models return expected content
### 3. Multi-Step Tool Orchestration Test (test_multi_step_tool_orchestration)
Simulates realistic diagnostic workflows with 6 tools (2 models tested):
- meta.llama-4-scout-17b-16e-instruct
- cohere.command-a-03-2025
Tools simulate monitoring scenarios:
- check_status: Current resource health
- get_events: Recent failure events
- get_metrics: Historical trends
- check_changes: Recent deployments
- create_alert: Incident creation
- take_action: Remediation actions
Verifies:
- Agent makes multiple tool calls (2-8)
- Respects max_sequential_tool_calls limit
- Eventually stops (no infinite loops)
- Handles OCI limitation (1 tool call at a time)
## Test Results
All 8 tests passing across 4 models:
✅ Basic tool calling (4 models × 1 test = 4 tests)
✅ Model-specific tests (2 tests)
✅ Multi-step orchestration (2 models × 1 test = 2 tests)
## Documentation
Added comprehensive test documentation including:
- Prerequisites (OCI auth, environment setup)
- Running instructions
- What each test verifies
- Model compatibility notes1 parent 36f40c4 commit 2ce0bf5
File tree
4 files changed
+669
-12
lines changed- libs/oci
- langchain_oci
- chat_models
- llms
- tests
- integration_tests/chat_models
- unit_tests/chat_models
4 files changed
+669
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
372 | 372 | | |
373 | 373 | | |
374 | 374 | | |
375 | | - | |
376 | | - | |
377 | | - | |
| 375 | + | |
378 | 376 | | |
379 | 377 | | |
380 | 378 | | |
| |||
386 | 384 | | |
387 | 385 | | |
388 | 386 | | |
389 | | - | |
390 | | - | |
391 | | - | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
392 | 398 | | |
393 | 399 | | |
394 | 400 | | |
| |||
723 | 729 | | |
724 | 730 | | |
725 | 731 | | |
726 | | - | |
727 | | - | |
| 732 | + | |
| 733 | + | |
728 | 734 | | |
729 | 735 | | |
730 | 736 | | |
| |||
746 | 752 | | |
747 | 753 | | |
748 | 754 | | |
749 | | - | |
| 755 | + | |
750 | 756 | | |
751 | 757 | | |
752 | 758 | | |
753 | 759 | | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
754 | 827 | | |
755 | 828 | | |
756 | 829 | | |
| |||
944 | 1017 | | |
945 | 1018 | | |
946 | 1019 | | |
| 1020 | + | |
947 | 1021 | | |
948 | 1022 | | |
949 | 1023 | | |
| |||
1060 | 1134 | | |
1061 | 1135 | | |
1062 | 1136 | | |
1063 | | - | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
1064 | 1142 | | |
1065 | 1143 | | |
1066 | 1144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
119 | 123 | | |
120 | 124 | | |
121 | 125 | | |
| |||
0 commit comments