Skip to content

[BUG] file_read search mode returns empty list if results is empty #303

@roeetal

Description

@roeetal

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

1.13.0

Tools Package Version

0.2.12

Tools used

  1. File Read

Python Version

3.12.4

Operating System

macOS 15.7

Installation Method

pip

Steps to Reproduce

from strands_tools.file_read import file_read

file_read({
    "toolUseId": "tooluse_8Xt6CI1ESNG9OkZa6F0Gvg",
    "name": "file_read",
    "input": {
        "path": "/path/to/file",
        "mode": "search",
        "search_pattern": "<pattern that does not exist in file>",
        "context_lines": 8
    }
    }
)

Expected Behavior

The tool call should result in an error indicating no results were found

        {
            "toolUseId": "<tooluse_ok5yc_fORPeK2jZhlq7z4Q>",
            "status": "error",
            "content": [{"text": "No search results found!"}]
        }

Actual Behavior

Response:

{
          "role": "user",
          "content": [
            {
              "toolResult": {
                "toolUseId": "tooluse_8Xt6CI1ESNG9OkZa6F0Gvg",
                "status": "success",
                "content": []
              }
            }
          ]
}

When an agent is operating, this leads to a bedrock exception:

litellm.BadRequestError: BedrockException - {\"message\":\"The model returned the following errors: messages.7: `tool_use` ids were found without `tool_result` blocks immediately after: tooluse_8Xt6CI1ESNG9OkZa6F0Gvg. Each `tool_use` block must have a corresponding `tool_result` block in the next message.\"}

Additional Context

No response

Possible Solution

If results is empty throw an exception instead of returning an empty content block

                elif mode == "search":
                    results = search_file(
                        console,
                        file_path,
                        tool_input.get("search_pattern", ""),
                        tool_input.get("context_lines", file_read_context_lines_default),
                    )
                    if not results:
                        raise Exception("No search results found!")
                    response_content.extend([{"text": r["context"]} for r in results])

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions