Nodes/XB_ToolBox/XB-llama - 📝 解包代码块
ComfyUI Node

XB-llama - 📝 解包代码块

Strip the ``` fences off your LLM's JSON

By wjluoxiao·Created 5 months ago·Updated 6 days ago· 302
XB-llama - 📝 解包代码块
    • output
    input
    label

    Every LLM thinks it's being helpful by wrapping its JSON in a fenced code block, and every downstream parser hates it. XB_llamaUnpackCodeBlock is the two-line fix: it strips the ``` fences (and the optional language label) off a string so the clean content comes out the other side.

    It's a small node, and it should be. It does exactly one job and does it without a model loaded, without VRAM, without any of the machinery the rest of the XB-llama family carries. The reason it's worth knowing about is that "clean up the LLM's output before parsing it" is one of the two failure modes that sinks every LLM-in-the-graph workflow (the other is subject drift, which this can't fix).

    How it works

    Give it a string, get a string. It strips the leading and trailing whitespace, then removes a leading ``` and trailing ``` - and if you pass an optional label, it also removes ```<label> exactly, so a ```json fence gets its json tag stripped too.

    input:  ```json
            {"shots": ["a", "b"]}
            ```
    output: {"shots": ["a", "b"]}
    

    Two details make it more useful than it looks:

    • It handles lists. The input type is STRING but it's marked forceInput, and the code checks whether it received a list - an LLM node's output-list, for instance - and processes every item. Feed it a list of five fenced blocks, get a list of five clean strings.
    • Input is required but label is optional. If your model isn't consistent about tagging its fences, leave label empty; the strip still works because it removes the backticks regardless.

    The output is the output STRING. If you fed it a single string you get a single string; if you fed a list you get a list back.

    Where it fits

    It belongs between an LLM node and anything that needs strict text - a XB_llamaParseJSON node, a prompt encoder, a text-to-conditioning path. The storyboard processors in this pack, for example, expect clean lines; if the LLM wrapped its output in fences first, this node is what cleans up before the parse.

    It's also handy if you're scripting: run ComfyUI headless with an LLM node producing fenced output, and this is the difference between a JSON parse succeeding on the first try and you hand-editing strings all afternoon.

    Install & notes

    cd ComfyUI/custom_nodes
    git clone https://github.com/WJLUOXIAO/XB_ToolBox.git
    # or: ComfyUI Manager → "XB_ToolBox"
    pip install llama-cpp-python
    

    Same caveat as every XB-llama node: the pack's README says zero pip dependencies, but this family only loads when llama-cpp-python is installed, so that pip install matters even though this particular node never touches a model. Annoying for such a trivial utility, but it's how the pack is wired.

    Common gotchas are mild: if the LLM's fence is ``` with a trailing space, the strip can leave a stray backtick - the code removes a leading ``` but doesn't hunt for extra whitespace inside the fence. And if your LLM isn't using fenced blocks (many abliterated models are trained to avoid markdown), this node is a no-op that just trims whitespace, which is fine - better a harmless pass-through than an error.

    CategoryXB-llama

    Inputs (2)

    NameTypeDefaultDescription
    inputSTRING
    labeloptSTRING

    Outputs (1)

    NameTypeDescription
    outputSTRING