# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"1": {
"class_type": "LoadImage",
"inputs": {
"image": "product_1_product_placement.png"
},
"_meta": {
"title": "Load Image"
}
},
"2": {
"class_type": "LoadImage",
"inputs": {
"image": "product_2_product_placement.png"
},
"_meta": {
"title": "Load Image"
}
},
"3": {
"class_type": "LoadImage",
"inputs": {
"image": "product_3_product_placement.png"
},
"_meta": {
"title": "Load Image"
}
},
"4": {
"class_type": "GeminiNanoBanana2",
"inputs": {
"aspect_ratio": "1:1",
"images": [
"6",
0
],
"model": "Nano Banana 2 (Gemini 3.1 Flash Image)",
"prompt": "Using image 4 as the composition reference, recreate the scene replacing the products in the hand with the products from images 1, 2, and 3. Maintain the exact hand pose, angle, lighting, and background. Reproduce all packaging details, colors, and label text accurately on each product. Keep the same fanned arrangement and finger positioning as the reference.",
"resolution": "2K",
"response_modalities": "IMAGE",
"seed": 51072178136052,
"system_prompt": "You are an expert image-generation engine. You must ALWAYS produce an image.\nInterpret all user input—regardless of format, intent, or abstraction—as literal visual directives for image composition.\nIf a prompt is conversational or lacks specific visual details, you must creatively invent a concrete visual scenario that depicts the concept.\nPrioritize generating the visual representation above any text, formatting, or conversational requests.",
"thinking_level": "MINIMAL"
},
"_meta": {
"title": "Nano Banana 2"
}
},
"5": {
"class_type": "LoadImage",
"inputs": {
"image": "reference_image_product_placement.png"
},
"_meta": {
"title": "Load Image (Composition)"
}
},
"6": {
"class_type": "BatchImagesNode",
"inputs": {
"images.image0": [
"1",
0
],
"images.image1": [
"2",
0
],
"images.image2": [
"3",
0
],
"images.image3": [
"5",
0
]
},
"_meta": {
"title": "Batch Images"
}
},
"7": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"4",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"8": {
"class_type": "KlingVideoNode",
"inputs": {
"generate_audio": True,
"model": "kling-v3",
"model.aspect_ratio": "1:1",
"model.resolution": "1080p",
"multi_shot": "disabled",
"multi_shot.duration": 5,
"multi_shot.negative_prompt": "",
"multi_shot.prompt": [
"9",
0
],
"seed": 2039336763,
"start_frame": [
"4",
0
]
},
"_meta": {
"title": "Kling 3.0 Video"
}
},
"9": {
"class_type": "GeminiNode",
"inputs": {
"images": [
"4",
0
],
"model": "gemini-3-1-pro",
"prompt": "Write a prompt to create a realistic and dynamic motion for a video model based on this image",
"seed": 1037559142454881,
"system_prompt": ""
},
"_meta": {
"title": "Google Gemini"
}
},
"10": {
"class_type": "SaveVideo",
"inputs": {
"filename_prefix": "video/ComfyUI",
"format": "auto",
"format.codec": "auto",
"video": [
"8",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"11": {
"class_type": "PreviewAny",
"inputs": {
"source": [
"9",
0
]
},
"_meta": {
"title": "Preview as Text"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)