calesthio/OpenMontage · error · ValueError

each element_list item must include element_id

Error message

each element_list item must include element_id

What it means

Raised while iterating element_list items: an item is a dict but contains neither an 'element_id' nor an 'id' key, so its reference id cannot be determined. Non-dict items fall through to direct int conversion and fail with a different message, so this error specifically means a malformed dict entry.

Source

Thrown at tools/_kling/elements.py:32


def normalize_element_list(element_list: Any | None) -> list[dict[str, int]]:
    """Normalize official Kling element references to element_list objects."""

    if not element_list:
        return []
    if not isinstance(element_list, list):
        raise ValueError("element_list must be a list of element ids or objects")

    normalized: list[dict[str, int]] = []
    for item in element_list:
        raw_id: Any
        if isinstance(item, dict):
            raw_id = item.get("element_id", item.get("id"))
        else:
            raw_id = item
        if raw_id is None:
            raise ValueError("each element_list item must include element_id")
        try:
            element_id = int(raw_id)
        except (TypeError, ValueError) as exc:
            raise ValueError(f"element_id must be an integer-compatible value: {raw_id!r}") from exc
        if element_id <= 0:
            raise ValueError("element_id must be positive")
        normalized.append({"element_id": element_id})
    return normalized


def element_ids(element_list: Any | None) -> list[int]:
    """Return normalized element ids from an element reference list."""

    return [item["element_id"] for item in normalize_element_list(element_list)]


def get_custom_element(element_id: int, client: KlingClient | None = None) -> dict[str, Any]:
    """Fetch one custom element for validation or diagnostics."""

View on GitHub (pinned to 95e1c3d0ab)

Solutions

  1. Rename the key to element_id (preferred) or id: {'element_id': 123}
  2. Unwrap nested objects before passing: item['element']['id']
  3. Log the failing item to spot the key mismatch immediately

Example fix

// before
refs = normalize_element_list([{'elementId': 42}])

// after
refs = normalize_element_list([{'element_id': 42}])
Defensive patterns

Strategy: type-guard

Validate before calling

def has_element_id(item) -> bool:
    if isinstance(item, dict):
        return item.get('element_id') is not None or item.get('id') is not None
    return True  # bare values are handled by int conversion

Type guard

def extract_element_id(item):
    if isinstance(item, dict):
        return item.get('element_id', item.get('id'))
    return item

Try / catch

try:
    refs = normalize_element_list(items)
except ValueError as e:
    if 'element_id' in str(e):
        bad = [i for i in items if isinstance(i, dict) and i.get('element_id') is None and i.get('id') is None]
        raise ValueError(f'malformed element dicts (missing element_id/id): {bad}') from e
    raise

Prevention

When it happens

Trigger: Passing dicts that use a different key, e.g. {'elementId': 1} (camelCase) or {'uuid': '...'}; passing wrapper objects like {'element': {'id': 1}} where the id is nested; empty dict {} in the list.

Common situations: Forwarding raw API response objects from another endpoint whose schema differs (camelCase vs snake_case); copy-pasted examples from different Kling docs versions; LLM-generated argument dicts with guessed key names.

Related errors


AI-assisted analysis of calesthio/OpenMontage@95e1c3d0ab (2026-08-15). Data as JSON: /api/errors/7430185123e52a04. Report an issue: GitHub.