ComfyUI API Client
A Python client library for interacting with ComfyUI API. This library provides a convenient wrapper to queue workflows, upload images/masks, and retrieve generated results (images, videos, audio, etc.) programmatically.
Features
- Workflow Management: Queue workflows easily and wait for results.
- File Management: Upload input images and masks directly to ComfyUI.
- Generic Output Handling: Automatically handles various output types (Images, Videos/GIFs, Audio).
- Execution Control: Interrupt running tasks, check queue status, and view history.
- Node Info: Retrieve definitions for ComfyUI nodes.
Installation
pip install comfyui_xy
How to Get Workflow JSON
To use this API, you need the workflow in API Format, which is different from the standard JSON saved by ComfyUI.
-
Enable Dev Mode Options:
- In ComfyUI web interface, click the Settings (gear icon) in the menu.
- Check the option "Enable Dev mode Options".
-
Save as API Format:
- Once Dev Mode is enabled, you will see a new button in the menu: "Save (API Format)".
- Click this button to save your workflow as a JSON file (e.g.,
workflow_api.json). - Use this JSON file with the
ComfyUiClient.
Quick Start
import json
from comfyui_xy import ComfyUiClient
# 1. Initialize Client
client = ComfyUiClient(url="http://127.0.0.1:8188")
# 2. Load Workflow
# You should export the workflow in "API Format" from ComfyUI
with open("workflow_api.json", "r", encoding="utf-8") as f:
workflow = json.load(f)
# 3. Modify Parameters (Optional)
# e.g., Change the seed in KSampler (Node ID "3")
import random
workflow["3"]["inputs"]["seed"] = random.randint(1, 1000000000)
# 4. Run Workflow
print("Queueing workflow...")
responses = client.process_workflow(workflow)
# 5. Handle Unified Responses
for i, response in enumerate(responses):
if response.is_file:
print(f"Received file: {response.filename} ({response.file_type})")
response.save(f"output_{i}_{response.filename}")
if response.file_type == "image":
response.show()
elif response.is_text:
print(f"Received text from node {response.node_id}: {response.text}")
Detailed Usage
1. Initialization
from comfyui_xy import ComfyUiClient
# Default local server (http://127.0.0.1:8188)
client = ComfyUiClient()
# Specify URL
client = ComfyUiClient(url="http://127.0.0.1:8188")
# Remote server with HTTPS
client = ComfyUiClient(url="https://my-comfyui-server.com:8188")
# Recommended for production scripts: explicit request timeout and strict errors
client = ComfyUiClient(timeout=30, execution_timeout=300, raise_on_error=True)
2. Uploading Files
You can upload images or masks before running a workflow. These files are saved in the input directory of ComfyUI.
# Upload an image
# Returns the filename used by ComfyUI (useful for setting node inputs)
image_name = client.upload_image("path/to/my_image.png")
# Upload a mask
mask_name = client.upload_mask("path/to/my_mask.png")
# Example: Set the uploaded image in a LoadImage node (e.g., Node ID "10")
workflow["10"]["inputs"]["image"] = image_name
3. Processing Workflows
The process_workflow method is a high-level helper that:
- Queues the prompt.
- Waits for execution to finish.
- Returns unified response objects for downloaded files and discovered text outputs.
responses = client.process_workflow(workflow)
Return Value:
It returns a list of ComfyResponse objects. Each response has output_type
set to either "file" or "text".
If your workflow produces text instead of files, return the raw ComfyUI
outputs data and extract the strings directly:
outputs = client.process_workflow(workflow, return_raw=True)
texts = client.extract_text_outputs(outputs)
print(outputs)
print(texts)
If you want explicit response objects from raw outputs, use:
file_responses = client.extract_file_responses(outputs)
text_responses = client.extract_text_responses(outputs)
responses = client.extract_outputs(outputs)
If you want failures to be explicit instead of silently returning None / {} / [],
initialize the client with raise_on_error=True. Request failures raise
ComfyUiError, ComfyUiHttpError, or ComfyUiTimeoutError.
4. Handling Responses (ComfyResponse)
The ComfyResponse object wraps the raw data returned by ComfyUI.
-
Attributes:
output_type:"file"or"text".data: Raw bytes of the file for file responses.text: Extracted text for text responses.filename: Original filename on server for file responses.node_id: Source node ID in ComfyUI outputs.output_name: Output field name such asimagesortext.file_type: Type of file (e.g.,'image','video','audio','text').image: APIL.Imageobject (if the response is a valid image file).
-
Methods:
save(path=None): Save file bytes or text content to disk.show(): Opens the image in the default viewer (only for images).
5. Advanced Controls
Interrupt Execution:
client.interrupt()
Get System Status:
# Get Queue Status (Pending/Running tasks)
queue_info = client.get_queue()
# Get Full History
history = client.get_history_all()
# Get Node Information
node_info = client.get_object_info("KSampler")
Async Support
You can use AsyncComfyUiClient for asynchronous operations using aiohttp.
import asyncio
from comfyui_xy import AsyncComfyUiClient
async def main():
async with AsyncComfyUiClient(url="http://127.0.0.1:8188") as client:
# Most methods are awaitable
results = await client.process_workflow(workflow)
if __name__ == "__main__":
asyncio.run(main())
Examples
Check the examples/ directory for more complete scripts:
basic_workflow.py: Simple Text-to-Image generation.image_to_image.py: Uploading an image and processing it.advanced_control.py: Inspecting queue and system info.
Changelog
0.3.0
- Added unified
ComfyResponseoutputs.process_workflow()now returns both file and text responses by default. - Kept
return_raw=Truesupport for callers that need the original ComfyUIoutputsstructure. - Added
extract_file_responses(),extract_text_responses(),extract_outputs(), andextract_text_outputs()helpers for explicit post-processing. - Added request timeout, workflow execution timeout, and strict error mode via
raise_on_error=True. - Added
ComfyUiError,ComfyUiHttpError, andComfyUiTimeoutError. - Added sync and async test coverage for raw outputs, unified responses, HTTP errors, and timeouts.
Compatibility note:
- Older code that assumed every item returned by
process_workflow()was a file should now checkresponse.is_filebefore calling file-specific logic.
License
MIT
Metadata
Release files for comfyui-xy 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| comfyui_xy-0.3.0.tar.gz | 15.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| comfyui_xy-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.5 kB
Release files / comfyui_xy-0.3.0.tar.gz
| Download URL | comfyui_xy-0.3.0.tar.gz |
|---|---|
| Size | 15.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d13d992daa9d03569f8fb7226721621bbff8362c9fc6cb12c1064ae289ea9a4f
|
|
BLAKE2b-256 checksum How to use checksums |
1a275e11c07809492c44bb94f7a50e5608195523556d0bbb0b35ebfac2c5ad8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Release files / comfyui_xy-0.3.0-py3-none-any.whl
| Download URL | comfyui_xy-0.3.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8f6963022b180ab9e4def505ce86d297a01ef60a552592ffc3475dd25bbdd95c
|
|
BLAKE2b-256 checksum How to use checksums |
61bae21179fd8ab327bbdc4c50e4d3a59dc3c55fe00956106863492faa96f749
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Security for comfyui-xy 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.