FoundationAgents/OpenManus · error · ToolError

No replacement was performed. Multiple occurrences of old_st

Error message

No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique

What it means

Thrown by str_replace_editor when old_str appears more than once in the file. Because the tool performs a whole-file string replace, an ambiguous old_str would corrupt every match site, so it refuses to edit and reports the 1-based line numbers where matches were found.

Source

Thrown at app/tool/str_replace_editor.py:311

        file_content = (await operator.read_file(path)).expandtabs()
        old_str = old_str.expandtabs()
        new_str = new_str.expandtabs() if new_str is not None else ""

        # Check if old_str is unique in the file
        occurrences = file_content.count(old_str)
        if occurrences == 0:
            raise ToolError(
                f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}."
            )
        elif occurrences > 1:
            # Find line numbers of occurrences
            file_content_lines = file_content.split("\n")
            lines = [
                idx + 1
                for idx, line in enumerate(file_content_lines)
                if old_str in line
            ]
            raise ToolError(
                f"No replacement was performed. Multiple occurrences of old_str `{old_str}` "
                f"in lines {lines}. Please ensure it is unique"
            )

        # Replace old_str with new_str
        new_file_content = file_content.replace(old_str, new_str)

        # Write the new content to the file
        await operator.write_file(path, new_file_content)

        # Save the original content to history
        self._file_history[path].append(file_content)

        # Create a snippet of the edited section
        replacement_line = file_content.split(old_str)[0].count("\n")
        start_line = max(0, replacement_line - SNIPPET_LINES)
        end_line = replacement_line + SNIPPET_LINES + new_str.count("\n")
        snippet = "\n".join(new_file_content.split("\n")[start_line : end_line + 1])

View on GitHub (pinned to 52a13f2a57)

Solutions

  1. Expand old_str with surrounding lines (function signature, preceding comment) until the snippet is unique in the file.
  2. Use the reported line numbers to pick a distinguishing anchor line directly above or below the target occurrence.
  3. If all occurrences must change identically, do sequential unique replacements or rewrite the whole file via write.
  4. Verify uniqueness first with a search (e.g. count matches) before calling str_replace.

Example fix

// before
str_replace(path, old_str='    return total', new_str='    return round(total, 2)')  // 'return total' appears 3 times
// after
str_replace(path, old_str='def sum_cart(items):\n    total = 0\n    ...\n    return total', new_str='def sum_cart(items):\n    total = 0\n    ...\n    return round(total, 2)')  // whole function is unique
Defensive patterns

Strategy: validation

Validate before calling

content = (await operator.read_file(path)).expandtabs()
count = content.count(old_str.expandtabs())
assert count == 1, f'old_str appears {count} times; expand the snippet to make it unique'

Type guard

def is_unique_snippet(content: str, old_str: str) -> bool:
    return content.expandtabs().count(old_str.expandtabs()) == 1

Try / catch

try:
    await editor.str_replace(path, old_str, new_str)
except ToolError as e:
    if 'Multiple occurrences' in str(e):
        # parse reported lines, grow old_str with neighboring lines, retry
        raise

Prevention

When it happens

Trigger: Passing an old_str that is a short or common snippet (e.g. a single line like 'return None', a closing brace, or an import statement) that occurs on multiple lines; multi-line old_str where one line substring-matches several places. Note: line numbers are computed per line containing old_str, so multi-line matches list every line that contains the string.

Common situations: Editing boilerplate, repeated log statements, repeated closing tags/braces, or methods duplicated across a class. Typical in LLM-agent file editing when the model picks an insufficient context window around the target line.

Related errors


AI-assisted analysis of FoundationAgents/OpenManus@52a13f2a57 (2026-08-15). Data as JSON: /api/errors/72112a1f0e5eb31a. Report an issue: GitHub.