Skip to main content

No project description provided

Project description

comfy-api-client

A Python client for the ComfyUI API, providing:

:zap: Full API coverage
:zap: Asynchronous execution
:zap: WebSocket support
:zap: Workflow templating
:zap: WebSocket or HTTP-based polling

Installation

Install the package using pip:

pip install comfy-api-client

Usage

Create a Client

Use the create_client context manager to create a ComfyUI client. This will set up the underlying HTTP client and a WebSocket or HTTP-based state tracker to poll results from the server:

from comfy_api_client import create_client

# Protocol is omitted as the URL may be used for both HTTP and WebSocket requests
comfyui_server = "localhost:8188"

async with create_client(comfyui_server) as client:
    print(await client.get_system_stats())

Submit Workflows

To submit a workflow, read the workflow configuration file and pass it to the client:

from comfy_api_client import utils

workflow = utils.read_json("workflow.json")

async with create_client(comfyui_server) as client:
    prompt = await client.submit_workflow(workflow)

    result = await prompt.future
    image_items = result.output_images
    image = image_items[0].image

Result polling

HTTP-based polling

By default, the execution state of a prompt is tracked via a WebSocket connection. In cases where a WebSocket connection cannot be established, e.g. if the ComfyUI server is behind a proxy or firewall, HTTP-based polling can be used instead.

Simply provide start_state_tracker="http" to the create_client function:

async with create_client(comfyui_server, start_state_tracker="http") as client:
    # Submit and await results as before
    prompt = await client.submit_workflow(workflow)
    result = await prompt.future

Note, that HTTP polling relies on frequent querying of the prompt status from the ComfyUI server and will thus create more traffic while being less responsive.

Manual polling

The prompt status and results can also be queried manually. In this case, start_state_tracker=None can be passed to create_client and return_future=False to the submit_workflow() method.

import time

async with create_client(comfyui_server, start_state_tracker=None) as client:
    # Submit and await result as before
    prompt = await client.submit_workflow(workflow, return_future=False)

    # Will be None; the status needs to be checked manually
    prompt.future

    # Wait for the prompt to finish
    time.sleep(20)

    result = await client.fetch_results(prompt)

    image_items = result.output_images
    image = image_items[0].image

Tests

Run tests:

pytest tests

This will set up a local ComfyUI instance to test against.

TODOs

  • Add logging support
  • Improve error handling and messages
  • Implement a synchronous client

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

comfy_api_client-0.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

comfy_api_client-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file comfy_api_client-0.1.0.tar.gz.

File metadata

  • Download URL: comfy_api_client-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Darwin/24.0.0

File hashes

Hashes for comfy_api_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 52d2bcc43809c9774751ddb6cda91a662458b5f45c1eed51175c2c3752912d22
MD5 bdc0b9cc128f2d6402a936f1e2cb2e9c
BLAKE2b-256 99d156e737b837d88906e8d25c1ade8d86e8a63c684f0d62dc26e4bc030331eb

See more details on using hashes here.

File details

Details for the file comfy_api_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for comfy_api_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01be45a33c90c65930ee1f662b207c1827b0392b1d55b9554d840904727f526e
MD5 cfabf5cafb69209e42d3841a47d16f08
BLAKE2b-256 1ec5c5500819a8c28e610dce0a061fd1a5801f14b3a42eba9cce12e856ae9de8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page