{"record":{"id":"a281b18e7d77ab4d","repo":"sgl-project/sglang","slug":"unknown-role-msg-role","errorCode":null,"errorMessage":"Unknown role: {msg_role}","messagePattern":"Unknown role: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/parser/conversation.py","lineNumber":709,"sourceCode":"                    )\n                conv.append_message(conv.roles[0], real_content)\n        elif msg_role == \"assistant\":\n            parsed_content = \"\"\n            if isinstance(message.content, str):\n                parsed_content = message.content\n            elif isinstance(message.content, list):\n                if (\n                    len(message.content) != 1\n                    or getattr(message.content[0], \"type\", None) != \"text\"\n                ):\n                    raise ValueError(\n                        \"The assistant's response should be a single text.\"\n                    )\n                else:\n                    parsed_content = getattr(message.content[0], \"text\", \"\")\n            conv.append_message(conv.roles[1], parsed_content)\n        else:\n            raise ValueError(f\"Unknown role: {msg_role}\")\n\n    # Add a blank message for the assistant.\n    conv.append_message(conv.roles[1], None)\n    return conv\n\n\n# llama2 template\n# reference: https://github.com/lm-sys/FastChat/blob/main/fastchat/conversation.py\n# reference: https://github.com/facebookresearch/llama/blob/1a240688810f8036049e8da36b073f63d2ac552c/llama/generation.py#L212\nregister_conv_template(\n    Conversation(\n        name=\"llama-2\",\n        system_template=\"[INST] <<SYS>>\\n{system_message}\\n<</SYS>>\\n\\n\",\n        roles=(\"[INST]\", \"[/INST]\"),\n        sep_style=SeparatorStyle.LLAMA2,\n        sep=\" \",\n        sep2=\" </s><s>\",\n        stop_str=[\"[INST]\", \"[/INST]\", \"<<SYS>>\", \"<</SYS>>\"],","sourceCodeStart":691,"sourceCodeEnd":727,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/parser/conversation.py#L691-L727","documentation":"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.","triggerScenarios":"Sending a message with role \"tool\", \"function\", or a typo like \"use\"/\"Assistant\" (case-sensitive) in a chat request.","commonSituations":"Porting OpenAI tool-message conversations to a model whose template has no tool role; role capitalization or whitespace differences from client SDKs.","solutions":["Restrict roles to system/user/assistant exactly (lowercase)","Remove tool/function messages before calling, or use a model/template that supports them"],"exampleFix":"# before\nmessages=[{\"role\":\"Tool\",\"content\":\"result\"}]\n# after\nmessages=[{\"role\":\"assistant\",\"content\":\"tool result: ...\"}]","handlingStrategy":"validation","validationCode":"allowed = {\"system\", \"user\", \"assistant\"}\nassert all(m.get(\"role\") in allowed for m in messages), [m.get(\"role\") for m in messages]","typeGuard":"def valid_roles(msgs): return all(m.get(\"role\") in {\"system\",\"user\",\"assistant\"} for m in msgs)","tryCatchPattern":"try: conv = generate_chat_conv(req)\nexcept ValueError as e: if str(e).startswith(\"Unknown role\"): map/strip offending roles","preventionTips":["Lowercase and trim role strings client-side","Inline tool results into assistant/user text for non-tool templates"],"tags":["chat-api","role-validation"],"backgroundTag":"invalid-request-payload","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}