openclaw - 💡(How to fix) Fix [Bug]: edit tool fails with "Expected ',' or ']'" error when edits array contains multi-line oldText/newText [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#70935Fetched 2026-04-24 10:37:37
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Participants
Timeline (top)
labeled ×1

edit Tool JSON Parameter Encoding Breaks with Multi-Line Text in Nested Arrays

Error Message

  1. The tool call succeeds locally but fails with a JSON parse error when sent to Anthropic's API • Error originates from Anthropic's JSON parser (not a tool execution error) • The error message is misleading — appears to be a file content issue, but is actually a parameter serialization issue Gateway error log shows: error=Expected ',' or ']' after array element in JSON at position 1164 (line 1 column 1165) • Confusion: error message is misleading (appears to be file corruption, not parameter encoding) • Pattern: Same tool (edit), same class of error (JSON parse failures), different underlying cause • Gateway logs show raw_params with truncated JSON in error messages

Root Cause

• Lost productivity: extra steps required (read → modify → write instead of direct edit) • Confusion: error message is misleading (appears to be file corruption, not parameter encoding) • 15+ hour diagnostic lag: errors visible in logs but not caught proactively (from lesson #130, happened 2026-04-23 14:14:54, not caught until 2026-04-24 05:55+) • Repeated mistakes: same issue recurred in this session because the underlying tool bug was never fixed

Fix Action

Fix / Workaround

Severity: Blocks workflow. The edit tool becomes unusable for any real-world file editing task (most files have multi-line content). Users must abandon the tool and use workarounds (write + read cycle).

• Gateway logs: consistent JSON parse errors at Anthropic API boundary (position 1164, 1486, etc.) • Reproduction: every attempt to use edit with newlines fails; simple single-line edits succeed • Workaround effectiveness: switching to write tool resolves the issue immediately (confirmed working)

Workaround effectiveness:

Code Example

Gateway error log shows: error=Expected ',' or ']' after array element in JSON at position 1164 (line 1 column 1165)

The raw_params logged show the edits array parameter is truncated/corrupted in the JSON before reaching the API.

Suggested Fix:

Ensure proper JSON escaping (newlines as \n, quotes as \", etc.) when serializing nested edits array parameters
Or: Use JSON.stringify with proper encoding for complex parameter types
Test with multi-line text in nested array parameters before API calls
RAW_BUFFERClick to expand / collapse

Bug type

Crash (process/app exits or hangs)

Beta release blocker

No

Summary

edit Tool JSON Parameter Encoding Breaks with Multi-Line Text in Nested Arrays

Steps to reproduce

  1. Call the edit tool with an edits array containing multi-line text:

{ "path": "path/to/file.md", "edits": [ { "oldText": "Line 1\nLine 2\nLine 3", "newText": "Replacement text with\nmultiple lines" } ] }

  1. The tool call succeeds locally but fails with a JSON parse error when sent to Anthropic's API

Expected behavior

• The edit tool should successfully replace multi-line text blocks • Files with newlines, special characters, and Unicode should be editable

Actual behavior

• JSON parameter encoding breaks when the edits array contains multi-line strings • Error originates from Anthropic's JSON parser (not a tool execution error) • The error message is misleading — appears to be a file content issue, but is actually a parameter serialization issue

OpenClaw version

v24.14.0

Operating system

Windows 10

Install method

npm global

Model

claud-opus-4.6

Provider / routing chain

openclaw

Additional provider/model setup details

No response

Logs, screenshots, and evidence

Gateway error log shows: error=Expected ',' or ']' after array element in JSON at position 1164 (line 1 column 1165)

The raw_params logged show the edits array parameter is truncated/corrupted in the JSON before reaching the API.

Suggested Fix:

• Ensure proper JSON escaping (newlines as \n, quotes as \", etc.) when serializing nested edits array parameters
• Or: Use JSON.stringify with proper encoding for complex parameter types
• Test with multi-line text in nested array parameters before API calls

Impact and severity

Affected users/systems/channels:

• Any OpenClaw user attempting to use the edit tool with multi-line text in file modifications • Specifically: developers, documentarians, and agents working with markdown, code, or spec files

Severity: Blocks workflow. The edit tool becomes unusable for any real-world file editing task (most files have multi-line content). Users must abandon the tool and use workarounds (write + read cycle).

Frequency: Always — occurs consistently whenever multi-line oldText or newText is provided to the edits array. Not an edge case; it's the normal use case for file editing.

Consequence:

• Lost productivity: extra steps required (read → modify → write instead of direct edit) • Confusion: error message is misleading (appears to be file corruption, not parameter encoding) • 15+ hour diagnostic lag: errors visible in logs but not caught proactively (from lesson #130, happened 2026-04-23 14:14:54, not caught until 2026-04-24 05:55+) • Repeated mistakes: same issue recurred in this session because the underlying tool bug was never fixed

Evidence:

• Gateway logs: consistent JSON parse errors at Anthropic API boundary (position 1164, 1486, etc.) • Reproduction: every attempt to use edit with newlines fails; simple single-line edits succeed • Workaround effectiveness: switching to write tool resolves the issue immediately (confirmed working)

Additional information

Regression history:

• First observed: 2026-04-23 14:14:54 (gateway logs show "Bad Unicode escape in JSON at position 784") • Related to: Lesson #130 documented the same pattern occurring on files with emoji/special characters • Root cause then: PowerShell encoding layer mangling UTF-8 during edit operations • Root cause now: JSON parameter encoding breaking on nested array structures with multi-line text • Pattern: Same tool (edit), same class of error (JSON parse failures), different underlying cause

Related issues/lessons:

• Lesson #130: "JSON corruption errors from edit tool when writing emoji files" (2026-04-23) • This appears to be a broader issue with the edit tool's parameter serialization, not just emoji-related

Affected tool versions:

• Tool: edit (OpenClaw built-in) • Consistent behavior across multiple sessions (2026-04-23 and 2026-04-24) • No version number change observed between sessions, issue persists

Diagnostic evidence:

• Gateway logs show raw_params with truncated JSON in error messages • The truncation point correlates with position of multi-line oldText content • Simple parameter values (no newlines) serialize cleanly; complex nested arrays with newlines fail consistently

Workaround effectiveness:

• write tool (simple string parameters) works reliably • Node.js fs module (handles UTF-8 explicitly) works reliably • Issue is isolated to edit tool's parameter encoding, not to the underlying filesystem or OpenClaw runtime

extent analysis

TL;DR

Proper JSON escaping and serialization of nested edits array parameters are necessary to fix the issue with the edit tool breaking on multi-line text.

Guidance

  • Ensure that newlines are escaped as \n and quotes are escaped as \" when serializing the edits array.
  • Use JSON.stringify with proper encoding for complex parameter types, such as nested arrays with multi-line text.
  • Test the edit tool with multi-line text in nested array parameters before making API calls to verify the fix.
  • Review the gateway logs to check for any JSON parse errors and verify that the raw_params are correctly serialized.

Example

const edits = [
  {
    oldText: "Line 1\nLine 2\nLine 3",
    newText: "Replacement text with\nmultiple lines"
  }
];

const serializedEdits = JSON.stringify(edits);
// Output: "[{\"oldText\":\"Line 1\\nLine 2\\nLine 3\",\"newText\":\"Replacement text with\\nmultiple lines\"}]"

Notes

The issue is specific to the edit tool's parameter encoding and does not affect the underlying filesystem or OpenClaw runtime. The problem is consistent across multiple sessions and tool versions.

Recommendation

Apply a workaround by using JSON.stringify with proper encoding for complex parameter types, such as nested arrays with multi-line text, until a permanent fix is implemented. This will ensure that the edit tool can handle multi-line text correctly and prevent JSON parse errors.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

• The edit tool should successfully replace multi-line text blocks • Files with newlines, special characters, and Unicode should be editable

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING