Fix max_runs check discarding successful tool call in run_until_tool_used#553
Merged
Conversation
- an issue with a max_run of 1
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
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.
Problem
LLMChain.run_until_tool_used/3withmax_runs: 1incorrectly errored withExceeded maximum number of runs (1/1)even when the very first LLM call successfully invoked the target tool. The successful tool result was discarded in favor of the max-runs error.The bug also affects any
max_runs: Nconfiguration where the target tool happens to be called on the Nth attempt — the success is masked by the ceiling check.Solution
The pipeline in
LangChain.Chains.LLMChain.Modes.UntilToolUsed.do_run/2runs a series of guarded steps where each step either continues with{:continue, chain}or short-circuits with a terminal state. The original order placedcheck_max_runsimmediately aftercall_llm, so the run counter was incremented and the ceiling check fired beforeexecute_toolsandcheck_until_toolcould observe that the target tool had been called.Reordering moves
check_max_runsto run aftercheck_until_tool. Successful termination is detected first;check_max_runsbecomes a no-op on already-terminal pipeline states. A comment was added at the call site explaining the ordering constraint so it isn't innocently shuffled in the future.Changes
lib/chains/llm_chain/modes/until_tool_used.ex— Movecheck_max_runsto run afterexecute_toolsandcheck_until_toolindo_run/2; add explanatory comment for the ordering constraint.test/chains/llm_chain_test.exs— Add regression test coveringmax_runs: 1with a first-call successful tool invocation; asserts the chain returns theToolResultrather than a max-runs error.Testing
LangChain.Chains.LLMChainTestthat mocksChatOpenAI.call/3to return the target tool call on the first attempt withmax_runs: 1, and asserts both thatupdated_chain.last_message.role == :tooland that the returnedToolResultis non-error.run_until_tool_usedtests (including the pre-existing max-runs exceeded path) continue to cover the failure case where the target tool is never called.