Home / Function/ __init__() — langchain Function Reference

__init__() — langchain Function Reference

Architecture documentation for the __init__() function in file_search.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  99cb0385_d25f_ae58_a625_e8187615e060["__init__()"]
  c970c63b_27bf_364e_6a46_1e5cc38ce38f["FilesystemFileSearchMiddleware"]
  99cb0385_d25f_ae58_a625_e8187615e060 -->|defined in| c970c63b_27bf_364e_6a46_1e5cc38ce38f
  b4e992f2_93ef_4642_9ddf_328ed58f534b["_validate_and_resolve_path()"]
  99cb0385_d25f_ae58_a625_e8187615e060 -->|calls| b4e992f2_93ef_4642_9ddf_328ed58f534b
  c95c91c6_daaf_119c_f394_b216ac96d04e["_ripgrep_search()"]
  99cb0385_d25f_ae58_a625_e8187615e060 -->|calls| c95c91c6_daaf_119c_f394_b216ac96d04e
  3f120244_d865_db15_009c_7c3c7671ebd6["_python_search()"]
  99cb0385_d25f_ae58_a625_e8187615e060 -->|calls| 3f120244_d865_db15_009c_7c3c7671ebd6
  dd8bd3f3_4ef2_0063_3646_d8a083bd6388["_format_grep_results()"]
  99cb0385_d25f_ae58_a625_e8187615e060 -->|calls| dd8bd3f3_4ef2_0063_3646_d8a083bd6388
  943f681c_e0cd_4a26_5c61_e9c06d70955a["_is_valid_include_pattern()"]
  99cb0385_d25f_ae58_a625_e8187615e060 -->|calls| 943f681c_e0cd_4a26_5c61_e9c06d70955a
  style 99cb0385_d25f_ae58_a625_e8187615e060 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/file_search.py lines 112–233

    def __init__(
        self,
        *,
        root_path: str,
        use_ripgrep: bool = True,
        max_file_size_mb: int = 10,
    ) -> None:
        """Initialize the search middleware.

        Args:
            root_path: Root directory to search.
            use_ripgrep: Whether to use `ripgrep` for search.

                Falls back to Python if `ripgrep` unavailable.
            max_file_size_mb: Maximum file size to search in MB.
        """
        self.root_path = Path(root_path).resolve()
        self.use_ripgrep = use_ripgrep
        self.max_file_size_bytes = max_file_size_mb * 1024 * 1024

        # Create tool instances as closures that capture self
        @tool
        def glob_search(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.
            """
            try:
                base_full = self._validate_and_resolve_path(path)
            except ValueError:
                return "No files found"

            if not base_full.exists() or not base_full.is_dir():
                return "No files found"

            # Use pathlib glob
            matching: list[tuple[str, str]] = []
            for match in base_full.glob(pattern):
                if match.is_file():
                    # Convert to virtual path
                    virtual_path = "/" + str(match.relative_to(self.root_path))
                    stat = match.stat()
                    modified_at = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat()
                    matching.append((virtual_path, modified_at))

            if not matching:
                return "No files found"

            file_paths = [p for p, _ in matching]
            return "\n".join(file_paths)

        @tool
        def grep_search(
            pattern: str,
            path: str = "/",
            include: str | None = None,
            output_mode: Literal["files_with_matches", "content", "count"] = "files_with_matches",
        ) -> str:
            """Fast content search tool that works with any codebase size.

            Searches file contents using regular expressions. Supports full regex
            syntax and filters files by pattern with the include parameter.

            Args:
                pattern: The regular expression pattern to search for in file contents.
                path: The directory to search in. If not specified, searches from root.
                include: File pattern to filter (e.g., `'*.js'`, `'*.{ts,tsx}'`).
                output_mode: Output format:

Domain

Subdomains

Frequently Asked Questions

What does __init__() do?
__init__() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/file_search.py.
Where is __init__() defined?
__init__() is defined in libs/langchain_v1/langchain/agents/middleware/file_search.py at line 112.
What does __init__() call?
__init__() calls 5 function(s): _format_grep_results, _is_valid_include_pattern, _python_search, _ripgrep_search, _validate_and_resolve_path.

Analyze Your Own Codebase

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

Try Supermodel Free