AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Mermaid Rendering Fails When Node Name Contains '[' Or ']' Due To Invalid ID Generation

draw_mermaid_png() returns a 400 error when a LangGraph node name contains square brackets, because _to_safe_id() generates Mermaid-invalid node IDs containing backslashes.

highConfidence 95%Langchain-CoreAffected V1.6.0

Origin Analysis

_to_safe_id() in langchain_core/runnables/graph_mermaid.py maps characters outside [A-Za-z0-9_-] to '\' + hex codepoint. Backslashes are not allowed in Mermaid node IDs, causing the Mermaid API to reject the entire document.
from typing import TypedDict from langchain_core.runnables.graph_mermaid import _to_safe_id from langgraph.graph import END, START, StateGraph class S(TypedDict): x: int def build(node_name: str): g = StateGraph(S) g.add_node(node_name, lambda s: s) g.add_edge(START, node_name) g.add_edge(node_name, END) return g.compile() # 'Plain.before_model' renders fine; 'PIIMiddleware[email].before_model' raises ValueError for name in ['Plain.before_model', 'PIIMiddleware[email].before_model']: print(name, '->', _to_safe_id(name)) try: build(name).get_graph().draw_mermaid_png() print(' draw_mermaid_png: OK') except Exception as e: print(f' draw_mermaid_png: {type(e).__name__}: {str(e).splitlines()[0]}')

Fixing Code Block

Edge Case Audit

The change alters node IDs (adds a hash suffix), which may break any external references to node IDs in cached diagrams, logs, or user scripts. Regenerate diagrams after updating. The hash suffix is deterministic but not collision-proof (though 6 hex chars reduce probability to ~1 in 16 million). Rollback: revert to the previous `_to_safe_id` implementation and restore the original caller code if existing diagrams are essential.

Ecosystem Topology