Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compress pipeline graphs before sending to mermaid #8767

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions haystack/core/pipeline/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# SPDX-License-Identifier: Apache-2.0

import base64
import json
import zlib

import networkx # type:ignore
import requests
Expand Down Expand Up @@ -68,11 +70,14 @@ def _to_mermaid_image(graph: networkx.MultiDiGraph):
"""
# Copy the graph to avoid modifying the original
graph_styled = _to_mermaid_text(graph.copy())
json_string = json.dumps({"code": graph_styled})

graphbytes = graph_styled.encode("ascii")
base64_bytes = base64.b64encode(graphbytes)
base64_string = base64_bytes.decode("ascii")
url = f"https://mermaid.ink/img/{base64_string}?type=png"
# Uses the DEFLATE algorithm at the highest level for smallest size
compressor = zlib.compressobj(level=9)
compressed_data = compressor.compress(json_string.encode("utf-8")) + compressor.flush()
compressed_url_safe_base64 = base64.urlsafe_b64encode(compressed_data).decode("utf-8").strip()

url = f"https://mermaid.ink/img/pako:{compressed_url_safe_base64}?type=png"

logger.debug("Rendering graph at {url}", url=url)
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Haystack pipelines with Mermaid graphs are now compressed to reduce the size of the encoded base64 and avoid HTTP 400 errors when the graph is too large.
Loading