Nodes/ComfyUI_LiteLLM/FirstCodeBlockReflectionFilter
ComfyUI Node

FirstCodeBlockReflectionFilter

A filter for code-generating loops

By Hopping-Mad-Games·Created 2 years ago·Updated 11 months ago· 7
FirstCodeBlockReflectionFilter
  • CALLABLE
  • args
  • kwargs
  • callable

Every LLM answers a "write code for me" prompt with the same crime: a markdown code fence, a language tag, and often a paragraph of commentary around it. If you're feeding that output back into a loop or into a tool that needs raw code, the fences are garbage in the pipeline. FirstCodeBlockReflectionFilter exists to cut them out: it wraps a callable so that before the completion goes downstream, everything outside the first fenced block is dropped.

Like CreateReflectionFilter, it's an adapter - it doesn't generate anything itself. It takes a callable and returns a new callable that pre-processes its input. The difference is the pre-processing: strip to the first ``` block (and drop a leading python/language tag if present), then hand the clean code to the wrapped callable.

How it works

When the wrapped filter is called with a completion, it checks for ```. If there's no fence, the completion passes through untouched - which is a thoughtful default, because it means text-only answers aren't destroyed. If there is a fence, it takes everything between the first pair of backticks and drops the rest. If the block starts with a language tag like python, that's removed too. The cleaned string then goes into the wrapped callable as the first argument, followed by any args/kwargs you configured.

The inputs

  • CALLABLE (required) - the function to wrap. Typical sources: another filter, a LiteLLMCompletionProvider output, or any CALLABLE your graph produces.
  • args (optional, LIST) - positional args appended after the cleaned completion.
  • kwargs (optional, DICT) - keyword args passed through.

Output: callable (CALLABLE). Connect it to a LiteLLMCompletionWithReflectionFilter's reflection_filter input for a code-refining loop: the model writes code, the filter pulls the code block out, the loop feeds the block back for review.

Installing

Same pack as always - ComfyUI Manager, search "ComfyUI_LiteLLM", or:

cd ComfyUI/custom_nodes
git clone https://github.com/Hopping-Mad-Games/ComfyUI_LiteLLM
cd ComfyUI_LiteLLM
pip install -r requirements.txt

Restart; it's under ETK/LLM/LiteLLM.

Where people get burned

The fence-splitting is naive by design: it splits on the first ``` and takes the segment after it, then everything up to the next ```. It does not handle nested fences, multiple code blocks, or fences that never close - if the model emits an unclosed block, the filter's output is effectively the tail of the answer, which may not be code at all. So it's great for single-block answers and fragile for anything more elaborate; if your code comes back with embedded explanations inside the fence, this filter won't save you. Second, like all these wrapper nodes, an unconnected CALLABLE (default None) makes a filter that crashes the loop on first use - always wire a real callable. And note the pass-through rule cuts both ways: a completion with no fence is returned untouched, which means this filter alone won't force structure on a model that refuses to use fences. Pair it with a prompt that demands a fenced code block, and it becomes a very clean pipe.

CategoryETK/LLM/LiteLLM

Inputs (3)

NameTypeDefaultDescription
CALLABLECALLABLE
argsoptLIST
kwargsoptDICT[object Object]

Outputs (1)

NameTypeDescription
callableCALLABLE