reflex-dev/reflex · error · ValueError

transformers should be a LiteralVar type. Got {type(self.tra

Error message

transformers should be a LiteralVar type. Got {type(self.transformers)} instead.

What it means

rx.code_block(..., transformers=...) from reflex_components_code requires the transformers argument to be a LiteralVar (built via rx.var/literal from a list of ShikiBaseTransformers) so the component can statically extract the imports each transformer needs. Passing a raw list or other object raises ValueError in add_imports.

Source

Thrown at packages/reflex-components-code/src/reflex_components_code/shiki_code_block.py:642

            style=Style({**transformer_styles, **BOX_PARENT_STYLING}),
            **code_wrapper_props,
        )

    def add_imports(self) -> dict[str, list[str]]:
        """Add the necessary imports.
        We add all referenced transformer functions as imports from their corresponding
        libraries.

        Returns:
            Imports for the component.

        Raises:
            ValueError: If the transformers are not of type LiteralVar.
        """
        imports = defaultdict(list)
        if not isinstance(self.transformers, LiteralVar):
            msg = f"transformers should be a LiteralVar type. Got {type(self.transformers)} instead."
            raise ValueError(msg)
        for transformer in self.transformers._var_value:
            if isinstance(transformer, ShikiBaseTransformers):
                imports[transformer.library].extend([
                    ImportVar(tag=str(fn)) for fn in transformer.fns
                ])
                if transformer.library not in self.lib_dependencies:
                    self.lib_dependencies.append(transformer.library)
        return imports

    @classmethod
    def create_transformer(cls, library: str, fns: list[str]) -> ShikiBaseTransformers:
        """Create a transformer from a third party library.

        Args:
            library: The name of the library.
            fns: The str names of the functions/callables to invoke from the library.

        Returns:

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Wrap the transformers list in a literal var: transformers=rx.Var([NotationDiff(), Whitespace()]) / LiteralVar.create([...]).
  2. Pass the transformers as literal data so the component can walk transformer.library and transformer.fns at compile time.
  3. Check the component's docs for the accepted transformer classes and construct them accordingly.

Example fix

# before
rx.code_block(code, language="diff", transformers=[rx.code_block.NotationDiff()])

# after
from reflex.vars import LiteralVar
rx.code_block(
    code,
    language="diff",
    transformers=LiteralVar.create([rx.code_block.NotationDiff()]),
)
Defensive patterns

Strategy: type-guard

Validate before calling

from reflex.vars import LiteralVar

def transformers_ok(t) -> bool:
    return isinstance(t, LiteralVar)

Type guard

from reflex.vars import LiteralVar

def is_literal_var(v) -> bool:
    return isinstance(v, LiteralVar)

Prevention

When it happens

Trigger: Passing a plain Python list of transformer objects (e.g. transformers=[NotationDiff()]) instead of a LiteralVar; passing a computed/route var; leaving an unannotated mutable default.

Common situations: Upgrading reflex-components-code where transformers used to accept raw lists; constructing transformer values dynamically from props; passing the result of a non-literal Var operation.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/bf83ecb7a2956798. Report an issue: GitHub.