sgl-project/sglang · error · TypeError

content part must be mapping, got {type(part).__name__}

Error message

content part must be mapping, got {type(part).__name__}

What it means

When content is a sequence, every element must be either a string or a Mapping (part dict). A non-mapping element such as an int, list, or tuple raises TypeError naming the actual type.

Source

Thrown at python/sglang/srt/parser/inkling_renderer.py:230

    input_ids.append(tokenizer.encode_special(END_MESSAGE))


def _iter_render_parts(content: Any):
    """Yield ordered ``(kind, text)`` pairs from message content."""
    if content is None:
        return
    if isinstance(content, str):
        if content:
            yield ("text", content)
        return
    if not isinstance(content, Sequence) or isinstance(content, (bytes, bytearray)):
        raise TypeError("message content must be a string or a sequence of parts")
    for part in content:
        if isinstance(part, str):
            yield ("text", part)
            continue
        if not isinstance(part, Mapping):
            raise TypeError(f"content part must be mapping, got {type(part).__name__}")
        ptype = part.get("type")
        if ptype in (None, "text", "input_text"):
            text = part.get("text", "")
            yield ("text", text if isinstance(text, str) else "")
        elif ptype in ("thinking", "reasoning"):
            text = part.get("thinking")
            if text is None:
                text = part.get("text", "")
            if not isinstance(text, str):
                raise TypeError("Inkling thinking part payload must be a string")
            yield ("thinking", text)
        elif ptype in _IMAGE_PART_TYPES:
            yield ("image", "")
        elif ptype in _AUDIO_PART_TYPES:
            yield ("audio", "")
        else:
            raise ValueError(f"unsupported content part type: {ptype!r}")

View on GitHub (pinned to 0132848349)

Solutions

  1. Convert every element to a part dict {"type":"text","text":str(value)} or a plain string
  2. Flatten nested lists before sending

Example fix

# before
content=["hi", 42]
# after
content=["hi", {"type":"text","text":"42"}]
Defensive patterns

Strategy: type-guard

Validate before calling

for m in messages:
    c = m.get("content", "")
    if isinstance(c, list):
        assert all(isinstance(p, (str, dict)) for p in c), [type(p).__name__ for p in c]

Type guard

def parts_ok(c):
    return not isinstance(c, list) or all(isinstance(p, (str, dict)) for p in c)

Prevention

When it happens

Trigger: content=["ok", 42] or content=[["nested"]] — a list element that is neither str nor dict.

Common situations: Clients that build content lists programmatically and accidentally include raw values or nested lists.

Related errors


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