Skip to content
LinkedInX

Issues with Mermaid and AI: Five Patterns Where Diagrams Failed to Render Correctly and How I Fixed Them

What This Article Covers

Mermaid is a tool that automatically generates diagrams — flowcharts, sequence diagrams, and others — from plain text descriptions. Because Mermaid notation written in a Markdown file renders as a diagram, it is a convenient way to include visuals in documentation.

AI generates Mermaid notation correctly in many cases, but specific patterns caused diagrams to fail to render as intended. This article records five such patterns and how each was resolved.


Pattern 1: Strings Containing Parentheses or Special Characters Break

Situation

When a node label included parentheses or symbols — such as (optional) or API / SDK — the diagram stopped rendering. Mermaid’s parser interprets parentheses as syntax rather than literal characters.

Fix

Enclose labels in quotation marks. In Mermaid notation, writing a label in the form ["text"] causes the content to be treated as a literal string. When asking AI to generate diagrams, I either explicitly say “use quotation marks when labels contain special characters” or verify and correct this after generation.


Pattern 2: Arrow Direction Between Nodes Is Reversed

Situation

When creating a flowchart to represent a processing sequence, several arrows pointed in the wrong direction. A relationship intended to mean “A calls B” was displayed as “B calls A.”

Why It Happens

AI writes the syntax correctly but can misinterpret the direction of a relationship — which node calls or depends on which — when the description is ambiguous.

Fix

When requesting diagrams, I specify direction explicitly using phrasing like “from A to B.” I then display the generated diagram, identify any reversed arrows, and correct them. The fix is straightforward — changing A --> B to B --> A — so I review after generation and correct as needed.


Pattern 3: Labels That Are Too Long Cause Layout to Break

Situation

When I tried including detailed descriptions in node labels, the diagram spread too wide horizontally and became difficult to read.

Fix

I now keep node labels short and place supplementary explanation in the text outside the diagram. When asking AI to generate a diagram, specifying “keep node labels to around 10 to 15 characters” tends to produce more compact results.


Pattern 4: Japanese Text Causes Display Issues

Situation

In Mermaid diagrams containing Japanese labels, some characters did not display correctly. How Japanese text is handled depends on the Mermaid version and rendering engine in use.

Fix

I first confirm whether Japanese text works in the specific Mermaid version and rendering environment being used. When the problem reproduces, I switch to writing labels in English and placing Japanese explanations outside the diagram. Telling AI “please write labels in English” is the most reliable way to avoid this issue.


Pattern 5: Nested Structures Are Not Interpreted as Intended

Situation

When using subgraphs to group related nodes, the grouping did not match what I intended. Nodes that should have been inside a group appeared outside it, or groups were duplicated.

Why It Happens

Mermaid’s subgraph notation depends on indentation and the placement of end statements. Even when AI generates syntactically valid code, the group boundaries can differ from the intended structure.

Fix

When nested structures are involved, I verify the generated code in a Mermaid online editor or live preview before using it. When problems are found, I correct the position of end statements. For cases requiring complex nesting, I ask AI to generate the structure in stages — starting with a simple skeleton and adding detail incrementally — which makes each step easier to verify.


Summary

Problems with AI-generated Mermaid notation usually stem not from unfamiliarity with the syntax itself, but from specific constraints: special character handling, relationship direction, label length, character encoding differences across environments, and nested group boundaries.

Having a preview step after generation and a set of correction patterns for each issue type reduces the time needed to resolve these problems.