{"record":{"id":"7430185123e52a04","repo":"calesthio/OpenMontage","slug":"each-element-list-item-must-include-element-id","errorCode":null,"errorMessage":"each element_list item must include element_id","messagePattern":"each element_list item must include element_id","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"tools/_kling/elements.py","lineNumber":32,"sourceCode":"\n\ndef normalize_element_list(element_list: Any | None) -> list[dict[str, int]]:\n    \"\"\"Normalize official Kling element references to element_list objects.\"\"\"\n\n    if not element_list:\n        return []\n    if not isinstance(element_list, list):\n        raise ValueError(\"element_list must be a list of element ids or objects\")\n\n    normalized: list[dict[str, int]] = []\n    for item in element_list:\n        raw_id: Any\n        if isinstance(item, dict):\n            raw_id = item.get(\"element_id\", item.get(\"id\"))\n        else:\n            raw_id = item\n        if raw_id is None:\n            raise ValueError(\"each element_list item must include element_id\")\n        try:\n            element_id = int(raw_id)\n        except (TypeError, ValueError) as exc:\n            raise ValueError(f\"element_id must be an integer-compatible value: {raw_id!r}\") from exc\n        if element_id <= 0:\n            raise ValueError(\"element_id must be positive\")\n        normalized.append({\"element_id\": element_id})\n    return normalized\n\n\ndef element_ids(element_list: Any | None) -> list[int]:\n    \"\"\"Return normalized element ids from an element reference list.\"\"\"\n\n    return [item[\"element_id\"] for item in normalize_element_list(element_list)]\n\n\ndef get_custom_element(element_id: int, client: KlingClient | None = None) -> dict[str, Any]:\n    \"\"\"Fetch one custom element for validation or diagnostics.\"\"\"","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/calesthio/OpenMontage/blob/95e1c3d0ab93482159818560f6a8c8e866b9139f/tools/_kling/elements.py#L14-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Rename the key to element_id (preferred) or id: {'element_id': 123}","Unwrap nested objects before passing: item['element']['id']","Log the failing item to spot the key mismatch immediately"],"exampleFix":"// before\nrefs = normalize_element_list([{'elementId': 42}])\n\n// after\nrefs = normalize_element_list([{'element_id': 42}])","handlingStrategy":"type-guard","validationCode":"def has_element_id(item) -> bool:\n    if isinstance(item, dict):\n        return item.get('element_id') is not None or item.get('id') is not None\n    return True  # bare values are handled by int conversion","typeGuard":"def extract_element_id(item):\n    if isinstance(item, dict):\n        return item.get('element_id', item.get('id'))\n    return item","tryCatchPattern":"try:\n    refs = normalize_element_list(items)\nexcept ValueError as e:\n    if 'element_id' in str(e):\n        bad = [i for i in items if isinstance(i, dict) and i.get('element_id') is None and i.get('id') is None]\n        raise ValueError(f'malformed element dicts (missing element_id/id): {bad}') from e\n    raise","preventionTips":["Use exactly element_id (snake_case) or id as the dict key","Unwrap nested objects ({'element': {'id': 1}}) before passing","Reject empty dicts upstream — they always fail here"],"tags":["kling","elements","validation","input-shape","key-mismatch"],"backgroundTag":null,"analyzedSha":"95e1c3d0ab93482159818560f6a8c8e866b9139f","analyzedAt":"2026-08-15T06:31:20.014Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}