sgl-project/sglang · error · FileNotFoundError

Mask file not found: {mask_path}

Error message

Mask file not found: {mask_path}

What it means

In edit mode with an optional mask, generate_image verifies the mask file exists before adding it to the multipart upload, raising FileNotFoundError with the given mask path. It mirrors the image_path check and fires only when a mask is actually supplied.

Source

Thrown at python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/server_api.py:156

        if image_path:
            if not os.path.exists(image_path):
                raise FileNotFoundError(f"Image file not found: {image_path}")

            # Prepare multipart form data for edit
            files: Dict[str, Any] = {}
            data = common_params.copy()

            # Add image file
            files["image"] = (
                os.path.basename(image_path),
                open(image_path, "rb"),
                self._get_content_type(image_path),
            )

            # Add mask file if provided
            if mask_path:
                if not os.path.exists(mask_path):
                    raise FileNotFoundError(f"Mask file not found: {mask_path}")
                files["mask"] = (
                    os.path.basename(mask_path),
                    open(mask_path, "rb"),
                    self._get_content_type(mask_path),
                )

            # Prepare headers for multipart form data
            headers = {
                "Authorization": f"Bearer {self.api_key}",
            }

            try:
                response = requests.post(
                    f"{self.base_url}/images/edits",
                    files=files,
                    data=data,
                    headers=headers,
                    timeout=300,  # 5 minutes timeout for generation

View on GitHub (pinned to 0132848349)

Solutions

  1. Verify or create the mask first; use absolute paths
  2. Skip the mask entirely if you want unmasked editing rather than passing a bad path
  3. Check the mask-generation step's output location and pass that path

Example fix

# before
client.generate_image(prompt, image_path=img, mask_path="mask.png")

# after
mask_path = os.path.abspath("masks/mask.png")
if not os.path.exists(mask_path):
    mask_path = None  # or generate the mask first
client.generate_image(prompt, image_path=img, mask_path=mask_path)
Defensive patterns

Strategy: validation

Validate before calling

import os
if mask_path:
    mask_path = os.path.abspath(mask_path)
    if not os.path.isfile(mask_path):
        mask_path = None  # degrade to unmasked edit, or raise

Type guard

def existing_mask(p: str | None) -> bool:
    return p is None or os.path.isfile(p)

Try / catch

try:
    client.generate_image(prompt, image_path=img, mask_path=mask_path)
except FileNotFoundError as e:
    if "Mask file" in str(e):
        client.generate_image(prompt, image_path=img)  # retry without mask
    else:
        raise

Prevention

When it happens

Trigger: Calling generate_image(..., image_path=valid, mask_path=m) where m doesn't exist — typo, wrong extension, or mask generated later by an external tool that hasn't run yet.

Common situations: Mask produced by an inpainting UI or preprocessing script that failed or wrote to a different location; mismatched .png/.jpg extensions.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/02b6073f031a1026. Report an issue: GitHub.