{"record":{"id":"45e543a53be8e600","repo":"sgl-project/sglang","slug":"cannot-put-argument-inside-a-f-string-this-is-not","errorCode":null,"errorMessage":"Cannot put argument inside a f-string. This is not compatible with the tracer. ","messagePattern":"Cannot put argument inside a f-string\\. This is not compatible with the tracer\\. ","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/lang/ir.py","lineNumber":428,"sourceCode":"        self.value = value\n\n    def __repr__(self):\n        return f\"Argument(name={self.name}, value={repr(self.value)})\"\n\n    def __len__(self):\n        return len(self.value)\n\n    def __getitem__(self, i):\n        return self.value[i]\n\n    def __int__(self):\n        return self.value\n\n    def __bool__(self):\n        return self.value\n\n    def __format__(self, *args):\n        raise TypeError(\n            \"Cannot put argument inside a f-string. \"\n            \"This is not compatible with the tracer. \"\n        )\n\n\nclass SglImage(SglExpr):\n    def __init__(self, path: str):\n        self.path = path\n\n    def __repr__(self) -> str:\n        return f\"SglImage({self.path})\"\n\n\nclass SglVideo(SglExpr):\n    def __init__(self, path: str, num_frames: int):\n        self.path = path\n        self.num_frames = num_frames\n","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/lang/ir.py#L410-L446","documentation":"SglArgument deliberately implements __format__ to raise TypeError because formatting a traced argument inside an f-string would eagerly evaluate it, breaking sglang's tracer which needs to capture the expression graph symbolically. Any attempt to interpolate an SGL argument into a string raises this immediately.","triggerScenarios":"Writing f\"{some_argument}\" or \"{}\".format(arg) inside an SGL function where some_argument is an SglArgument (e.g. created via sgen or a function parameter traced by the frontend).","commonSituations":"Building prompts with f-strings that embed traced variables instead of using string concatenation or the library's own composition primitives; porting normal Python prompt code into a traced SGL function.","solutions":["Replace the f-string with concatenation: prefix + argument + suffix, which the tracer supports","Use the library's text/interpolation primitives (SglText / sgen-style expression composition) to embed the argument","Move dynamic string building outside the traced function and pass the result in as a plain value"],"exampleFix":"# before\nq = f\"Question: {argument}\\n\"\n\n# after\nq = \"Question: \" + argument + \"\\n\"","handlingStrategy":"type-guard","validationCode":"from sglang.lang.ir import SglArgument\nif isinstance(piece, SglArgument):\n    text = \"Question: \" + piece + \"\\n\"","typeGuard":"def is_sgl_expr(x) -> bool:\n    from sglang.lang.ir import SglExpr\n    return isinstance(x, SglExpr)","tryCatchPattern":"try:\n    s = f\"{arg}\"\nexcept TypeError:\n    s = \"prefix\" + arg + \"suffix\"","preventionTips":["Never embed traced arguments in f-strings inside SGL functions","Lint for f-strings containing identifiers bound to SglArgument in traced code"],"tags":["sglang","f-string","tracer","typeerror"],"backgroundTag":"unsupported-operation-in-traced-code","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}