VAE Encode (Optional)
A VAE encode that doesn't break your graph when there's no image
- vae
- image
- LATENT
The stock VAEEncode node in ComfyUI is simple and unforgiving: give it an image and a VAE, it hands back a latent. Give it nothing, and it errors. VAEEncodeOptional does the same encode when you do have an image, but if the image input is empty it just returns None instead of blowing up your graph. That's the entire node, and it's a genuinely useful difference if you build workflows with an optional img2img path.
Why you'd want this
The normal way to make an img2img branch optional in ComfyUI is a switch or router node upstream of a real VAEEncode - something has to make sure an image is actually present before the encode node ever runs. VAEEncodeOptional skips that step: leave the image input unconnected (or feed it a value that resolves to nothing) and the node quietly passes None through instead of throwing. You get a single node you can leave permanently wired into a "maybe img2img, maybe txt2img" workflow, with the image source doing the deciding rather than a separate switch.
The catch, and it's worth saying plainly: a None latent is only harmless to things built to expect it. Feed it straight into a stock KSampler and that node will error just as hard as the original VAEEncode would have on a missing image - it doesn't know what to do with nothing. This node is meant to pair with something downstream that's also None-aware, whether that's another optional node in this same pack or your own conditional logic, not to make a whole workflow silently tolerate a missing image on its own.
The mechanism
Encoding is the same job either way: the VAE compresses your pixel image down into the smaller latent space diffusion actually works in. That compression is lossy - every encode/decode round trip loses a little information - but that's the unavoidable cost of doing img2img at all, not something specific to this node. When there's no image to encode, there's nothing to lose; the node just skips the work and returns nothing.
What matters
vae(VAE, required) - the matching VAE for whatever checkpoint you're using. Wrong VAE, and you get a garbled or wrong-looking latent, same as the stock node.image(IMAGE, optional) - the image to encode. Leave it disconnected and you getNoneout instead of an error.
One output: LATENT - the encoded latent, or None if no image was given.
Installing it
Search ComfyUI-TkNodes in ComfyUI Manager, or clone it directly:
cd ComfyUI/custom_nodes
git clone https://github.com/TensorKaze/ComfyUI-TkNodes
cd ComfyUI-TkNodes
pip install -r requirements.txt
Restart ComfyUI. Nothing heavy here - this node adds no dependencies of its own beyond what the pack as a whole needs.
Common issues
Downstream node errors on a missing latent. Expected, and not a bug in this node. VAEEncodeOptional returning None only helps if whatever's next in the chain is built to handle None gracefully - a stock KSampler or VAEDecode is not. If you're routing the output somewhere that isn't None-aware, you still need a switch or a default-latent fallback before it.
Output looks wrong, not empty. If you did wire an image in and the latent looks off, that's a VAE-mismatch problem, not something specific to this node - check that the VAE you're feeding it actually matches the checkpoint you're sampling with.
This is a small, quiet pack - no big community footprint, no workflow-share threads built around it. It's a solo dev's collection of small quality-of-life wrappers, and this is one of the more genuinely useful ones if you already build workflows with optional branches.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| vae | VAE | The VAE model used for encoding the image to latent space. | |
| imageopt | IMAGE | The image to encode to latent space. If not provided, returns None. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| LATENT | LATENT | The encoded latent image, or None if no image is provided. |