Skip to content

LM tool loop treats max_tool_iterations=1 as immediate failure #61

Open

Description

@darinkishore

Summary

LM::execute_tool_loop currently executes the initial tool call, then iterates with for iteration in 1..max_iterations.

When max_tool_iterations is set to 1, that follow-up loop runs zero times and the function always returns Max tool iterations reached, even though one iteration was explicitly allowed.

Why this matters

This makes max_tool_iterations=1 unusable for valid single-iteration tool workflows and can surface as an unexpected hard failure.

Reproduction

  1. Configure an LM with max_tool_iterations = 1.
  2. Make a request that triggers a tool call.
  3. Observe that the initial tool is executed, then the call fails with Max tool iterations reached.

Expected

With max_tool_iterations=1, one follow-up completion turn should be attempted after the initial tool result is appended, or the setting semantics should be clarified and enforced consistently.

Actual

No follow-up completion turn is attempted; the function exits with a max-iterations error.

Candidate fixes

  • Adjust loop bounds (for example 1..=max_iterations) if the current setting represents allowed completion iterations after the initial tool call.
  • Or redefine semantics so max_tool_iterations is total tool-call budget and update logic/docs accordingly.

Activity

  1. darinkishore commented on Feb 12, 2026

    @darinkishore
    CollaboratorAuthor

    Confirmed — exact location

    crates/dspy-rs/src/core/lm/mod.rs:281:

    for iteration in 1..max_iterations {

    When max_tool_iterations = 1, max_iterations = 1, so 1..1 is an empty range — zero iterations.

    Fix: 1..=max_iterations (inclusive range) if the semantics are "number of follow-up completion turns allowed after the initial tool call".

    Alternatively, if max_tool_iterations should mean "total tool-call budget including the initial one", the initial execution already counts as 1, so the loop should be 1..max_iterations (current behavior is correct for that interpretation, but then max_tool_iterations=1 means "execute the tool but don't do a follow-up completion", which is arguably a valid no-op). Clarify the semantics and update the doc comment on the field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions