Implementation:Langchain ai Langchain Anthropic File Search
| Knowledge Sources | |
|---|---|
| Domains | LLM Framework, Anthropic, Agent Middleware, File Search |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
The file_search.py module provides Glob and Grep search middleware that operates on virtual files stored in LangGraph state, enabling agents to search through files managed by the Anthropic text editor and memory tool middleware.
Description
This module implements StateFileSearchMiddleware, an AgentMiddleware class that adds two search tools to an agent: glob_search (file pattern matching by path) and grep_search (content search using regular expressions). These tools operate on files stored in the AnthropicToolsState virtual file system, making them complementary to the StateClaudeTextEditorMiddleware and StateClaudeMemoryMiddleware. The module also contains internal helper functions for brace-expansion of glob patterns and include-pattern validation.
Usage
Import StateFileSearchMiddleware and include it in the middleware list alongside a state-based text editor or memory middleware. The search tools will automatically have access to the virtual files in state.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/anthropic/langchain_anthropic/middleware/file_search.py - Lines: 1-354
Signature
class StateFileSearchMiddleware(AgentMiddleware):
state_schema = AnthropicToolsState
def __init__(self, *, state_key: str = "text_editor_files") -> None: ...
Import
from langchain_anthropic.middleware.file_search import StateFileSearchMiddleware
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| state_key | str | No | State key to search. Default "text_editor_files". Use "memory_files" to search memory tool files
|
Tool: glob_search Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pattern | str | Yes | The glob pattern to match files against (e.g., **/*.py)
|
| path | str | No | The directory to search in (default: "/")
|
Tool: grep_search Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pattern | str | Yes | Regular expression pattern to search for in file contents |
| path | str | No | The directory to search in (default: "/")
|
| include | str or None | No | File pattern filter (e.g., "*.js", "*.{ts,tsx}")
|
| output_mode | Literal | No | Output format: "files_with_matches" (default), "content", or "count"
|
Outputs
| Name | Type | Description |
|---|---|---|
| result | str | Newline-separated search results. Returns "No files found" or "No matches found" if empty
|
Key Mechanisms
Glob Search
The glob search uses PurePosixPath.match() to match file paths against glob patterns. It handles the **/ prefix specially to also match files in the base directory. Results are sorted by modification time (most recently modified first).
Grep Search
The grep search compiles the pattern as a Python regular expression and searches line-by-line through file contents. It supports three output modes:
files_with_matches: Returns only the file paths containing matches (sorted)content: Returnsfile:line_number:contentformat for each matching linecount: Returnsfile:countformat showing match counts per file
Brace Expansion
The internal _expand_include_patterns function handles brace expansion for include filters (e.g., *.{py,pyi} becomes ["*.py", "*.pyi"]). This enables users to specify multiple file extensions in a single include pattern.
Pattern Validation
Include patterns are validated by _is_valid_include_pattern, which:
- Rejects empty patterns
- Rejects patterns containing null bytes, newlines, or carriage returns
- Verifies brace expansion succeeds
- Confirms each expanded pattern compiles to a valid regex via
fnmatch.translate
Usage Examples
Basic Usage
from langchain.agents import create_agent
from langchain_anthropic.middleware.anthropic_tools import (
StateClaudeTextEditorMiddleware,
)
from langchain_anthropic.middleware.file_search import StateFileSearchMiddleware
agent = create_agent(
model=model,
tools=[],
middleware=[
StateClaudeTextEditorMiddleware(),
StateFileSearchMiddleware(), # Searches text_editor_files by default
],
)
Searching Memory Files
from langchain_anthropic.middleware.file_search import StateFileSearchMiddleware
# Search memory tool files instead of text editor files
search_middleware = StateFileSearchMiddleware(state_key="memory_files")