deepset-ai/haystack · error · ValueError

Fit must be a boolean.

Error message

Fit must be a boolean.

What it means

For PDF output, the mermaid 'fit' option must be a boolean. Haystack validates this before requesting the PDF from mermaid.ink.

Source

Thrown at haystack/core/pipeline/draw.py:163

        raise ValueError(f"Invalid theme: {params['theme']}. Valid options are: {valid_themes}.")

    if "width" in params and not isinstance(params["width"], int):
        raise ValueError("Width must be an integer.")
    if "height" in params and not isinstance(params["height"], int):
        raise ValueError("Height must be an integer.")

    if "scale" in params and not 1 <= params["scale"] <= 3:
        raise ValueError("Scale must be a number between 1 and 3.")
    if "scale" in params and not ("width" in params or "height" in params):
        raise ValueError("Scale is only allowed when width or height is set.")

    if "bgColor" in params and not isinstance(params["bgColor"], str):
        raise ValueError("Background color must be a string.")

    # PDF specific parameters
    if params["format"] == "pdf":
        if "fit" in params and not isinstance(params["fit"], bool):
            raise ValueError("Fit must be a boolean.")
        if "paper" in params and not isinstance(params["paper"], str):
            raise ValueError("Paper size must be a string (e.g., 'a4', 'a3').")
        if "landscape" in params and not isinstance(params["landscape"], bool):
            raise ValueError("Landscape must be a boolean.")
        if "fit" in params and ("paper" in params or "landscape" in params):
            logger.warning("`fit` overrides `paper` and `landscape` for PDFs. Ignoring `paper` and `landscape`.")


# Magic-byte signatures used to verify a Mermaid server response matches the requested output format.
_PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
_JPEG_SIGNATURE = b"\xff\xd8\xff"
_PDF_SIGNATURE = b"%PDF-"
_RIFF_SIGNATURE = b"RIFF"
_WEBP_SIGNATURE = b"WEBP"
_SVG_PREFIXES = (b"<?xml", b"<svg")


def _validate_image_response(resp: httpx.Response, params: dict[str, Any]) -> None:

View on GitHub (pinned to e318778c9b)

Solutions

  1. Pass a real boolean: params['fit'] = True
  2. Convert strings: params['fit'] = str(params['fit']).lower() == 'true'
  3. Remove 'fit' and rely on paper/landscape sizing

Example fix

// before
pipeline.draw(path="g.pdf", params={"format": "pdf", "fit": "true"})  # ValueError
// after
pipeline.draw(path="g.pdf", params={"format": "pdf", "fit": True})
Defensive patterns

Strategy: type-guard

Validate before calling

if "fit" in params and not isinstance(params["fit"], bool):
    params["fit"] = bool(params["fit"])

Type guard

def is_bool_fit(params: dict) -> bool:
    return "fit" not in params or isinstance(params["fit"], bool)

Try / catch

try:
    pipeline.draw(path, params=params)
except ValueError as e:
    if "Fit must be a boolean" in str(e):
        params["fit"] = params["fit"] in ("true", 1, True)
        pipeline.draw(path, params=params)

Prevention

When it happens

Trigger: params={'format': 'pdf', 'fit': 'true'} or {'fit': 1} when drawing a pipeline as PDF.

Common situations: Reading fit from CLI flags/env vars where it arrives as a string; JSON config with string booleans.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/cde9f3fa59b6108e. Report an issue: GitHub.