Skip to content

Malformed tool-call format errors are masked by the "output truncated" template branch (finish_reason == "tool_calls") #894

Closed

Description

@simple-zy

Describe the bug

Summary

When the model returns a tool call that is present but malformed (wrong tool name, missing command, or complete-but-invalid JSON arguments), the correction message sent back to the model is the "your previous response was cut off due to the output token limit" prompt, and the real error (e.g. Unknown tool 'shell'.) is never shown to the model. This can cause the model to keep retrying in the wrong direction until it hits max_consecutive_format_errors and exits with RepeatedFormatError.

Affected version

v2.4.4 (and likely earlier).

Where

Parser passes finish_reason into the error template:

  • src/minisweagent/models/utils/actions_toolcall.py (parse_toolcall_actions)
  • src/minisweagent/models/litellm_model.py → template_kwargs={"finish_reason": response.choices[0].finish_reason}

Templates that contain the problematic branch (finish_reason in ["length", "tool_calls"]):

  • src/minisweagent/config/mini.yaml:130
  • src/minisweagent/config/benchmarks/swebench.yaml:160
  • src/minisweagent/config/benchmarks/programbench.yaml:260
  • src/minisweagent/config/default.yaml:145 (text-based)
  • src/minisweagent/config/mini_textbased.yaml:146 (text-based)
  • src/minisweagent/config/benchmarks/swebench_backticks.yaml:219 (text-based)
  • src/minisweagent/config/benchmarks/swebench_xml.yaml:204 (text-based)

The tool-call configs (mini.yaml, swebench.yaml, programbench.yaml) are the ones actually affected, because only tool-call responses produce finish_reason == "tool_calls". The text-based configs carry the same branch but are not hit in practice (they do not use tools).

The template branch in question

{% if finish_reason is defined and finish_reason in ["length", "tool_calls"] -%}
Your previous response reached the output token limit (finish_reason={{ finish_reason }}) before you produced a tool call, so it was cut off. Respond more concisely and finish with exactly one bash tool call. If you need to think more, do so briefly.
{%- else -%}
Tool call error:

<error>
{{error}}
</error>
...
{%- endif %}

Root cause

The condition treats "length" (a genuine truncation) and "tool_calls" (a normal completion that produced tool calls) identically. When a model produces a tool call and finishes normally, providers return finish_reason == "tool_calls". If that tool call fails validation in parse_toolcall_actions (unknown tool name, missing command, or invalid-but-complete JSON), a FormatError is raised — but because finish_reason == "tool_calls", the template renders the truncation message instead of {{ error }}. The actual validation error is discarded from the model's view.

Steps to reproduce

Have the model emit a tool call with a wrong tool name, e.g.:

{
  "role": "assistant",
  "content": "Let me look at the directory.",
  "tool_calls": [
    {"id": "call_x", "type": "function",
     "function": {"name": "shell", "arguments": "{\"command\": \"ls\"}"}}
  ]
}

Here finish_reason == "tool_calls".

Expected correction message to the model:

Tool call error:

<error>
Unknown tool 'shell'.
</error>
...guidance on how to call the bash tool...

Actual correction message to the model:

Your previous response reached the output token limit (finish_reason=tool_calls) before you produced a tool call, so it was cut off. Respond more concisely and finish with exactly one bash tool call. If you need to think more, do so briefly.

The Unknown tool 'shell'. error is never surfaced.

Activity

  1. added
    bug
    on Jul 6, 2026
  2. klieret commented on Jul 6, 2026

    @klieret
    Member

    thanks for raising this, this should indeed be fixed

  3. Batman0x0000001 commented on Jul 6, 2026

    @Batman0x0000001

    Hi, I’d like to try working on this issue.

    My understanding is that finish_reason == "tool_calls" should not be treated as an output truncation case. I plan to make a small fix by narrowing the affected format_error_template checks to only handle finish_reason == "length", and add a focused regression test for malformed tool calls with finish_reason == "tool_calls".

    Please let me know if this direction sounds reasonable.

  4. added a commit that references this issue on Jul 6, 2026
    147121f
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions