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
  219744f9_1d86_457c_0923_cb1c51e6d7c4["_handle_insert()"]
  cc57e604_ebeb_0d7e_259e_ae26b40e03fa["_StateClaudeFileToolMiddleware"]
  219744f9_1d86_457c_0923_cb1c51e6d7c4 -->|defined in| cc57e604_ebeb_0d7e_259e_ae26b40e03fa
  e9dbd5b2_27e4_e878_3cde_25abd3acf9b7["__init__()"]
  e9dbd5b2_27e4_e878_3cde_25abd3acf9b7 -->|calls| 219744f9_1d86_457c_0923_cb1c51e6d7c4
  e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b["_handle_insert()"]
  e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b -->|calls| 219744f9_1d86_457c_0923_cb1c51e6d7c4
  e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b["_handle_insert()"]
  219744f9_1d86_457c_0923_cb1c51e6d7c4 -->|calls| e26ad50c_f58e_2a8a_dcf4_f2fc823c4f6b
  c3b02d97_e5fe_b3c4_bc92_7f2655652ed9["_validate_path()"]
  219744f9_1d86_457c_0923_cb1c51e6d7c4 -->|calls| c3b02d97_e5fe_b3c4_bc92_7f2655652ed9
  style 219744f9_1d86_457c_0923_cb1c51e6d7c4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py lines 458–503

    def _handle_insert(
        self, args: dict, state: AnthropicToolsState, tool_call_id: str | None
    ) -> Command:
        """Handle insert command."""
        path = args["path"]
        insert_line = args["insert_line"]
        text_to_insert = args["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"]
        new_lines = text_to_insert.split("\n")

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

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

        return Command(
            update={
                self.state_key: {
                    normalized_path: {
                        "content": updated_lines,
                        "created_at": file_data["created_at"],
                        "modified_at": now,
                    }
                },
                "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 458.
What does _handle_insert() call?
_handle_insert() calls 2 function(s): _handle_insert, _validate_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