Comfy-Org/ComfyUI · error · RuntimeError

Kling request failed. Code: {response.code}, Message: {respo

Error message

Kling request failed. Code: {response.code}, Message: {response.message}, Data: {response.data}

What it means

Raised in finish_omni_video_task when the Kling omni-video task-creation response carries a non-zero code. Kling's API envelope uses code/message/data; any non-zero code means the initial request (creating the video task) failed before polling began. The exception embeds Kling's own code, message, and data for diagnosis.

Source

Thrown at comfy_api_nodes/nodes_kling.py:244

    """
    if not prompt:
        return prompt

    def _image_repl(match):
        return f"<<<image_{match.group('idx') or '1'}>>>"

    def _video_repl(match):
        return f"<<<video_{match.group('idx') or '1'}>>>"

    # (?<!\w) avoids matching e.g. "test@image.com"
    # (?!\w) makes sure we only match @image / @image<digits> and not @imageFoo
    prompt = re.sub(r"(?<!\w)@image(?P<idx>\d*)(?!\w)", _image_repl, prompt)
    return re.sub(r"(?<!\w)@video(?P<idx>\d*)(?!\w)", _video_repl, prompt)


async def finish_omni_video_task(cls: type[IO.ComfyNode], response: TaskStatusResponse) -> IO.NodeOutput:
    if response.code:
        raise RuntimeError(
            f"Kling request failed. Code: {response.code}, Message: {response.message}, Data: {response.data}"
        )
    final_response = await poll_op(
        cls,
        ApiEndpoint(path=f"/proxy/kling/v1/videos/omni-video/{response.data.task_id}"),
        response_model=TaskStatusResponse,
        status_extractor=lambda r: (r.data.task_status if r.data else None),
    )
    return IO.NodeOutput(await download_url_to_video_output(final_response.data.task_result.videos[0].url))


def is_valid_task_creation_response(response: KlingText2VideoResponse) -> bool:
    """Verifies that the initial response contains a task ID."""
    return bool(response.data.task_id)


def is_valid_video_response(response: KlingText2VideoResponse) -> bool:
    """Verifies that the response contains a task result with at least one video."""

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Read the embedded Kling code/message — e.g. invalid_token means refresh API credentials, insufficient_balance means top up credits
  2. Verify the Kling API key/secret in ComfyUI's API provider settings and re-authenticate
  3. Check Kling credit balance and rate limits on the developer console
  4. Simplify the request (shorter prompt, default params) to isolate a rejected field
Defensive patterns

Strategy: try-catch

Try / catch

try:
    out = await kling_omni_video_node(...)
except RuntimeError as e:
    msg = str(e)
    if "invalid_token" in msg or "unauthorized" in msg.lower():
        refresh_kling_credentials()
    elif "insufficient" in msg.lower():
        raise InsufficientCredits(msg)
    else:
        raise

Prevention

When it happens

Trigger: POST to /proxy/kling/v1/videos/omni-video returning an envelope with a code — invalid API key, insufficient Kling credits, malformed request payload (bad model name, invalid parameters), rate limiting, or unwhitelisted region.

Common situations: Expired or wrong Kling JWT credentials in ComfyUI API settings; depleted Kling credits; parameter combos the omni model rejects; upstream maintenance window.

Related errors


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