Home / Function/ _python_search() — langchain Function Reference

_python_search() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/file_search.py lines 312–353

    def _python_search(
        self, pattern: str, base_path: str, include: str | None
    ) -> dict[str, list[tuple[int, str]]]:
        """Search using Python regex (fallback)."""
        try:
            base_full = self._validate_and_resolve_path(base_path)
        except ValueError:
            return {}

        if not base_full.exists():
            return {}

        regex = re.compile(pattern)
        results: dict[str, list[tuple[int, str]]] = {}

        # Walk directory tree
        for file_path in base_full.rglob("*"):
            if not file_path.is_file():
                continue

            # Check include filter
            if include and not _match_include_pattern(file_path.name, include):
                continue

            # Skip files that are too large
            if file_path.stat().st_size > self.max_file_size_bytes:
                continue

            try:
                content = file_path.read_text()
            except (UnicodeDecodeError, PermissionError):
                continue

            # Search content
            for line_num, line in enumerate(content.splitlines(), 1):
                if regex.search(line):
                    virtual_path = "/" + str(file_path.relative_to(self.root_path))
                    if virtual_path not in results:
                        results[virtual_path] = []
                    results[virtual_path].append((line_num, line))

        return results

Domain

Subdomains

Frequently Asked Questions

What does _python_search() do?
_python_search() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/file_search.py.
Where is _python_search() defined?
_python_search() is defined in libs/langchain_v1/langchain/agents/middleware/file_search.py at line 312.
What does _python_search() call?
_python_search() calls 2 function(s): _match_include_pattern, _validate_and_resolve_path.
What calls _python_search()?
_python_search() is called by 2 function(s): __init__, _ripgrep_search.

Analyze Your Own Codebase

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

Try Supermodel Free