From 9db00d3a6ef4b9541697079cb7f91f581eff8827 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Wed, 4 Feb 2026 16:47:52 -0500 Subject: [PATCH] revert: precompile hex color regex pattern at module level (#35016) Reverts langchain-ai/langchain#34480 --- libs/core/langchain_core/runnables/graph_mermaid.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/core/langchain_core/runnables/graph_mermaid.py b/libs/core/langchain_core/runnables/graph_mermaid.py index 0e500598335..2b8bf2526e3 100644 --- a/libs/core/langchain_core/runnables/graph_mermaid.py +++ b/libs/core/langchain_core/runnables/graph_mermaid.py @@ -41,8 +41,6 @@ except ImportError: MARKDOWN_SPECIAL_CHARS = "*_`" -_HEX_COLOR_PATTERN = re.compile(r"^#(?:[0-9a-fA-F]{3}){1,2}$") - def draw_mermaid( nodes: dict[str, Node], @@ -432,8 +430,10 @@ def _render_mermaid_using_api( ) # Check if the background color is a hexadecimal color code using regex - if background_color is not None and not _HEX_COLOR_PATTERN.match(background_color): - background_color = f"!{background_color}" + if background_color is not None: + hex_color_pattern = re.compile(r"^#(?:[0-9a-fA-F]{3}){1,2}$") + if not hex_color_pattern.match(background_color): + background_color = f"!{background_color}" # URL-encode the background_color to handle special characters like '!' encoded_bg_color = urllib.parse.quote(str(background_color), safe="")