{"record":{"id":"0b2d4dd0a0a9077e","repo":"Comfy-Org/ComfyUI","slug":"too-many-references-len-media-the-maximum-to","errorCode":null,"errorMessage":"Too many references ({len(media)}). The maximum total of reference videos and images is 5.","messagePattern":"Too many references \\((.+?)\\)\\. The maximum total of reference videos and images is 5\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy_api_nodes/nodes_wan.py","lineNumber":1612,"sourceCode":"        validate_string(model[\"prompt\"], strip_whitespace=False, min_length=1)\n        media = []\n        reference_videos = model.get(\"reference_videos\", {})\n        for key in reference_videos:\n            media.append(\n                Wan27MediaItem(type=\"reference_video\", url=await upload_video_to_comfyapi(cls, reference_videos[key]))\n            )\n        reference_images = model.get(\"reference_images\", {})\n        for key in reference_images:\n            media.append(\n                Wan27MediaItem(\n                    type=\"reference_image\",\n                    url=await upload_image_to_comfyapi(cls, image=reference_images[key]),\n                )\n            )\n        if not media:\n            raise ValueError(\"At least one reference video or reference image must be provided.\")\n        if len(media) > 5:\n            raise ValueError(\n                f\"Too many references ({len(media)}). The maximum total of reference videos and images is 5.\"\n            )\n\n        initial_response = await sync_op(\n            cls,\n            ApiEndpoint(\n                path=\"/proxy/wan/api/v1/services/aigc/video-generation/video-synthesis\",\n                method=\"POST\",\n            ),\n            response_model=TaskCreationResponse,\n            data=Wan27ReferenceVideoTaskCreationRequest(\n                model=model[\"model\"],\n                input=Wan27ReferenceVideoInputField(\n                    prompt=model[\"prompt\"],\n                    negative_prompt=model[\"negative_prompt\"] or None,\n                    media=media,\n                ),\n                parameters=Wan27ReferenceVideoParametersField(","sourceCodeStart":1594,"sourceCodeEnd":1630,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_api_nodes/nodes_wan.py#L1594-L1630","documentation":"Raised by Wan2ReferenceVideoApi.execute when the combined count of reference videos and images exceeds 5. The node counts entries appended to `media` from both the reference_videos and reference_images maps, then enforces the Wan 2.7 API limit of 5 total reference assets.","triggerScenarios":"reference_videos plus reference_images entries total 6 or more in the model dict; e.g. 3 videos and 3 images. The check runs after uploads, so the failure surfaces late.","commonSituations":"Autogrow/batch reference inputs expanded past the limit; merging multiple reference workflows together; assuming the limit was per-type rather than total.","solutions":["Reduce total references (videos + images) to 5 or fewer","Pick the highest-quality references rather than uploading many and failing","Prefer trimming the list before the node runs so uploads are not wasted (the check currently happens after upload)"],"exampleFix":"// before\nrefs = {**videos, **images}  # 7 entries -> ValueError after upload\n// after\nrefs = dict(list({**videos, **images}.items())[:5])","handlingStrategy":"validation","validationCode":"total = len(reference_videos) + len(reference_images)\nif total > 5:\n    reference_videos = dict(list(reference_videos.items())[: max(0, 5 - len(reference_images))])\n    # or simply reject:\n    # raise ValueError(f\"{total} references exceeds the limit of 5\")","typeGuard":"def within_reference_limit(videos: dict, images: dict) -> bool:\n    return len(videos) + len(images) <= 5","tryCatchPattern":null,"preventionTips":["Count videos+images together — the limit is on the total","Trim the reference list before running so uploads are not wasted"],"tags":["wan","api-node","validation","reference-video","input-limits"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}