hermes - 💡(How to fix) Fix [Bug]: write_file silently fails + agent loops when model emits unescaped triple-quotes in JSON content [1 pull requests]

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…

Error Message

When a model tries to write Python code containing triple-double-quote docstrings (""") via the write_file tool, the tool call silently fails. No error is shown to the user, and the agent retries with the same broken JSON causing a stuck loop. …the JSON is malformed (inner quotes terminate the string early). The repair function correctly identifies this as unrepairable and falls back to "{}". However, it also discards the path field — so _handle_write_file({}) fires a generic "path missing" error instead of the actionable "content missing" error (which suggests execute_code as an alternative). The model receives a confusing error, retries with the same broken JSON, and loops.

  • The model should receive the "content missing" error (which suggests execute_code) In _repair_tool_call_arguments(), just before the final return "{}" fallback: add a write_file-specific recovery pass that uses a narrow regex to extract the "path" value from the malformed JSON string. File paths are always simple strings (no quoting issues), so a regex like "path"\s*:\s*"([^"]+)" reliably extracts them. Return {"path": "<extracted>"}, which triggers the existing "content missing" error path that already guides the model to use execute_code.

Root Cause

The failure is in _repair_tool_call_arguments() in agent/message_sanitization.py. When the model emits unescaped """ inside the "content" JSON string value:

{"path": "foo.py", "content": "def f():\n    """Docstring."""\n    pass"}

…the JSON is malformed (inner quotes terminate the string early). The repair function correctly identifies this as unrepairable and falls back to "{}". However, it also discards the path field — so _handle_write_file({}) fires a generic "path missing" error instead of the actionable "content missing" error (which suggests execute_code as an alternative). The model receives a confusing error, retries with the same broken JSON, and loops.

Fix Action

Fixed

Code Example

{"path": "foo.py", "content": "def f():\n    """Docstring."""\n    pass"}
RAW_BUFFERClick to expand / collapse

Bug Description

When a model tries to write Python code containing triple-double-quote docstrings (""") via the write_file tool, the tool call silently fails. No error is shown to the user, and the agent retries with the same broken JSON causing a stuck loop.

Root Cause

The failure is in _repair_tool_call_arguments() in agent/message_sanitization.py. When the model emits unescaped """ inside the "content" JSON string value:

{"path": "foo.py", "content": "def f():\n    """Docstring."""\n    pass"}

…the JSON is malformed (inner quotes terminate the string early). The repair function correctly identifies this as unrepairable and falls back to "{}". However, it also discards the path field — so _handle_write_file({}) fires a generic "path missing" error instead of the actionable "content missing" error (which suggests execute_code as an alternative). The model receives a confusing error, retries with the same broken JSON, and loops.

Steps to Reproduce

  1. Ask a model to use write_file to create a .py file containing triple-double-quoted docstrings
  2. The JSON argument string will be malformed by the unescaped """
  3. Agent silently fails — no file written
  4. Agent retries with identical malformed JSON → stuck loop

Expected Behavior

  • The repair function should extract the path field even when content is unrecoverable
  • The model should receive the "content missing" error (which suggests execute_code)
  • The retry loop should be broken

Proposed Fix

In _repair_tool_call_arguments(), just before the final return "{}" fallback: add a write_file-specific recovery pass that uses a narrow regex to extract the "path" value from the malformed JSON string. File paths are always simple strings (no quoting issues), so a regex like "path"\s*:\s*"([^"]+)" reliably extracts them. Return {"path": "<extracted>"}, which triggers the existing "content missing" error path that already guides the model to use execute_code.

A PR with the fix (agent/message_sanitization.py only) is ready to submit.

Related

  • #19096 - write_file silently produces zero-byte files when content arg is missing (symptom-level)
  • #22696 - tool_use XML parser fails when closing tag appears inside JSON string (cousin problem, different code path)

Environment

  • Hermes Agent v0.14.0 (2026.5.16)
  • Any model that emits Python code with docstrings via write_file

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING

hermes - 💡(How to fix) Fix [Bug]: write_file silently fails + agent loops when model emits unescaped triple-quotes in JSON content [1 pull requests]