{"record":{"id":"548f0eddacef703a","repo":"reflex-dev/reflex","slug":"code-should-be-string-literal-or-a-stringvar-type","errorCode":null,"errorMessage":"code should be string literal or a StringVar type. Got {type(code)} instead.","messagePattern":"code should be string literal or a StringVar type\\. Got (.+?) instead\\.","errorType":"exception","errorClass":"VarTypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-components-code/src/reflex_components_code/shiki_code_block.py","lineNumber":839,"sourceCode":"        return ShikiCodeBlock.create(children[0], **props)\n\n    @staticmethod\n    def _map_themes(theme: str) -> str:\n        if isinstance(theme, str) and theme in THEME_MAPPING:\n            return THEME_MAPPING[theme]\n        return theme\n\n    @staticmethod\n    def _map_languages(language: str) -> str:\n        if isinstance(language, str) and language in LANGUAGE_MAPPING:\n            return LANGUAGE_MAPPING[language]\n        return language\n\n    @staticmethod\n    def _strip_transformer_triggers(code: str | StringVar) -> StringVar | str:\n        if not isinstance(code, (StringVar, str)):\n            msg = f\"code should be string literal or a StringVar type. Got {type(code)} instead.\"\n            raise VarTypeError(msg)\n        regex_pattern = r\"[\\/#]+ *\\[!code.*?\\]\"\n\n        if isinstance(code, Var):\n            return string_replace_operation(\n                code, StringVar(_js_expr=f\"/{regex_pattern}/g\", _var_type=str), \"\"\n            )\n        if isinstance(code, str):\n            return re.sub(regex_pattern, \"\", code)\n        return None\n\n\nclass TransformerNamespace(ComponentNamespace):\n    \"\"\"Namespace for the Transformers.\"\"\"\n\n    shikijs = ShikiJsTransformer\n\n\nclass CodeblockNamespace(ComponentNamespace):","sourceCodeStart":821,"sourceCodeEnd":857,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-components-code/src/reflex_components_code/shiki_code_block.py#L821-L857","documentation":"Shiki code blocks accept only Python str or reflex StringVar for the `code` prop so the component can strip transformer trigger comments ([!code ...]) via a compiled JS regex. Any other type (int, list, a Var of non-string type) triggers VarTypeError at render prep time.","triggerScenarios":"Passing rx.State numeric/list state vars, Python numbers, or arbitrary objects as the `code` argument of shiki_code_block / code_block create(), which routes through _strip_transformer_triggers.","commonSituations":"Binding a State field typed as int or list (e.g. a line count or file array) directly to the code prop, or passing a computed Var whose _var_type is not str.","solutions":["Convert the value to a string: str(value) for literals, or value.to_string() for Vars","Type the backing State field as str: code: str = ''","For non-string Vars, wrap with rx.code(... only after .to_string()) before passing to the shiki component"],"exampleFix":"// before\nrx.shiki_code_block(State.line_count)  # int var\n\n// after\nrx.shiki_code_block(State.code)  # code: str\n# or\nrx.shiki_code_block(State.line_count.to_string())","handlingStrategy":"type-guard","validationCode":"from reflex import Var\nfrom reflex.vars import StringVar\ncode = State.code if isinstance(State.code, str) else State.code.to_string()","typeGuard":"def is_code_arg(code) -> bool:\n    from reflex.vars import StringVar\n    return isinstance(code, (str, StringVar))","tryCatchPattern":null,"preventionTips":["Type upload/source State fields used for code as str","Call .to_string() on non-string Vars before passing to code blocks"],"tags":["shiki","var-type","prop-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}