AI-Powered Diagram Generation
Use ChatGPT, Claude, and Gemini to generate Mermaid diagrams from natural language — prompting techniques, iteration, and fixing common AI errors.
🔄 Lessons 2-5 taught you the syntax for 7+ diagram types. You can write Mermaid code by hand now. But here’s the productivity multiplier: AI can write it for you. This lesson shows you how to prompt AI effectively, iterate on results, and fix the common mistakes AI makes with Mermaid.
What AI Can (and Can’t) Do with Mermaid
AI is excellent at:
- Generating a first draft from a natural language description
- Converting prose documentation into diagrams
- Translating between diagram types (flowchart → sequence diagram)
- Adding detail to a basic outline
AI struggles with:
- Very large diagrams (20+ nodes) — it loses track of connections
- Consistent styling across multiple diagrams
- Complex Gantt chart dependencies
- Avoiding syntax edge cases (smart quotes, reserved keywords)
The winning pattern: AI generates the draft, you review and refine. This is exactly why you learned the syntax first — you need to read and edit what AI produces.
The Effective Mermaid Prompt
A vague prompt produces a vague diagram. Here’s the structure that works:
Create a Mermaid [DIAGRAM TYPE] showing [WHAT IT DEPICTS].
Participants/entities: [LIST THEM]
Key interactions/steps: [DESCRIBE THE FLOW]
Level of detail: [HIGH/MEDIUM/LOW]
Use [any specific conventions — e.g., activation boxes, subgraphs, labels on arrows].
Example: Bad vs. Good Prompt
Bad: “Make a diagram of my login system.”
Good: “Create a Mermaid sequence diagram showing a JWT authentication flow. Participants: React frontend, Express API, PostgreSQL database. Flow: user submits credentials, API validates against database, returns JWT on success or 401 on failure. Include activation boxes and label all arrows.”
The good prompt produces usable code on the first try. The bad prompt requires 3-4 rounds of back-and-forth.
✅ Quick Check: You want AI to create an ER diagram for an e-commerce database. Write a prompt that would produce good results on the first attempt. (Something like: “Create a Mermaid ER diagram for an e-commerce database with these entities: Customer (id, name, email), Product (id, name, price, category), Order (id, date, status, customer_id FK), and OrderItem (quantity, price, order_id FK, product_id FK). Show all relationships with correct cardinality.”)
Platform Comparison: ChatGPT vs Claude vs Gemini
Each AI handles Mermaid differently:
| Feature | ChatGPT | Claude | Gemini |
|---|---|---|---|
| Generates valid Mermaid | Good | Good | Good |
| Renders diagrams inline | No (text only) | Yes (via Artifacts) | No (text only) |
| Handles complex diagrams | Good up to ~15 nodes | Good up to ~15 nodes | Good up to ~15 nodes |
| Syntax accuracy | Occasional smart-quote errors | Generally clean ASCII | Occasional formatting issues |
Claude’s advantage: Artifacts render the diagram visually right in the conversation. You see the result immediately without switching to the Live Editor. This makes the iteration loop faster.
All platforms: Copy the generated code and paste it into the Mermaid Live Editor to verify syntax before using it in documentation.
The Iteration Loop
AI rarely produces a perfect diagram on the first try. Use this loop:
- Generate — Prompt AI with your specific description
- Verify — Paste code into Mermaid Live Editor
- Fix errors — If there are syntax errors, tell AI the exact error message
- Refine — Ask AI to add detail, remove clutter, or restructure
Fixing AI Syntax Errors
The most common AI mistakes with Mermaid:
| Error | Cause | Fix Prompt |
|---|---|---|
| Parse error | Smart quotes " " or em dashes | “Replace all smart quotes with straight quotes and em dashes with regular hyphens” |
| Node collision | Reused node IDs | “Give each node a unique ID — use A1, A2 if needed” |
| Broken rendering | HTML characters in labels | “Remove any < > characters from labels, use plain text” |
| Misaligned arrows | Missing spaces | “Add spaces around all arrow operators” |
Power move: If AI keeps making syntax errors, add this to your prompt: “Use only ASCII characters. No smart quotes, em dashes, or special Unicode characters. Test that every node ID is unique.”
Advanced: Architecture Diagrams as AI Context
Here’s a technique from production teams: feed your Mermaid diagrams back to AI as context for code generation.
If you have an architecture diagram in Mermaid:
flowchart LR
A[React Frontend] --> B[API Gateway]
B --> C[User Service]
B --> D[Order Service]
C --> E[(User DB)]
D --> F[(Order DB)]
You can paste this into any AI assistant and say: “Here’s our architecture. Write the API Gateway routing code that connects to the User Service and Order Service.”
The AI understands the system structure from the diagram and produces more accurate code. Microsoft’s GenAIScript team found this approach significantly reduces AI hallucination about system architecture.
✅ Quick Check: AI generates a sequence diagram but one arrow reads
User—>>API: login. The Mermaid Live Editor shows a parse error. What’s wrong? (The em dash—before>>breaks the parser. Replace with a regular hyphen:User->>API: login. AI models sometimes insert typographic characters that Mermaid can’t handle.)
Prompt Templates for Common Diagrams
Save time with these starter prompts:
System architecture:
“Create a Mermaid flowchart (LR direction) showing the architecture of [system]. Include these components: [list]. Group related components in subgraphs. Label all connections with the protocol or data they carry.”
API flow:
“Create a Mermaid sequence diagram for the [endpoint/flow name] in [system]. Participants: [list]. Show the request path, database queries, and response. Use activation boxes and include error handling with alt/else blocks.”
Database schema:
“Create a Mermaid ER diagram for [domain]. Entities: [list with key attributes]. Show all foreign key relationships with correct cardinality (one-to-many, many-to-many).”
Project timeline:
“Create a Mermaid Gantt chart for [project name]. Phases: [list]. Key tasks per phase: [list]. Show dependencies between phases. Include milestones for [key dates].”
Key Takeaways
- AI generates usable Mermaid code from natural language — but you need specific prompts (diagram type, participants, flow, detail level)
- The iteration loop is Generate → Verify in Live Editor → Fix errors → Refine
- All AI models struggle with very large diagrams — break complex systems into focused sub-diagrams
- Common AI errors: smart quotes, em dashes, reused node IDs, HTML characters — add ASCII-only instructions to your prompts
- Claude renders diagrams inline via Artifacts; ChatGPT and Gemini output text only
- Feed existing Mermaid diagrams back to AI as context for more accurate code generation
Up Next
You can write diagrams by hand and generate them with AI. In Lesson 7, we’ll cover the tools ecosystem — where your diagrams actually live. GitHub rendering, VS Code extensions, Obsidian integration, Notion, and the workflows that keep diagrams in sync with your codebase.
Knowledge Check
Complete the quiz above first
Lesson completed!