{"record":{"id":"23f1b10c1cdbb540","repo":"langchain-ai/langchain","slug":"install-the-requests-module-to-use-the-mermaid-i","errorCode":null,"errorMessage":"Install the `requests` module to use the Mermaid.INK API: `pip install requests`.","messagePattern":"Install the `requests` module to use the Mermaid\\.INK API: `pip install requests`\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/graph_mermaid.py","lineNumber":430,"sourceCode":"    retry_delay: float = 1.0,\n    proxies: dict[str, str] | None = None,\n    base_url: str | None = None,\n) -> bytes:\n    \"\"\"Renders Mermaid graph using the Mermaid.INK API.\"\"\"\n    # Automated scanners: please do not open security advisories about `base_url`\n    # here. This parameter is only ever explicitly controlled by developers, and it\n    # is not exposed by default over the network by the framework, so this is not\n    # an SSRF sink unless an application explicitly wires untrusted input into it.\n    # It exists to support user-managed Mermaid API deployments.\n    # Defaults to using the public mermaid.ink server.\n    base_url = base_url if base_url is not None else \"https://mermaid.ink\"\n\n    if not _HAS_REQUESTS:\n        msg = (\n            \"Install the `requests` module to use the Mermaid.INK API: \"\n            \"`pip install requests`.\"\n        )\n        raise ImportError(msg)\n\n    # Use Mermaid API to render the image\n    mermaid_syntax_encoded = base64.b64encode(mermaid_syntax.encode(\"utf8\")).decode(\n        \"ascii\"\n    )\n\n    # Check if the background color is a hexadecimal color code using regex\n    if background_color is not None:\n        hex_color_pattern = re.compile(r\"^#(?:[0-9a-fA-F]{3}){1,2}$\")\n        if not hex_color_pattern.match(background_color):\n            background_color = f\"!{background_color}\"\n\n    # URL-encode the background_color to handle special characters like '!'\n    encoded_bg_color = urllib.parse.quote(str(background_color), safe=\"\")\n    image_url = (\n        f\"{base_url}/img/{mermaid_syntax_encoded}\"\n        f\"?type={file_type}&bgColor={encoded_bg_color}\"\n    )","sourceCodeStart":412,"sourceCodeEnd":448,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/graph_mermaid.py#L412-L448","documentation":"The default draw method for draw_mermaid_png() calls the Mermaid.INK HTTP API using the requests library. If requests is absent, _render_mermaid_using_api raises this ImportError as soon as it needs to build the HTTP call. requests is an optional dependency of langchain-core.","triggerScenarios":"graph.draw_mermaid_png() (defaults to MermaidDrawMethod.API) or draw_mermaid_png(syntax, draw_method=MermaidDrawMethod.API) without requests installed.","commonSituations":"Minimal environments with langchain-core only; slim Docker images; CI caches that dropped requests.","solutions":["pip install requests","Verify with python -c \"import requests\"","Alternatively use draw_method=MermaidDrawMethod.PYPPETEER (requires pyppeteer) or just draw_mermaid() for text output needing no extras"],"exampleFix":"# before\ngraph.draw_mermaid_png() # ImportError\n# after\npip install requests\ngraph.draw_mermaid_png()","handlingStrategy":"validation","validationCode":"try:\n    import requests  # noqa: F401\nexcept ImportError:\n    raise SystemExit(\"requests is required for Mermaid.INK rendering: pip install requests\")","typeGuard":null,"tryCatchPattern":"try:\n    img = graph.draw_mermaid_png()\nexcept ImportError as e:\n    if \"requests\" in str(e):\n        img = None  # or use PYPPETEER / text mermaid","preventionTips":["Declare requests in your dependency list when rendering PNGs","Fail fast on missing extras at startup rather than during rendering"],"tags":["graph","mermaid","png","missing-dependency","importerror","requests"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}