Skip to content

The agent engineering platform.

revert: precompile hex color regex pattern at module level (#35016)

Mason Daugherty9db00d3a6ef4
diff --git a/libs/core/langchain_core/runnables/graph_mermaid.py b/libs/core/langchain_core/runnables/graph_mermaid.py
index 0e50059833..2b8bf2526e 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="")