Nodes/ComfyDL/String → Tensor
ComfyUI Node

String → Tensor

Type a matrix by hand and get a tensor — the pack's manual data entry

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
String → Tensor
    • tensor
    text[[1, 2, 3], [4, 5, 6]]
    error_strategyempty_tensor

    Sometimes you don't need a fancy dataset generator - you just want to type [[1, 2, 3], [4, 5, 6]] into a box and have it become a tensor you can poke at. That's exactly what String → Tensor does. It parses a Python-list-literal-style string into a torch tensor, which makes it the ComfyDL equivalent of typing a value into a REPL. If you're testing a transpose, a reshape, or a loss function with hand-picked numbers, this is the node you reach for.

    The mechanism is simple and honest: it strips all whitespace, cleans up trailing commas (so [1,2,] parses fine - that's a nicety most people don't expect), and hands the result to Python's ast.literal_eval. That last part is the clever bit: literal_eval only accepts actual literals - no code execution, no __import__ shenanigans - so this is about as safe as string-to-data parsing gets. The result is always cast to torch.float32, so don't try to feed it integers and expect a long tensor back.

    Inputs

    Just two, and only text matters day to day:

    • text - a multiline box, defaulting to [[1, 2, 3], [4, 5, 6]]. Paste a matrix, a vector, even a plain scalar (42 gets wrapped into a one-element tensor).
    • error_strategy - a dropdown deciding what happens on a parse failure: empty_tensor (an empty tensor, the default), zero_tensor (a [0.] tensor), or raise_error (let the exception blow up the queue so you actually notice).

    The error_strategy dropdown is the kind of small thoughtful touch that tells you this is an educational pack: instead of just crashing on a typo, it lets you choose between "silently give me something" and "scream at me." For debugging, raise_error is your friend; for wiring something into a pipeline where a bad parse shouldn't kill the whole run, empty_tensor is nicer.

    Output

    One output, tensor, of the pack's cdlTensor type. Wire it into Transpose, Squared Loss, or the pack's Tensor → String node to print it back - the round trip is a great way to sanity-check what the parser did to your input.

    Installing it

    It's part of ComfyDL, not a standalone download. ComfyUI Manager: search "ComfyDL". Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Cynthia-lxx/ComfyDL
    pip install -r ./ComfyDL/requirements.txt
    

    Restart ComfyUI and it'll be under the Tensor Basic category. The whole pack only depends on matplotlib, so there's no heavy install to babysit.

    Gotchas

    The big one is the float32 coercion - a matrix of integers becomes floats, which surprises people building index tensors. If you need integer indices, use Vocab Encode or Truncate/Pad, which produce long tensors directly. And remember the output is a custom cdlTensor, so it only plugs into other ComfyDL sockets, not native ComfyUI tensor inputs. This is a tiny node in a young pack (basically zero community presence yet), but because it's a thin wrapper over ast.literal_eval, there's almost nothing to go wrong that the error dropdown won't tell you about.

    Categoryd2l/Tensor Basic

    Inputs (2)

    NameTypeDefaultDescription
    textSTRING[[1, 2, 3], [4, 5, 6]]
    error_strategyCOMBOempty_tensor3 options: empty_tensor, zero_tensor, raise_error

    Outputs (1)

    NameTypeDescription
    tensorTENSOR