Mermaids Unbroken

Mermaid diagrams are a popular way to create diagrams in markdown. They are used in many projects, including in GitHub Markdown. Since there are so many examples of mermaid diagrams in the wild,
it is not surprising that LLMs are pretty good at generating them.
Mermaid diagrams in Markdown
Section titled βMermaid diagrams in MarkdownβMermaid supports a number of diagram types and quite a few options to control the appearance of nodes and edges. You can try out different diagrams in the Mermaid playground
Here is a simple example of a flowchart:
```mermaidgraph TD A[Start] --> B{Is it?} B -->|Yes| C[OK] B -->|No| D[Not OK] C --> E[End] D --> E```
This markdown will be rendered as a flowchart in preview mode (and in GitHub!):
Syntax errors
Section titled βSyntax errorsβOne issue with mermaid is thatβ¦ syntax matters and LLM sometimes get it wrong. Letβs introduce a syntax error in the example above.
```mermaidgraph TD A[Start] --> B{Is it?} B ->|Yes| C[OK] B -->|No| D[Not OK] C --> E[End] D --> E```
Now mermaid fails to parse and the diagram is not rendered:
Parse error on line 3:...--> B{Is it?} B ->|Yes| C[OK] B----------------------^Expecting 'SEMI', 'NEWLINE', 'EOF', 'AMP', 'START_LINK', 'LINK', 'LINK_ID', got 'MINUS'
In most cases, the LLM is able to fix the syntax error and generate a valid diagram using the error message.
Automatic repair
Section titled βAutomatic repairβWe have added a βrepairerβ in system.diagrams system prompt.
The repairer looks for mermaid
code blocks in the output and tries to parse them.
If it the diagram has parse errors, the repairer adds a message to the chat to fix those.
Here is a trace of a simple script that generates a diagram from any code.
def("CODE", env.files)$`Generate a class diagram using mermaid of the code symbols in the CODE.`
We run the cli with the mermaid.genai.mjs
script and the runpromptcontext.ts file.
genaiscript run mermaid packages/core/src/runpromptcontext.ts
First attempt
Section titled βFirst attemptβThe script generates a prompt with the code and the instructions to generate a diagram.
ββπ¬ github:gpt-4.1 β 2 ~β9.2ktββπ systemβ## Safety: Jailbreakβ... (18 lines)βUse clear, concise node and relationship labels.βImplement appropriate styling and colors to enhance readability.ββπ€ userβ<CODE lang="ts" file="packages/core/src/runpromptcontext.ts">βimport debug from "debug"βconst dbg = debug("genaiscript:prompt:context")β// cspell: disableβimport {β PromptNode,β... (1202 lines)β env,β })β return ctxβ}β</CODE>βGenerate a class diagram using mermaid of the code symbols in the CODE.
```mermaidclassDiagram %% Main context classes and interfaces class ChatTurnGenerationContext { <<interface>> +node: PromptNode... %% Highlights to show this is a function producing a context with many callable methods. class createChatGenerationContext,createChatTurnGenerationContext highlightFunction; classDef highlightFunction fill:#f5f,stroke:#333,stroke-width:1.5px;```This diagram summarizes the main types, classes, and their relationships from `runpromptcontext.ts`. The context factories (`createChatGenerationContext`, `createChatTurnGenerationContext`) produce context "objects" rich in methods. Some utilities and dependencies are shown for structure clarity.
ββπ github:gpt-4.1 β 2 7418ms β
9.7kt β8.8kt β892t 2.47Β’
Syntax error detected, repairer activated
Section titled βSyntax error detected, repairer activatedβThe mermaid diagrem generated has a syntax error.
Parse error on line 107:...hatGenerationContext,createChatTurnGener-----------------------^Expecting 'NEWLINE', 'EOF', 'SQS', 'STR', 'DOT', 'GENERICTYPE', 'LABEL', 'STRUCT_START', 'STRUCT_STOP', 'STYLE_SEPARATOR', 'ANNOTATION_END', 'ALPHA', 'AGGREGATION', 'EXTENSION', 'COMPOSITION', 'DEPEβ¦
The repairer enters the game and responds to the assistant with a message to fix the error. The entire chat conversation is sent back to the LLM , including the error message.
ββπ¬ github:gpt-4.1 β 4 ~β10.5ktββπ systemβ...ββπ€ userβ...βGenerate a class diagram using mermaid of the code symbols in the CODE.ββπ€ assistantβ```mermaidβ...β```ββπ€ userβI found syntax errors in the mermaid diagram. Please repair the parse error:βParse error on line 107:β...hatGenerationContext,createChatTurnGenerβ-----------------------^βExpecting 'NEWLINE', 'EOF', 'SQS', 'STR', 'DOT', 'GENERICTYPE', 'LABEL', 'STRUCT_START', 'STRUCT_STOP', 'STYLE_SEPARATOR', 'ANNOTATION_END', 'ALPHA', 'AGGREGATION', 'EXTENSION', 'COMPOSITION', 'DEPEβ¦
The LLM repairs the diagram
Section titled βThe LLM repairs the diagramβIn this case, the LLM is able to repair the diagram and generate a valid mermaid diagram.
```mermaidclassDiagram %% Core Context Interfaces and Classes class ChatTurnGenerationContext { <<interface>>... %% Factory highlighting (removed previous classDef/annotations for compatibility)```This diagram removes invalid classDef and annotation syntax and corrects relationship/arrows for Mermaid compatibility.
ββπ github:gpt-4.1 β 4 3741ms β
10.3kt β9.9kt β457t 2.34Β’
The repaired diagram
Section titled βThe repaired diagramβFinally, the repaired diagram is returned to the user:
Can I parse mermaid diagrams myselft?
Section titled βCan I parse mermaid diagrams myselft?βYes, you can use parsers.mermaid
to parse mermaid diagrams in your scripts
programmatically.