sgl-project/sglang · error · TypeError

Cannot put argument inside a f-string. This is not compatibl

Error message

Cannot put argument inside a f-string. This is not compatible with the tracer. 

What it means

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.

Source

Thrown at python/sglang/lang/ir.py:428

        self.value = value

    def __repr__(self):
        return f"Argument(name={self.name}, value={repr(self.value)})"

    def __len__(self):
        return len(self.value)

    def __getitem__(self, i):
        return self.value[i]

    def __int__(self):
        return self.value

    def __bool__(self):
        return self.value

    def __format__(self, *args):
        raise TypeError(
            "Cannot put argument inside a f-string. "
            "This is not compatible with the tracer. "
        )


class SglImage(SglExpr):
    def __init__(self, path: str):
        self.path = path

    def __repr__(self) -> str:
        return f"SglImage({self.path})"


class SglVideo(SglExpr):
    def __init__(self, path: str, num_frames: int):
        self.path = path
        self.num_frames = num_frames

View on GitHub (pinned to 0132848349)

Solutions

  1. Replace the f-string with concatenation: prefix + argument + suffix, which the tracer supports
  2. Use the library's text/interpolation primitives (SglText / sgen-style expression composition) to embed the argument
  3. Move dynamic string building outside the traced function and pass the result in as a plain value

Example fix

# before
q = f"Question: {argument}\n"

# after
q = "Question: " + argument + "\n"
Defensive patterns

Strategy: type-guard

Validate before calling

from sglang.lang.ir import SglArgument
if isinstance(piece, SglArgument):
    text = "Question: " + piece + "\n"

Type guard

def is_sgl_expr(x) -> bool:
    from sglang.lang.ir import SglExpr
    return isinstance(x, SglExpr)

Try / catch

try:
    s = f"{arg}"
except TypeError:
    s = "prefix" + arg + "suffix"

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/45e543a53be8e600. Report an issue: GitHub.