{"record":{"id":"c88d486ba45ea0c8","repo":"sgl-project/sglang","slug":"task-requires-at-least-one-message-with-role-us","errorCode":null,"errorMessage":"`task` requires at least one message with role='user' or 'developer'.","messagePattern":"`task` requires at least one message with role='user' or 'developer'\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/entrypoints/openai/encoding_dsv4.py","lineNumber":254,"sourceCode":"        thinking_end_token=thinking_end_token,\n    )\n\n\ndef find_last_user_index(messages: List[Dict[str, Any]]) -> int:\n    \"\"\"Find the index of the last user/developer message.\"\"\"\n    last_user_index = -1\n    for idx in range(len(messages) - 1, -1, -1):\n        if messages[idx].get(\"role\") in [\"user\", \"developer\"]:\n            last_user_index = idx\n            break\n    return last_user_index\n\n\ndef attach_task_to_last_user_message(messages: List[Dict[str, Any]], task: str) -> None:\n    \"\"\"Set `task` on the most recent user/developer message; raise if none exists.\"\"\"\n    idx = find_last_user_index(messages)\n    if idx == -1:\n        raise ValueError(\n            \"`task` requires at least one message with role='user' or 'developer'.\"\n        )\n    messages[idx][\"task\"] = task\n\n\n# ============================================================\n# Message Rendering\n# ============================================================\n\n\ndef render_message(\n    index: int,\n    messages: List[Dict[str, Any]],\n    thinking_mode: str,\n    drop_thinking: bool = True,\n    reasoning_effort: Optional[str] = None,\n    reasoning_effort_profile: str = \"preview\",\n) -> str:","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/entrypoints/openai/encoding_dsv4.py#L236-L272","documentation":"attach_task_to_last_user_message needs an existing user/developer message to attach the task to; find_last_user_index returned -1, so ValueError is raised.","triggerScenarios":"Passing a `task` extra in a chat completion request whose messages contain only assistant/system/tool roles.","commonSituations":"Sending a task instruction with an assistant-prefill-only conversation; programmatic message builders that omit the user turn when a task is set.","solutions":["Include at least one message with role 'user' or 'developer' in the request.","Or drop the `task` field if no user message is intended.","Ensure the task is not attached after history truncation removed the user turn."],"exampleFix":"# before\nmessages=[{\"role\":\"assistant\",\"content\":\"...\"}], extra_body={\"task\":\"summarize\"}\n# after\nmessages=[{\"role\":\"user\",\"content\":\"doc...\"},{\"role\":\"assistant\",\"content\":\"...\"}], extra_body={\"task\":\"summarize\"}","handlingStrategy":"validation","validationCode":"if task and not any(m['role'] in ('user','developer') for m in messages):\n    messages.insert(0,{'role':'user','content':task})","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always include a user turn when using `task`."],"tags":["deepseek-v4","task","message-validation"],"backgroundTag":"request-message-validation","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}