Skip to content

Commit

Permalink
fix: compress pipeline graphs before sending to mermaid (#8767)
Browse files Browse the repository at this point in the history
* compress graph data to support pako endpoint

* Update haystack/core/pipeline/draw.py

Co-authored-by: David S. Batista <[email protected]>

* Update haystack/core/pipeline/draw.py

Co-authored-by: David S. Batista <[email protected]>

---------

Co-authored-by: David S. Batista <[email protected]>
  • Loading branch information
lbux and davidsbatista authored Jan 28, 2025
1 parent bba84e5 commit d939321
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
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.

0 comments on commit d939321

Please sign in to comment.