Home / Function/ _handle_grep_search() — langchain Function Reference

_handle_grep_search() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e["_handle_grep_search()"]
  90afd8f3_1e0a_8505_387a_6cda18a008c3["StateFileSearchMiddleware"]
  9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e -->|defined in| 90afd8f3_1e0a_8505_387a_6cda18a008c3
  5e762dee_61b5_cd13_bdc0_96a8285f41ee["__init__()"]
  5e762dee_61b5_cd13_bdc0_96a8285f41ee -->|calls| 9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e
  2fea0616_83be_c78c_1ec3_10dc4ee6b2a3["_format_grep_results()"]
  9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e -->|calls| 2fea0616_83be_c78c_1ec3_10dc4ee6b2a3
  60184a91_fab8_982f_769b_44d51b7e4b97["_is_valid_include_pattern()"]
  9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e -->|calls| 60184a91_fab8_982f_769b_44d51b7e4b97
  54e626eb_8746_c714_57b6_ac728c78ed52["_match_include_pattern()"]
  9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e -->|calls| 54e626eb_8746_c714_57b6_ac728c78ed52
  style 9c9a2eda_be6c_5f6f_9a0a_3f3e5575dc2e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/file_search.py lines 261–320

    def _handle_grep_search(
        self,
        pattern: str,
        path: str,
        include: str | None,
        output_mode: str,
        state: AnthropicToolsState,
    ) -> str:
        """Handle grep search operation.

        Args:
            pattern: The regular expression pattern to search for in file contents.
            path: The directory to search in.
            include: File pattern to filter (e.g., `'*.js'`, `'*.{ts,tsx}'`).
            output_mode: Output format.
            state: The current agent state.

        Returns:
            Search results formatted according to `output_mode`.

                Returns `'No matches found'` if no results.
        """
        # Normalize base path
        base_path = path if path.startswith("/") else "/" + path

        # Compile regex pattern (for validation)
        try:
            regex = re.compile(pattern)
        except re.error as e:
            return f"Invalid regex pattern: {e}"

        if include and not _is_valid_include_pattern(include):
            return "Invalid include pattern"

        # Search files
        files = cast("dict[str, Any]", state.get(self.state_key, {}))
        results: dict[str, list[tuple[int, str]]] = {}

        for file_path, file_data in files.items():
            if not file_path.startswith(base_path):
                continue

            # Check include filter
            if include:
                basename = Path(file_path).name
                if not _match_include_pattern(basename, include):
                    continue

            # Search file content
            for line_num, line in enumerate(file_data["content"], 1):
                if regex.search(line):
                    if file_path not in results:
                        results[file_path] = []
                    results[file_path].append((line_num, line))

        if not results:
            return "No matches found"

        # Format output based on mode
        return self._format_grep_results(results, output_mode)

Domain

Subdomains

Called By

Frequently Asked Questions

What does _handle_grep_search() do?
_handle_grep_search() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/file_search.py.
Where is _handle_grep_search() defined?
_handle_grep_search() is defined in libs/partners/anthropic/langchain_anthropic/middleware/file_search.py at line 261.
What does _handle_grep_search() call?
_handle_grep_search() calls 3 function(s): _format_grep_results, _is_valid_include_pattern, _match_include_pattern.
What calls _handle_grep_search()?
_handle_grep_search() is called by 1 function(s): __init__.

Analyze Your Own Codebase

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

Try Supermodel Free