ComfyUI Node

encode

Why your text suddenly looks like b'\\xc3\\xb6'

By StableLlama·Created about a year ago·Updated 4 days ago· 48
encode
    • STRING
    string
    encoding
    errors

    You might expect an "encode" node to hand you actual bytes. It can't - ComfyUI moves strings around, not bytes objects. So this node does the closest thing: it encodes your text into bytes and then hands you the string representation of those bytes. That's why the output looks like b'\xc3\xb6\xc3\xa4' instead of the text you put in.

    What it does

    Under the hood it's str.encode(encoding, errors), then a str() of the resulting bytes. So a string like "öäü" with utf-8 becomes the output "b'\\xc3\\xb6\\xc3\\xa4\\xc3\\xbc'" - a bytes literal rendered as text, escapes and all.

    The inputs that matter:

    • string - the text to encode (required).
    • encoding - dropdown: utf-8 (default), ascii, latin-1, utf-16, utf-32, cp1252 (required).
    • errors - dropdown: strict (default), ignore, replace, xmlcharrefreplace, backslashreplace (required).

    Output: one STRING.

    Why you'd reach for it

    The honest use is less glamorous than it sounds. You reach for this when you want to see what your text looks like in a given encoding, or when you're preparing text for a format that needs a specific encoding and you want to verify it first. The realistic pairing is with the pack's decode node for round-trip testing - encode a string, decode it back, confirm you get what you started with.

    The trap

    Encoding to ascii with strict errors on non-ASCII text throws - and in this node, "throws" means "returns Encoding error: ... as a string". Same pattern as its decode sibling: no crash, but error text flows silently downstream. If you're encoding text that might contain special characters, switch errors to replace or ignore unless you genuinely want strictness.

    And remember the output is display/storage representation, not real bytes. You can't wire this directly into a node expecting an actual bytes object; it's for inspection and for feeding the decode node.

    Installing

    Part of Basic data handling by StableLlama. In ComfyUI Manager search "Basic data handling" → Install → restart. Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
    

    Restart ComfyUI. Zero runtime dependencies - pure Python stdlib - so there's no pip install dance and no risk of dependency conflicts with your other packs.

    Where people get burned

    Expectation mismatch, mostly - people wire encode into a file-save and wonder why the file contains b'...' text. It's a string all the way down. Also, encoding and decoding must use the same codec; mixing utf-8 on the way in and latin-1 on the way out is a guaranteed way to produce mojibake.

    CategoryBasic/STRING

    Inputs (3)

    NameTypeDefaultDescription
    stringSTRING
    encodingCOMBO6 options: utf-8, ascii, latin-1, utf-16, utf-32, cp1252
    errorsCOMBO5 options: strict, ignore, replace, xmlcharrefreplace, backslashreplace

    Outputs (1)

    NameTypeDescription
    STRINGSTRING