/n8n-tutorials

How to fix broken markdown formatting in Claude messages from n8n?

Learn how to fix broken Markdown formatting in Claude messages from n8n with simple steps to ensure clean, reliable outputs.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to fix broken markdown formatting in Claude messages from n8n?

The direct fix is: clean or escape the markdown before sending it to Claude. In n8n the Claude node will break formatting if the incoming text contains unescaped backticks, mismatched triple‑backticks, or malformed line breaks. The safe production pattern is to preprocess the message in a Function node so you output clean, valid markdown, or wrap the entire message in a fenced code block from n8n rather than passing through user‑generated raw text. In practice, you sanitize the text before it reaches the Claude node.

 

Why markdown breaks in Claude when coming from n8n

 

n8n passes data between nodes as plain JSON. If the text comes from a webhook, database, or another API, it can contain unclosed backticks, weird line breaks, or accidental triple‑backticks. Claude is strict about markdown fences, so malformed or nested backticks cause Claude to render everything incorrectly or to prematurely close code blocks.

  • Webhook nodes often receive user‑generated text that is not safe markdown.
  • HTTP Request nodes may bring in text with invisible characters (like \r\n combos) that break block formatting.
  • Set nodes with expressions can accidentally inject backticks if the expression is inside backticks.

 

The reliable production fix: sanitize the text in a Function node

 

The simplest stable solution is to preprocess the text before passing it to Claude. You strip or escape problematic sequences. In n8n this is done with a Function node, which gives you clean control of the exact string being output.

// n8n Function node
// This escapes backticks and normalizes line breaks

const input = $json.text || ""

const cleaned = input
  .replace(/```/g, "ʼʼʼ")      // replace triple-backticks with safe chars
  .replace(/`/g, "ʼ")          // replace single backticks
  .replace(/\r\n/g, "\n")      // normalize line breaks
  .trim()

return [{ text: cleaned }]

 

This produces markdown that Claude will accept safely. Replace characters however you prefer; the key idea is that you remove anything that could prematurely close a markdown code fence.

 

Alternative fix: force stable formatting by wrapping everything in a fenced code block

 

If your use case is “send raw text and let Claude reason about it,” you can wrap the entire input in a controlled triple‑backtick block so Claude never interprets internal formatting.

// Function node wrapping content safely

const t = $json.text || ""

return [{
  text: "```\n" + t + "\n```"
}]

 

Only do this if you want Claude to treat the content as literal text, not mixed instructions and markdown.

 

Practical n8n workflow patterns that avoid markdown breakage

 

  • Always sanitize external text before the Claude node. Never trust user/browser input.
  • Avoid Set node expressions inside backticks. They sometimes inject extra backticks.
  • Never store markdown fences in environment variables that get merged into text.
  • When dynamically building long messages, build them entirely inside a Function node; mixing Set nodes, expressions, and external data increases the risk of broken fences.

 

Root idea

 

Claude only breaks formatting when the incoming string is malformed. The fix is to make sure your workflow produces valid markdown. The safest production strategy in n8n is to run every user‑generated or external text through a Function node that cleans or wraps it before sending it to Claude.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022