sgl-project/sglang · error · ValueError

Tried to append None to state.

Error message

Tried to append None to state.

What it means

Raised by State.__iadd__ (the `+=` operator on an SGL program state) when the right-hand side is None. Appending None is almost always a bug — usually an expression or variable that evaluated to None being added to the program.

Source

Thrown at python/sglang/lang/interpreter.py:1025

                        break
        else:
            if var_name is None:
                yield self.text()
            else:
                yield self.get_var(var_name)

    def get_var(self, name):
        return self.stream_executor.get_var(name)

    def set_var(self, name, value):
        return self.stream_executor.set_var(name, value)

    def get_meta_info(self, name):
        return self.stream_executor.get_meta_info(name)

    def __iadd__(self, other):
        if other is None:
            raise ValueError("Tried to append None to state.")
        self.stream_executor.submit(other)
        return self

    def __getitem__(self, name):
        return self.get_var(name)

    def __setitem__(self, name, value):
        self.set_var(name, value)

    def __contains__(self, name):
        return name in self.stream_executor.variables

    def __del__(self):
        self.stream_executor.end()

    def __repr__(self) -> str:
        return f"ProgramState({self.text()})"

View on GitHub (pinned to 0132848349)

Solutions

  1. Guard the append: only `+=` when the value is not None
  2. Fix the upstream expression so it returns '' or a valid Sgl primitive instead of None
  3. Use explicit defaults: vars.get('x', '')

Example fix

# before
state += prompt_parts.get("image")  # None when no image
# after
img = prompt_parts.get("image")
if img is not None:
    state += img
Defensive patterns

Strategy: type-guard

Validate before calling

if value is not None:
    state += value

Type guard

def appendable(v) -> bool:
    return v is not None

Try / catch

try:
    state += value
except ValueError as e:
    if "None to state" in str(e):
        pass  # skip optional segment
    else:
        raise

Prevention

When it happens

Trigger: `state += maybe_image` where maybe_image is None (e.g. multimodal payload absent); `state += vars.get('x')` with a missing key; a helper returning None on failure being appended unconditionally.

Common situations: Optional image/audio inputs not present in the request; dict.get() defaulting to None; branching logic that was supposed to append text but fell through.

Related errors


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