Home / Function/ _handle_insert() — langchain Function Reference

_handle_insert() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b["_handle_insert()"]
  87c73dc6_9eac_4f37_3263_c6a4d3764b6e["_FilesystemClaudeFileToolMiddleware"]
  e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b -->|defined in| 87c73dc6_9eac_4f37_3263_c6a4d3764b6e
  219744f9_1d86_457c_0923_cb1c51e6d7c4["_handle_insert()"]
  219744f9_1d86_457c_0923_cb1c51e6d7c4 -->|calls| e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b
  32d77b79_4a98_2de5_3e92_7514829faaa3["__init__()"]
  32d77b79_4a98_2de5_3e92_7514829faaa3 -->|calls| e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b
  f268378f_de49_cc43_eb3b_bb6313c21433["_validate_and_resolve_path()"]
  e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b -->|calls| f268378f_de49_cc43_eb3b_bb6313c21433
  219744f9_1d86_457c_0923_cb1c51e6d7c4["_handle_insert()"]
  e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b -->|calls| 219744f9_1d86_457c_0923_cb1c51e6d7c4
  style e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py lines 965–1008

    def _handle_insert(self, args: dict, tool_call_id: str | None) -> Command:
        """Handle insert command."""
        path = args["path"]
        insert_line = args["insert_line"]
        text_to_insert = args["new_str"]

        full_path = self._validate_and_resolve_path(path)

        if not full_path.exists():
            msg = f"File not found: {path}"
            raise FileNotFoundError(msg)

        # Read file
        content = full_path.read_text()
        lines = content.split("\n")
        # Handle trailing newline
        if lines and lines[-1] == "":
            lines = lines[:-1]
            had_trailing_newline = True
        else:
            had_trailing_newline = False

        new_lines = text_to_insert.split("\n")

        # Insert after insert_line (0-indexed)
        updated_lines = lines[:insert_line] + new_lines + lines[insert_line:]

        # Write back
        new_content = "\n".join(updated_lines)
        if had_trailing_newline:
            new_content += "\n"
        full_path.write_text(new_content)

        return Command(
            update={
                "messages": [
                    ToolMessage(
                        content=f"Text inserted in {path}",
                        tool_call_id=tool_call_id,
                        name=self.tool_name,
                    )
                ]
            }
        )

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free