GLM-5 is available to try on Modal. Get started
April 2, 20245 minute read

How to convert a ComfyUI workflow to Python code

author
Kenny Ning@kenny_ning
Data Engineer

Edit 2024-08-26: Our latest recommended solution for productionizing a ComfyUI workflow is detailed in this example. As a result, this post has been largely re-written to focus on the specific use case of converting a ComfyUI JSON workflow to Python.

ComfyUI workflow diagram

ComfyUI is a popular no-code, visual editor for building complex image generation workflows. While ComfyUI started as a prototyping / experimental playground for Stable Diffusion, increasingly more users are using it to deploy image generation pipelines in production.

Modal is a great solution for this and our ComfyUI example walks you through the step-by-step process of serving your ComfyUI workflow behind an API endpoint. This is our recommended solution for productionizing ComfyUI in most cases.

However, some users prefer defining and iterating on their ComfyUI workflows in Python. For example, if you’re doing some complex user prompt handling in your workflow, Python is arguably easier to work with than handling the raw workflow JSON object. In this blog post, we’ll show you how to convert your ComfyUI workflow to executable Python code as an alternative design to serving a workflow in production.

Export your workflow as JSON

The native representation of a ComfyUI workflow is in JSON. First, we need to extract that representation from the UI.

  1. Click the gear icon in the top right of the menu box:

ComfyUI menu box with gear circled

  1. Check Enable Dev mode Options:

ComfyUI enable dev options selection

  1. Click Save (API Format) option in your menu:

ComfyUI menu with api json cirlced

Save the file as workflow_api.json.

Convert JSON to Python

We’ll use the ComfyUI to Python Extension to convert the JSON from the previous step to Python code. This tool requires ComfyUI to be installed on the host machine, so we’ll use Modal to install ComfyUI in a container and then run the tool in that container.

Save the following script to a file called comfypython.py in the same directory as the workflow_api.json you created in the previous step.

At a high level, this script will convert a JSON node representation:

Into a Python object:

Run modal run comfypython::fetch_comfyui_to_python to convert workflow_api.json into a Python file called _generated_workflow_api.py in your local directory.

Run the Python workflow

Now we can run this generated code and fetch the generated images. Add the following to the comfypython.py script we created in the previous step:

Run modal run comfypython.py::fetch_images to run the Python workflow and write the generated images to your local directory.

Conclusion

Some of our users have had success using this approach to establish the foundation of a Python-based ComfyUI workflow, from which they can continue to iterate. For example:

  • Add command-line arguments to _generated_workflow_api.py to handle prompts
  • Turn run_comfyui_python into a web endpoint that can receive requests via API

Generally speaking though, we don’t recommend this approach anymore to productionize your ComfyUI pipeline because the extra step required to convert from JSON to Python isn’t worth the marginal ergonomic beneftis of coding in Python. This is especially true if your workflow contains a lot of models or custom nodes. Your best bet is to follow our ComfyUI example which directly serves your ComfyUI workflow JSON.

Ship your first app in minutes.

Get Started

$30 / month free compute