sgl-project/sglang · error · TypeError

{key}: expected {field_type}, got {value!r}

Error message

{key}: expected {field_type}, got {value!r}

What it means

Raised when _parse_env_value fails to coerce the string value to the type of the field's default (e.g. int('abc')). The original ValueError/TypeError is chained, and the message names the expected type (derived from type(default).__name__) and the offending raw value.

Source

Thrown at python/sglang/srt/debug_utils/dumper.py:123

        missing = object()
        defaults = {f.name: f.default for f in fields(cls)}
        result: dict = {}

        for pair in pairs:
            key, sep, value = pair.partition("=")
            if not sep:
                raise ValueError(f"Invalid config pair (missing '='): {pair!r}")
            default = defaults.get(key, missing)
            if default is missing:
                raise ValueError(
                    f"Unknown config key {key!r}. Valid keys: {sorted(defaults)}"
                )
            try:
                result[key] = cls._parse_env_value(value, default)
            except (ValueError, TypeError) as exc:
                field_type = type(default).__name__
                raise TypeError(f"{key}: expected {field_type}, got {value!r}") from exc

        return result


_DEFAULT_EXP_NAME_PREFIX = "dump_"


@dataclass(frozen=True)
class DumperConfig(_BaseConfig):
    enable: bool = False
    filter: Optional[str] = None
    dir: str = "/tmp/dumper"
    enable_output_file: bool = True
    enable_output_console: bool = True
    enable_value: bool = True
    enable_grad: bool = False
    enable_model_value: bool = False
    enable_model_grad: bool = False

View on GitHub (pinned to 0132848349)

Solutions

  1. Match the literal format the type expects: ints as digits, bools as true/false (check _parse_env_value's accepted bool spellings)
  2. Strip units/quotes from the value string
  3. Verify the field's default type via the 'Valid keys' error or dataclasses.fields before writing the value

Example fix

// before
cfg = DumpConfig.from_kv_pairs(["interval=abc"])
// after
cfg = DumpConfig.from_kv_pairs(["interval=10"])
Defensive patterns

Strategy: try-catch

Validate before calling

# pre-validate against default types
from dataclasses import fields
for p in pairs:
    k, _, v = p.partition("=")
    f = next(f for f in fields(DumpConfig) if f.name == k)
    type(f.default)(v)  # raises early if unparseable

Try / catch

try:
    cfg = DumpConfig.from_kv_pairs(pairs)
except TypeError as e:
    # e.message names key, expected type, and bad value — fix that pair
    ...

Prevention

When it happens

Trigger: Passing 'interval=abc' when the field default is an int; 'enabled=maybe' when the default is a bool; any string that the default type cannot parse.

Common situations: Booleans given as 'yes'/'1'? (parser may only accept 'true'/'false'), floats given with trailing units ('10ms'), locale-dependent numbers.

Related errors


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