Home / Function/ str_replace() — anthropic-sdk-python Function Reference

str_replace() — anthropic-sdk-python Function Reference

Architecture documentation for the str_replace() function in basic.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  d99fe51f_04cf_c035_7ebe_a5ab0468d24f["str_replace()"]
  c2730250_7059_6f44_aa62_71ce88c5a804["LocalFilesystemMemoryTool"]
  d99fe51f_04cf_c035_7ebe_a5ab0468d24f -->|defined in| c2730250_7059_6f44_aa62_71ce88c5a804
  42a84966_de9f_298e_a98d_c527baf8aa0e["_validate_path()"]
  d99fe51f_04cf_c035_7ebe_a5ab0468d24f -->|calls| 42a84966_de9f_298e_a98d_c527baf8aa0e
  style d99fe51f_04cf_c035_7ebe_a5ab0468d24f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

examples/memory/basic.py lines 120–135

    def str_replace(self, command: BetaMemoryTool20250818StrReplaceCommand) -> str:
        full_path = self._validate_path(command.path)

        if not full_path.is_file():
            raise FileNotFoundError(f"File not found: {command.path}")

        content = full_path.read_text(encoding="utf-8")
        count = content.count(command.old_str)
        if count == 0:
            raise ValueError(f"Text not found in {command.path}")
        elif count > 1:
            raise ValueError(f"Text appears {count} times in {command.path}. Must be unique.")

        new_content = content.replace(command.old_str, command.new_str)
        full_path.write_text(new_content, encoding="utf-8")
        return f"File {command.path} has been edited"

Subdomains

Frequently Asked Questions

What does str_replace() do?
str_replace() is a function in the anthropic-sdk-python codebase, defined in examples/memory/basic.py.
Where is str_replace() defined?
str_replace() is defined in examples/memory/basic.py at line 120.
What does str_replace() call?
str_replace() calls 1 function(s): _validate_path.

Analyze Your Own Codebase

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

Try Supermodel Free