sgl-project/sglang · error · RuntimeError

Failed to edit image: {str(e)}

Error message

Failed to edit image: {str(e)}

What it means

Raised by SGLDiffusionServerAPI.generate_image when the multipart image-edit POST to the SGLang Diffusion server fails at the HTTP level (connection error, timeout after 300s, or a non-2xx status via raise_for_status). The original requests exception text is embedded in the RuntimeError.

Source

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

                )

            # 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
                )
                response.raise_for_status()
                return response.json()
            except requests.exceptions.RequestException as e:
                raise RuntimeError(f"Failed to edit image: {str(e)}")
            finally:
                # Close file handles
                for file_tuple in files.values():
                    if isinstance(file_tuple, tuple) and len(file_tuple) > 1:
                        file_tuple[1].close()
        else:
            # Use generation endpoint - add generation-specific parameters
            payload = common_params.copy()
            if quality:
                payload["quality"] = quality
            if style:
                payload["style"] = style

            try:
                response = requests.post(
                    f"{self.base_url}/images/generations",
                    json=payload,
                    headers=self.headers,

View on GitHub (pinned to 0132848349)

Solutions

  1. Verify the SGLang Diffusion server is up and base_url is correct (curl the /v1 endpoints).
  2. Check the wrapped message for HTTP status codes: 401/403 means bad api_key, 404 means wrong endpoint/path.
  3. For timeouts, reduce image size/n or raise the client timeout in server_api.py.
  4. Inspect server logs for stack traces during the edit request.

Example fix

// before
resp = api.generate_image(prompt=p, image_paths=["in.png"])  # RuntimeError: Failed to edit image: ...
// after
import requests
try:
    resp = api.generate_image(prompt=p, image_paths=["in.png"])
except RuntimeError as e:
    print("edit failed:", e)  # inspect embedded status code / timeout
Defensive patterns

Strategy: try-catch

Validate before calling

import requests
requests.get(base_url.rstrip('/') + '/models', headers=headers, timeout=5).raise_for_status()

Try / catch

try:
    resp = api.generate_image(prompt=p, image_paths=["in.png"])
except RuntimeError as e:
    msg = str(e)
    if 'timeout' in msg.lower(): retry_or_shrink()
    else: log_and_surface(msg)

Prevention

When it happens

Trigger: Calling generate_image() in edit mode (with input image files) when the server is unreachable, the edit endpoint returns 4xx/5xx, or generation takes longer than the hardcoded 300-second timeout.

Common situations: Server not started or wrong base_url; auth header rejected (401/403); large input images causing slow uploads; heavy edit workload exceeding the 5-minute timeout.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


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