sgl-project/sglang · error · RuntimeError

Failed to generate image: {str(e)}

Error message

Failed to generate image: {str(e)}

What it means

Raised by SGLDiffusionServerAPI.generate_image when the text-to-image POST to the SGLang Diffusion server raises a requests exception (connection failure, 300s timeout, or non-2xx status). It is the generic HTTP-level failure wrapper for image generation.

Source

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

        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,
                    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 generate image: {str(e)}")

    def generate_video(
        self,
        prompt: str,
        size: Optional[str] = None,
        width: Optional[int] = None,
        height: Optional[int] = None,
        seconds: Optional[int] = 4,
        fps: Optional[int] = None,
        num_frames: Optional[int] = None,
        negative_prompt: Optional[str] = None,
        guidance_scale: Optional[float] = None,
        num_inference_steps: Optional[int] = None,
        seed: Optional[int] = None,
        enable_teacache: bool = False,
        generator_device: Optional[str] = "cuda",
        input_reference: Optional[str] = None,
        output_path: Optional[str] = None,

View on GitHub (pinned to 0132848349)

Solutions

  1. Confirm the server is running and responding (e.g. GET /health or /v1/models on base_url).
  2. Check the embedded error for HTTP status: fix api_key on 401/403, path on 404.
  3. Retry once the model finishes loading; reduce n or image size for long generations.
  4. Increase the 300s timeout in server_api.py for slow hardware.

Example fix

// before
response = sgld_client.generate_image(**request_params)
// after
import requests, time
for attempt in range(3):
    try:
        response = sgld_client.generate_image(**request_params)
        break
    except RuntimeError as e:
        if attempt == 2:
            raise
        time.sleep(5)
Defensive patterns

Strategy: retry

Validate before calling

import requests
assert requests.get(f"{base_url}/models", headers=headers, timeout=5).ok

Try / catch

try:
    response = sgld_client.generate_image(**params)
except RuntimeError as e:
    if 'timed out' in str(e): raise  # not retried blindly
    time.sleep(3); response = sgld_client.generate_image(**params)

Prevention

When it happens

Trigger: Calling generate_image() with no image inputs while the server is down, the endpoint path is wrong, auth fails, or generation exceeds the hardcoded 300-second timeout.

Common situations: Server not launched before running the ComfyUI node; wrong base_url/api_key; long prompts or many images (large n) exceeding timeout; model still loading when the request arrives.

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/b480c8a61a8e2dda. Report an issue: GitHub.