danny-avila/LibreChat · warning · Error
old_text matched ${match.count} locations with ${match.strat
Error message
old_text matched ${match.count} locations with ${match.strategy}; make it unique before retrying. What it means
Thrown by applyTextEdits when findReplacementMatch returns status 'ambiguous': old_text matches more than one location under a given strategy (exact, line-trimmed, whitespace-normalized, or indentation-flexible). The message reports the count and the strategy that hit multiple matches. The tool refuses to guess, requiring the caller to make old_text unambiguous before retrying.
Source
Thrown at packages/api/src/agents/handlers.ts:1106
return whitespaceNormalized;
}
return findLineWindowMatch(content, needle, 'indentation-flexible');
}
function applyTextEdits(
content: string,
edits: TextEdit[],
): { content: string; strategies: string[] } {
let working = content;
const strategies: string[] = [];
for (const edit of edits) {
const match = findReplacementMatch(working, edit.old_text);
if (match.status === 'none') {
throw new Error('old_text did not match the file content.');
}
if (match.status === 'ambiguous') {
throw new Error(
`old_text matched ${match.count} locations with ${match.strategy}; make it unique before retrying.`,
);
}
working =
working.slice(0, match.index) + edit.new_text + working.slice(match.index + match.length);
strategies.push(match.strategy);
}
return { content: working, strategies };
}
function formatRange(start: number, count: number): string {
return count === 1 ? String(start) : `${start},${count}`;
}
function createUnifiedDiff(filePath: string, oldContent: string, newContent: string): string {
if (oldContent === newContent) {
return '';View on GitHub (pinned to 5ff282f900)
Solutions
- Expand old_text to include enough preceding/following unique lines so only one location matches.
- Use the reported strategy hint: if it matched under 'whitespace-normalized', the exact text differs only by whitespace — add distinguishing content lines.
- If you genuinely want to replace all occurrences, issue separate edits targeting each one with unique context.
Example fix
// before (ambiguous: 'return 0;' appears 5 times)
{ old_text: " return 0;", new_text: " return -1;" }
// after (unique via surrounding context)
{ old_text: "function getDefault() {\n return 0;\n}", new_text: "function getDefault() {\n return -1;\n}" } Defensive patterns
Strategy: validation
Validate before calling
function countMatches(content: string, needle: string): number {
let count = 0, from = 0;
while (true) {
const i = content.indexOf(needle, from);
if (i === -1) break;
count++;
from = i + Math.max(1, needle.length);
}
return count;
}
for (const e of edits) {
const n = countMatches(currentContent, e.old_text);
if (n > 1) throw new Error(`old_text is ambiguous (${n} matches); add unique context.`);
} Type guard
function isOldTextUnique(content: string, oldText: string): boolean {
const first = content.indexOf(oldText);
return first !== -1 && content.indexOf(oldText, first + Math.max(1, oldText.length)) === -1;
} Try / catch
try {
const { content, strategies } = applyTextEdits(current.content, edits);
} catch (e) {
if (e instanceof Error && /ambiguous/.test(e.message)) {
// expand old_text with neighboring lines to disambiguate, then retry
} else throw e;
} Prevention
- Include distinctive surrounding lines in old_text so only one location matches.
- Avoid editing repeated boilerplate with a tiny snippet.
- For repeated patterns, target each occurrence separately with unique context or line anchors.
When it happens
Trigger: old_text is a short/repeated snippet (a common import line, a single closing brace, a duplicated function signature) that appears 2+ times; even after the looser normalization strategies the matches stay plural.
Common situations: Editing boilerplate that repeats across a file (log lines, return statements, similar methods); old_text too small to be unique; the file has duplicate blocks and the edit needs disambiguation.
Related errors
AI-assisted analysis of danny-avila/LibreChat@5ff282f900 (2026-08-12).
Data as JSON: /api/errors/31142b5bfc3c22f5.
Report an issue: GitHub.