5.6 KiB
5.6 KiB
Building ComfyUI Nodes From KIE API Specs
This is the ground-truth process for turning a pinned KIE API spec into:
- a helper (model runner) in
kie_api/ - a ComfyUI node wrapper in
nodes.py - a matching user-facing doc in
web/docs/
Source Material (What We Actually Use)
Primary inputs:
_internal/docs/KIE_API_*_SPEC.mdand other spec files in_internal/docs/_internal/docs/COMFYUI_NODE_CHEATSHEET.md_internal/docs/REPO_RULES.md- Existing node + helper implementations (
nodes.py,kie_api/*.py)
Note: There is no codecs/ folder in this repo. If one appears later, add it to this list.
Non-Negotiable Rules (Do Not Hallucinate)
- Never invent fields or options that are not in a spec.
- If a spec is incomplete or ambiguous, stop and request clarification.
- The helper owns API logic. The node is a thin wrapper only.
- ComfyUI image tensors are BHWC float32 in [0..1].
- Video outputs must remain compatible with ComfyUI
SaveVideo.
Step 1: Parse the Spec (Pin the Facts)
From the spec file, extract these fields exactly:
- Model name (string used in the payload
model) - Input fields (required vs optional)
- Allowed options (enumerations for combos)
- Input constraints (max lengths, valid ranges, max count)
- Response shape (task id, result URLs, failure fields)
If you cannot find a field in the spec, do not add it to the node.
Step 2: Decide Helper vs Node Responsibilities
Helpers (model runners) MUST do:
- prompt validation (length/range)
- image/video upload (if input URLs are required)
- task creation and polling
- result parsing and download/decoding
- logging + credit logging
Nodes MUST do:
- define ComfyUI inputs/outputs
- call exactly one helper function
- return the helper output without modification
Step 3: Map Spec Inputs to ComfyUI Inputs
Use these mapping rules (always prefer existing patterns):
Spec field type -> ComfyUI type:
- string prompt ->
STRING(multiline) - boolean ->
BOOLEAN - number ->
FLOAT(with min/max/step if in spec) - integer ->
INT(with min/max if in spec) - enum ->
COMBOwith the exact options list - image URL(s) ->
IMAGE(ComfyUI tensors), then upload in helper - video URL(s) ->
VIDEO, then upload in helper
Required vs optional:
- Spec required ->
requiredsection ofINPUT_TYPES - Spec optional ->
optionalsection ofINPUT_TYPES
Image and video rules:
- If spec allows 1..N images, accept a batch
IMAGEinput and enforce max N. - If spec allows optional image list, permit
images=Noneand skip upload. - Video inputs are passed through the existing video helpers.
Step 4: Implement the Helper (Model Runner)
Location: kie_api/<model>.py
Required responsibilities:
- Validate inputs
- Upload any images/videos (convert tensors to bytes)
- Create task (
createTask) - Poll until completion (
recordInfo) - Parse result URLs
- Download output and convert to ComfyUI types
Existing helpers to reuse:
kie_api/auth.py(_load_api_key)kie_api/jobs.py(_poll_task_until_complete)kie_api/upload.py(image/video upload + conversions)kie_api/images.py(image download + tensor decode)kie_api/video.py(video download + SaveVideo-compatible output)kie_api/results.py(result URL extraction)kie_api/validation.py(prompt validation)kie_api/log.py(logging)kie_api/credits.py(credits logging)
Helper output contract:
- Image models return
torch.Tensorwith shape [B, H, W, C], float32, 0..1 - Video models return a SaveVideo-compatible object (match existing video nodes)
Step 5: Implement the Node Wrapper
Location: nodes.py
Requirements:
- Keep node
generate()minimal: just call helper and return results. INPUT_TYPES,RETURN_TYPES,RETURN_NAMES,FUNCTION,CATEGORYare required.HELPmust match the actual node inputs and defaults.- Expose enums as
COMBOoptions exactly as listed in the spec. - If spec includes optional inputs, include them under
optional.
Step 6: Register the Node
Ensure:
- The node class is imported in
nodes.py - It appears in
NODE_CLASS_MAPPINGS - It has a name in
NODE_DISPLAY_NAME_MAPPINGS
Step 7: Document the Node (web/docs)
Create or update web/docs/<NodeName>.md:
- Match the exact ComfyUI inputs and outputs
- Include defaults and enums
- Add a brief “Helper behavior” summary:
- validation rules
- polling defaults
- retry behavior (if any)
Helper/Node Consistency Checklist
Before declaring done:
- Node HELP matches INPUT_TYPES (no hidden inputs).
- Helper validates all required fields and enum options.
- Spec-required inputs are not optional in the node.
- All options in the spec are represented as COMBOs.
- Output type is correct (IMAGE or VIDEO).
Spec-to-Node Quick Example (Image-to-Image)
Spec fields:
- model: "flux-2/pro-image-to-image"
- input.input_urls (required, 1..8)
- input.prompt (required, max 5000)
- input.aspect_ratio (required enum)
- input.resolution (required enum)
ComfyUI node inputs:
images: IMAGE(required)prompt: STRING(required)aspect_ratio: COMBO(required)resolution: COMBO(required)log: BOOLEAN(optional, local-only)
Helper responsibilities:
- Validate prompt length
- Validate
imagestensor shape and count (max 8) - Upload images to URLs
- Build payload with
input_urls - Create task + poll
- Download result URL and decode to tensor
When Specs Are Incomplete
If a spec:
- omits an option list
- lacks a max length/range
- conflicts with existing helper behavior
Then:
- Do not guess defaults.
- Add a TODO in the spec doc.
- Ask for clarification or source doc before coding.