Comfy-Org/ComfyUI · error · ValueError

image_limit must be greater than or equal to 0 when creating

Error message

image_limit must be greater than or equal to 0 when creating Gemini image parts.

What it means

Raised by create_image_parts() in nodes_gemini.py when the image_limit argument is negative. image_limit == 0 means 'use all images'; positive values clamp the count; negative values are a programming error in the calling node, not user configuration.

Source

Thrown at comfy_api_nodes/nodes_gemini.py:95

      $r := widgets.resolution;
      $isFlash := $contains($m, "nano banana 2");
      $flashPrices := {"1k": 0.0835, "2k": 0.1217, "4k": 0.1848};
      $proPrices := {"1k": 0.1608, "2k": 0.1608, "4k": 0.288};
      $prices := $isFlash ? $flashPrices : $proPrices;
      {"type":"usd","usd": $lookup($prices, $r), "format":{"suffix":"/Image","approximate":true}}
    )
    """,
)


async def create_image_parts(
    cls: type[IO.ComfyNode],
    images: Input.Image | list[Input.Image],
    image_limit: int = 0,
) -> list[GeminiPart]:
    image_parts: list[GeminiPart] = []
    if image_limit < 0:
        raise ValueError("image_limit must be greater than or equal to 0 when creating Gemini image parts.")

    # Accept either a single (possibly-batched) tensor or a list of them; share URL budget across all.
    images_list: list[Input.Image] = images if isinstance(images, list) else [images]
    total_images = sum(get_number_of_images(img) for img in images_list)
    if total_images <= 0:
        raise ValueError("No images provided to create_image_parts; at least one image is required.")

    # If image_limit == 0 --> use all images; otherwise clamp to image_limit.
    effective_max = total_images if image_limit == 0 else min(total_images, image_limit)

    # Number of images we'll send as URLs (fileData)
    num_url_images = min(effective_max, 10)  # Vertex API max number of image links
    upload_kwargs: dict = {"wait_label": "Uploading reference images"}
    if effective_max > num_url_images:
        # Split path (e.g. 11+ images): suppress per-image counter to avoid a confusing dual-fraction label.
        upload_kwargs = {
            "wait_label": f"Uploading reference images ({num_url_images}+)",
            "show_batch_index": False,

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Pass 0 to send all images, or a positive integer to cap the count.
  2. If the limit is computed, clamp it: max(0, computed_limit).
  3. Check the calling node's widget for a negative number and correct it.

Example fix

// before: parts = await create_image_parts(cls, images, image_limit=-1)
// after:  parts = await create_image_parts(cls, images, image_limit=0)  # 0 = use all images
Defensive patterns

Strategy: validation

Validate before calling

def normalize_image_limit(limit: int) -> int:
    if limit < 0:
        raise ValueError("image_limit must be >= 0")
    return limit

# or simply: limit = max(0, limit) if -1 was meant as 'all'

Prevention

When it happens

Trigger: A Gemini node passes image_limit < 0 to create_image_parts — i.e. a widget value or code path supplying a negative limit where only 0 (unlimited) or a positive cap is meaningful.

Common situations: Custom nodes or modified workflows calling create_image_parts with a computed limit that underflows; an upstream numeric widget holding a negative value; copy-paste from code where -1 meant 'all'.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/95847fe8dd5d5254. Report an issue: GitHub.