A global override to disable API calls to mermaid.ink for graph visualization #30957
Replies: 1 comment 1 reply
-
Hi sundaraa-deshaw, You're absolutely right to be concerned about sending potentially sensitive data to an external server through the mermaid.ink API. I can suggest several approaches to address this issue: 1. Modify the _render_mermaid_using_api functionYou can modify the LangGraph source code directly by navigating to the langchain_core/runnables/graph_mermaid.py file and locating the _render_mermaid_using_api function. There, you can replace the mermaid.ink URL with a local server URL. This approach involves:
2. Set up a local Mermaid rendererYou can use the official mermaid-cli tool to render Mermaid diagrams locally without sending data to an external service. This is available via Docker or can be installed via npm.
3. Create a custom rendering functionSince the issue is happening in the from langgraph.prebuilt.agent import CompiledGraph
from langchain_core.runnables.graph import MermaidDrawMethod
# Create a subclass of CompiledGraph with modified behavior
class SafeCompiledGraph(CompiledGraph):
def _repr_mimebundle_(self, **kwargs):
# Override to use PYPPETEER instead of API for rendering
bundle = super()._repr_mimebundle_(**kwargs)
if "image/png" in bundle:
# Replace with your custom rendering logic
graph = self.get_graph()
mermaid_syntax = graph.draw_mermaid()
# Use your own rendering method here
# ...
return bundle 4. Fix the PYPPETEER method issuesFor the PYPPETEER rendering method, you're encountering issues with event loops, which is a known issue. The error occurs because you're trying to call an async function from an existing event loop. To fix this:
5. Monkey patch the repr_mimebundle methodSince the issue is specifically with the Jupyter integration, you could monkey patch the method: from langgraph.prebuilt import CompiledGraph
from langchain_core.runnables.graph import MermaidDrawMethod
import types
# Store the original method
original_repr = CompiledGraph._repr_mimebundle_
# Create a new method that uses PYPPETEER
def new_repr_mimebundle(self, **kwargs):
# Call original but modify the result
bundle = original_repr(self, **kwargs)
if "image/png" in bundle:
# Replace with locally rendered version
png_bytes = self.get_graph().draw_mermaid_png(
draw_method=MermaidDrawMethod.PYPPETEER
)
bundle["image/png"] = png_bytes
return bundle
# Replace the method
CompiledGraph._repr_mimebundle_ = new_repr_mimebundle This would ensure that all graph renderings use the PYPPETEER method rather than the API, even in Jupyter notebooks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
CompiledGraph.get_graph().draw_mermaid_png()
- also set as the default while rendering Langgraph graph in Jupyter, sends the generated mermaid syntax code tomermaid.ink
server.draw_method
, it is not the honored inCompiledGraph::_repr_mimebundle_
which Jupyter uses to render the graph.draw_method
or the mermaid server URL?draw_png
and notdraw_mermaid_png
in_repr_mimebundle_
?Are there any other ways to avoid sending the data to an external server?
draw_method
toPYPPETEER
per instructions in https://langchain-ai.github.io/langgraph/how-tos/visualization/#using-mermaid-pyppeteer but I get this trace:System Info
Beta Was this translation helpful? Give feedback.
All reactions