xtekky/gpt4free · error · ToolLoopError

Tool loop detected: function '{key[0]}' returned empty resul

Error message

Tool loop detected: function '{key[0]}' returned empty results {empty_counts[key]} times with the same arguments. The target is likely not indexed by this tool (e.g. a git submodule). Switch strategy: use read_file with an absolute path, or list_dir to confirm the file exists. Arguments: {key[1]}

What it means

Error "Tool loop detected: function '{key[0]}' returned empty results {empty_counts[key]} times with the same arguments. The target is likely not indexed by this tool (e.g. a git submodule). Switch strategy: use read_file with an absolute path, or list_dir to confirm the file exists. Arguments: {key[1]}" thrown in xtekky/gpt4free.

Source

Thrown at g4f/api/tool_loop_detection.py:207

                            f"Arguments: {key[1]}",
                            function_name=key[0],
                            arguments=key[1],
                            repeats=call_counts[key],
                        )

        # Tool result message: attribute to the originating call.
        elif role == "tool":
            tool_call_id = message.get("tool_call_id")
            content = message.get("content")
            key = pending_calls.get(tool_call_id) if tool_call_id else None
            # If we can't attribute via id, fall back to the most recent call.
            if key is None and pending_calls:
                # Heuristic: attribute to the last registered pending call.
                key = next(reversed(pending_calls), None)
            if key is not None and _is_empty_result(content):
                empty_counts[key] = empty_counts.get(key, 0) + 1
                if empty_counts[key] >= EMPTY_RESULT_LOOP_THRESHOLD:
                    raise ToolLoopError(
                        f"Tool loop detected: function '{key[0]}' returned empty "
                        f"results {empty_counts[key]} times with the same arguments. "
                        f"The target is likely not indexed by this tool (e.g. a git "
                        f"submodule). Switch strategy: use read_file with an absolute "
                        f"path, or list_dir to confirm the file exists. Arguments: "
                        f"{key[1]}",
                        function_name=key[0],
                        arguments=key[1],
                        repeats=empty_counts[key],
                    )


def has_tool_loop(messages: Messages) -> Optional[ToolLoopError]:
    """Convenience wrapper: return the error object if a loop is detected, else None."""
    try:
        detect_tool_loop(messages)
    except ToolLoopError as e:
        return e

View on GitHub (pinned to 973504e177)

Solutions

  1. Switch to read_file with an absolute path or list_dir, since the target is likely not indexed by this tool.

When it happens

Trigger: Thrown at g4f/api/tool_loop_detection.py:207 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/29dfc6a12a3464b3. Report an issue: GitHub.