Nodes/Model Utility Toolkit/Rename Text Encoder Keys
ComfyUI Node

Rename Text Encoder Keys

When your text encoder ships with the wrong labels

By silveroxides·Created about a year ago·Updated 7 days ago· 16
Rename Text Encoder Keys
    • output_path
    textencoder_name
    old_keys
    new_keys
    output_filenamerenamed_textencoder

    You downloaded a fine-tuned text encoder, ComfyUI won't load it, and somewhere in the error there's a tensor key that doesn't match what the loader expects. That's the whole reason this node exists. TextEncoderRenameKeys loads a text encoder from your models/text_encoders/ folder, renames the keys you tell it to, and writes a fresh .safetensors back into the same folder. It's a repair tool, not a creative one - and honestly, that's why 22 impressions in a year feels about right. Most people never need it. The people who do would otherwise be hand-rolling a Python script at 2am.

    The text encoder is the separate network that turns your prompt into conditioning vectors. A model file is just a dictionary of named tensors - the "keys" are those names - and loaders are strict about them. When someone ships an encoder whose keys use a prefix ComfyUI doesn't expect, or a LoRA trained against a differently-named encoder, the whole thing refuses to load. This node is the scalpel for exactly that mismatch.

    How it works

    The mechanism is refreshingly honest. You give it two lists - old_keys and new_keys, one pair per line - and it builds a rename map, streams every tensor out of the source file one at a time (low-memory mode, so it never holds a full model in RAM), writes each tensor under its new name, and saves a new safetensors atomically. Unmatched keys pass through untouched.

    Two things the code tells you that the README doesn't:

    • The matching is exact, not "pattern matching." The README's feature blurb says "pattern matching," but the implementation is a plain dictionary lookup on the full key name. If your line doesn't exactly match a key, nothing happens - no error, no rename, just silence. Get the real key names first (see below).
    • It silently converts .pt/.pth/.bin/.ckpt input to safetensors before renaming, so you can feed it a legacy pickle file too. That means it's also a lazy format converter, though safetensors is the format you want anyway.

    The inputs that matter

    • textencoder_name - dropdown of everything in models/text_encoders/.
    • old_keys / new_keys - the one pair that defines the whole job. One entry per line, paired by line, and the counts must match or it throws a ValueError.
    • output_filename - no extension needed; it lands in models/text_encoders/ under whatever name you give it.

    Output is a single output_path string pointing at the new file. Wire it into a text display node if you want to see where it went.

    Installing it

    It's part of the Model Utility Toolkit pack, so one install covers a dozen nodes. Easiest route is ComfyUI Manager - search "Model Utility Toolkit" (publisher silveroxides) and install. Manual:

    cd ComfyUI/custom_nodes
    git clone https://github.com/silveroxides/ComfyUI-ModelUtils
    

    Then restart ComfyUI. The pack's requirements.txt pulls in unifiedefficientloader (the memory-efficient loader this node actually uses), plus av, mutagen, requests, and Pillow for the pack's other nodes. The nodes use the newer ComfyUI node API, so keep your ComfyUI reasonably current rather than a year-old build.

    Where people get burned

    • "Number of old keys must match the number of new keys." You have a stray blank line or an extra entry. Count your lines.
    • Nothing got renamed. You're almost certainly matching substrings when it's exact - a key is conditioner.embedders.0.transformer.text_model.encoder.layers.0, not text_model.encoder. Inspect first with the pack's own GetMetaKeys node (also in ModelUtils) and copy key names from its output verbatim.
    • The file doesn't appear in loaders. ComfyUI caches the model list; refresh or restart before you go hunting.
    • Disk space. The .pt.safetensors conversion writes a temp file alongside the source, so a big encoder briefly wants double its disk footprint.

    Real talk: this is a niche repair node, and it knows it. But when you genuinely need one key renamed across a 2GB encoder, it's the difference between a 20-second workflow and writing your own streaming script.

    CategoryModelUtils/Keys

    Inputs (4)

    NameTypeDefaultDescription
    textencoder_nameCOMBOText encoder whose tensor keys will be renamed.
    old_keysSTRINGExisting key text or pattern, one entry per line, paired by line with New Keys.
    new_keysSTRINGReplacement key text, one entry per line, paired by line with Old Keys.
    output_filenameSTRINGrenamed_textencoderOutput filename without extension, written under the matching ComfyUI model directory.

    Outputs (1)

    NameTypeDescription
    output_path*