What's the best way to make llms output correct syntax mermaid and explain to them that there is a limit in the chat so they should not create graphs too large?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To ensure that LLMs (Large Language Models) output correct Mermaid syntax and adhere to size limitations in chat responses, you can use a structured approach with clear instructions. Here’s the best way to do it:
1. Provide Clear Syntax Guidelines
Explicitly instruct the LLM to follow Mermaid syntax rules. Example:
"Generate a Mermaid diagram with correct syntax. Ensure proper indentation, valid node connections, and proper use of graph, flowchart, classDiagram, etc. Avoid syntax errors like missing brackets or incorrect arrow types."
2. Set Size Constraints
Explain the limitations of the chat interface to prevent overly large graphs:
"Keep the Mermaid diagram concise and under [X] lines of code to fit within chat limits. If the graph is too complex, simplify it or split it into smaller sub-diagrams."
3. Use Step-by-Step Instructions
Break down the request to ensure clarity:
Define the diagram type (flowchart, sequence, class, etc.).
Specify key elements (nodes, edges, labels).
Set a line limit (e.g., "Keep it under 20 lines").
Request validation (e.g., "Check for syntax errors before finalizing").
4. Example Prompt
Here’s a well-structured prompt:
"Create a Mermaid flowchart with 5-10 nodes, ensuring correct syntax. Use proper arrow types (--> for solid, -.-> for dashed, etc.). Keep the diagram under 15 lines of code to fit within chat limits. Verify the syntax before finalizing."
5. Post-Generation Review
Ask the LLM to self-check:
"Does this Mermaid diagram follow correct syntax? If not, fix any errors. Is it within the size limit? If not, simplify it."
6. Alternative: Use Code Blocks
If the LLM supports it, request the output in a code block for better readability:
mermaid
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Action 1]
B -->|No| D[Action 2]
7. Handling Large Graphs
If the graph is too big:
Split into sub-diagrams (e.g., "Part 1/2").
Use high-level summaries (e.g., "Here’s a simplified version").
Provide a text description instead of a full diagram.