Home / Function/ _handle_str_replace() — langchain Function Reference

_handle_str_replace() — langchain Function Reference

Architecture documentation for the _handle_str_replace() function in anthropic_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  d5be6f58_fb1c_3813_50f1_02703977d3ba["_handle_str_replace()"]
  cc57e604_ebeb_0d7e_259e_ae26b40e03fa["_StateClaudeFileToolMiddleware"]
  d5be6f58_fb1c_3813_50f1_02703977d3ba -->|defined in| cc57e604_ebeb_0d7e_259e_ae26b40e03fa
  e9dbd5b2_27e4_e878_3cde_25abd3acf9b7["__init__()"]
  e9dbd5b2_27e4_e878_3cde_25abd3acf9b7 -->|calls| d5be6f58_fb1c_3813_50f1_02703977d3ba
  ca64d5cb_384c_c03b_d650_455c3ed4afca["_handle_str_replace()"]
  ca64d5cb_384c_c03b_d650_455c3ed4afca -->|calls| d5be6f58_fb1c_3813_50f1_02703977d3ba
  ca64d5cb_384c_c03b_d650_455c3ed4afca["_handle_str_replace()"]
  d5be6f58_fb1c_3813_50f1_02703977d3ba -->|calls| ca64d5cb_384c_c03b_d650_455c3ed4afca
  c3b02d97_e5fe_b3c4_bc92_7f2655652ed9["_validate_path()"]
  d5be6f58_fb1c_3813_50f1_02703977d3ba -->|calls| c3b02d97_e5fe_b3c4_bc92_7f2655652ed9
  style d5be6f58_fb1c_3813_50f1_02703977d3ba fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py lines 408–456

    def _handle_str_replace(
        self, args: dict, state: AnthropicToolsState, tool_call_id: str | None
    ) -> Command:
        """Handle str_replace command."""
        path = args["path"]
        old_str = args["old_str"]
        new_str = args.get("new_str", "")

        normalized_path = _validate_path(path, allowed_prefixes=self.allowed_prefixes)

        # Read file
        files = cast("dict[str, Any]", state.get(self.state_key, {}))
        file_data = files.get(normalized_path)
        if file_data is None:
            msg = f"File not found: {path}"
            raise FileNotFoundError(msg)

        lines_content = file_data["content"]
        content = "\n".join(lines_content)

        # Replace string
        if old_str not in content:
            msg = f"String not found in file: {old_str}"
            raise ValueError(msg)

        new_content = content.replace(old_str, new_str, 1)
        new_lines = new_content.split("\n")

        # Update file
        now = datetime.now(timezone.utc).isoformat()

        return Command(
            update={
                self.state_key: {
                    normalized_path: {
                        "content": new_lines,
                        "created_at": file_data["created_at"],
                        "modified_at": now,
                    }
                },
                "messages": [
                    ToolMessage(
                        content=f"String replaced in {path}",
                        tool_call_id=tool_call_id,
                        name=self.tool_name,
                    )
                ],
            }
        )

Domain

Subdomains

Frequently Asked Questions

What does _handle_str_replace() do?
_handle_str_replace() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py.
Where is _handle_str_replace() defined?
_handle_str_replace() is defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py at line 408.
What does _handle_str_replace() call?
_handle_str_replace() calls 2 function(s): _handle_str_replace, _validate_path.
What calls _handle_str_replace()?
_handle_str_replace() is called by 2 function(s): __init__, _handle_str_replace.

Analyze Your Own Codebase

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

Try Supermodel Free