Home / Class/ StateFileSearchMiddleware Class — langchain Architecture

StateFileSearchMiddleware Class — langchain Architecture

Architecture documentation for the StateFileSearchMiddleware class in file_search.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  90afd8f3_1e0a_8505_387a_6cda18a008c3["StateFileSearchMiddleware"]
  949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e["AgentMiddleware"]
  90afd8f3_1e0a_8505_387a_6cda18a008c3 -->|extends| 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e
  2be37047_277b_5443_dc33_7aebad3faaa4["file_search.py"]
  90afd8f3_1e0a_8505_387a_6cda18a008c3 -->|defined in| 2be37047_277b_5443_dc33_7aebad3faaa4
  5e762dee_61b5_cd13_bdc0_96a8285f41ee["__init__()"]
  90afd8f3_1e0a_8505_387a_6cda18a008c3 -->|method| 5e762dee_61b5_cd13_bdc0_96a8285f41ee
  8a879678_2844_abbb_fe87_14ae1c8a856c["_handle_glob_search()"]
  90afd8f3_1e0a_8505_387a_6cda18a008c3 -->|method| 8a879678_2844_abbb_fe87_14ae1c8a856c
  9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e["_handle_grep_search()"]
  90afd8f3_1e0a_8505_387a_6cda18a008c3 -->|method| 9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e
  2fea0616_83be_c78c_1ec3_10dc4ee6b2a3["_format_grep_results()"]
  90afd8f3_1e0a_8505_387a_6cda18a008c3 -->|method| 2fea0616_83be_c78c_1ec3_10dc4ee6b2a3

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/file_search.py lines 87–349

class StateFileSearchMiddleware(AgentMiddleware):
    """Provides Glob and Grep search over state-based files.

    This middleware adds two tools that search through virtual files in state:

    - Glob: Fast file pattern matching by file path
    - Grep: Fast content search using regular expressions

    Example:
        ```python
        from langchain.agents import create_agent
        from langchain.agents.middleware import (
            StateTextEditorToolMiddleware,
            StateFileSearchMiddleware,
        )

        agent = create_agent(
            model=model,
            tools=[],
            middleware=[
                StateTextEditorToolMiddleware(),
                StateFileSearchMiddleware(),
            ],
        )
        ```
    """

    state_schema = AnthropicToolsState

    def __init__(
        self,
        *,
        state_key: str = "text_editor_files",
    ) -> None:
        """Initialize the search middleware.

        Args:
            state_key: State key to search

                Use `'memory_files'` to search memory tool files.
        """
        self.state_key = state_key

        # Create tool instances
        @tool
        def glob_search(  # noqa: D417
            runtime: ToolRuntime[None, AnthropicToolsState],
            pattern: str,
            path: str = "/",
        ) -> str:
            """Fast file pattern matching tool that works with any codebase size.

            Supports glob patterns like `**/*.js` or `src/**/*.ts`.

            Returns matching file paths sorted by modification time.

            Use this tool when you need to find files by name patterns.

            Args:
                pattern: The glob pattern to match files against.
                path: The directory to search in.

                    If not specified, searches from root.

            Returns:
                Newline-separated list of matching file paths, sorted by modification
                    time (most recently modified first).

                    Returns `'No files found'` if no matches.
            """
            return self._handle_glob_search(pattern, path, runtime.state)

        @tool
        def grep_search(  # noqa: D417
            runtime: ToolRuntime[None, AnthropicToolsState],
            pattern: str,
            path: str = "/",
            include: str | None = None,
            output_mode: Literal[
                "files_with_matches", "content", "count"
            ] = "files_with_matches",

Extends

Frequently Asked Questions

What is the StateFileSearchMiddleware class?
StateFileSearchMiddleware is a class in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/file_search.py.
Where is StateFileSearchMiddleware defined?
StateFileSearchMiddleware is defined in libs/partners/anthropic/langchain_anthropic/middleware/file_search.py at line 87.
What does StateFileSearchMiddleware extend?
StateFileSearchMiddleware extends AgentMiddleware.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free