sgl-project/sglang · error · ValueError

Unknown role: {msg_role}

Error message

Unknown role: {msg_role}

What it means

generate_chat_conv only accepts roles 'system', 'user', and 'assistant'. Any other role string in request.messages hits the final else and raises ValueError with the offending role.

Source

Thrown at python/sglang/srt/parser/conversation.py:709

                    )
                conv.append_message(conv.roles[0], real_content)
        elif msg_role == "assistant":
            parsed_content = ""
            if isinstance(message.content, str):
                parsed_content = message.content
            elif isinstance(message.content, list):
                if (
                    len(message.content) != 1
                    or getattr(message.content[0], "type", None) != "text"
                ):
                    raise ValueError(
                        "The assistant's response should be a single text."
                    )
                else:
                    parsed_content = getattr(message.content[0], "text", "")
            conv.append_message(conv.roles[1], parsed_content)
        else:
            raise ValueError(f"Unknown role: {msg_role}")

    # Add a blank message for the assistant.
    conv.append_message(conv.roles[1], None)
    return conv


# llama2 template
# reference: https://github.com/lm-sys/FastChat/blob/main/fastchat/conversation.py
# reference: https://github.com/facebookresearch/llama/blob/1a240688810f8036049e8da36b073f63d2ac552c/llama/generation.py#L212
register_conv_template(
    Conversation(
        name="llama-2",
        system_template="[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n",
        roles=("[INST]", "[/INST]"),
        sep_style=SeparatorStyle.LLAMA2,
        sep=" ",
        sep2=" </s><s>",
        stop_str=["[INST]", "[/INST]", "<<SYS>>", "<</SYS>>"],

View on GitHub (pinned to 0132848349)

Solutions

  1. Restrict roles to system/user/assistant exactly (lowercase)
  2. Remove tool/function messages before calling, or use a model/template that supports them

Example fix

# before
messages=[{"role":"Tool","content":"result"}]
# after
messages=[{"role":"assistant","content":"tool result: ..."}]
Defensive patterns

Strategy: validation

Validate before calling

allowed = {"system", "user", "assistant"}
assert all(m.get("role") in allowed for m in messages), [m.get("role") for m in messages]

Type guard

def valid_roles(msgs): return all(m.get("role") in {"system","user","assistant"} for m in msgs)

Try / catch

try: conv = generate_chat_conv(req)
except ValueError as e: if str(e).startswith("Unknown role"): map/strip offending roles

Prevention

When it happens

Trigger: Sending a message with role "tool", "function", or a typo like "use"/"Assistant" (case-sensitive) in a chat request.

Common situations: Porting OpenAI tool-message conversations to a model whose template has no tool role; role capitalization or whitespace differences from client SDKs.

Related errors


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