{"record":{"id":"c1e342375e240db3","repo":"oobabooga/textgen","slug":"messages-is-required","errorCode":null,"errorMessage":"messages is required","messagePattern":"messages is required","errorType":"exception","errorClass":"InvalidRequestError","httpStatus":400,"severity":"error","filePath":"modules/api/completions.py","lineNumber":485,"sourceCode":"    if not user_input_last:\n        user_input = \"\"\n\n    return user_input, system_message, {\n        'internal': chat_dialogue,\n        'visible': copy.deepcopy(chat_dialogue),\n        'messages': history  # Store original messages for multimodal models\n    }\n\n\ndef chat_completions_common(body: dict, is_legacy: bool = False, stream=False, prompt_only=False, stop_event=None) -> dict:\n    if body.get('functions', []):\n        raise InvalidRequestError(message=\"functions is not supported.\", param='functions')\n\n    if body.get('function_call', ''):\n        raise InvalidRequestError(message=\"function_call is not supported.\", param='function_call')\n\n    if 'messages' not in body:\n        raise InvalidRequestError(message=\"messages is required\", param='messages')\n\n    tools = None\n    if 'tools' in body and body['tools'] is not None and isinstance(body['tools'], list) and body['tools']:\n        tools = validateTools(body['tools'])  # raises InvalidRequestError if validation fails\n\n    tool_choice = body.get('tool_choice', None)\n    if tool_choice == \"none\":\n        tools = None  # Disable tool detection entirely\n\n    messages = body['messages']\n    for m in messages:\n        if 'role' not in m:\n            raise InvalidRequestError(message=\"messages: missing role\", param='messages')\n        elif m['role'] == 'function':\n            raise InvalidRequestError(message=\"role: function is not supported.\", param='messages')\n\n        # Handle multimodal content validation\n        content = m.get('content')","sourceCodeStart":467,"sourceCodeEnd":503,"githubUrl":"https://github.com/oobabooga/textgen/blob/ed888c71f221df552750e1834b3654abab8ae345/modules/api/completions.py#L467-L503","documentation":"chat_completions_common requires a 'messages' key in the chat completions request body; it is the minimal contract for building the prompt. Missing it raises InvalidRequestError (400) with param='messages', matching the OpenAI API error shape.","triggerScenarios":"POST /v1/chat/completions with a body that has no 'messages' key, or where 'messages' was dropped by a serialization bug (e.g. sending {\"model\": \"x\", \"prompt\": \"hi\"} because the caller reused text-completions style params).","commonSituations":"Copy-pasting a /v1/completions curl example against /v1/chat/completions; a client library that only includes optional keys when non-empty and passing messages=None; JSON typos like 'message' or 'Messages'.","solutions":["Include a messages array: {\"model\": \"...\", \"messages\": [{\"role\": \"user\", \"content\": \"hello\"}]}.","If you intended plain text completion, use /v1/completions with 'prompt' instead of the chat endpoint.","Check the client is not stripping None/empty fields before sending."],"exampleFix":"# before\ncurl $URL/v1/chat/completions -d '{\"model\": \"model-x\"}'\n\n# after\ncurl $URL/v1/chat/completions -d '{\"model\": \"model-x\", \"messages\": [{\"role\": \"user\", \"content\": \"hello\"}]}'","handlingStrategy":"validation","validationCode":"def valid_chat_body(body: dict) -> bool:\n    msgs = body.get('messages')\n    return isinstance(msgs, list) and len(msgs) > 0","typeGuard":"def has_messages(body: dict) -> bool:\n    return isinstance(body.get('messages'), list) and len(body['messages']) > 0","tryCatchPattern":"try:\n    resp = client.chat.completions.create(**body)\nexcept openai.BadRequestError as e:\n    if e.code == 'messages' or 'messages is required' in str(e):\n        raise ValueError('Chat request built without messages; check message assembly')\n    raise","preventionTips":["Assert messages is a non-empty list in your request builder.","Don't reuse /v1/completions payloads against /v1/chat/completions.","Fail fast client-side on empty conversations."],"tags":["openai-api","chat-completions","validation","required-field"],"backgroundTag":null,"analyzedSha":"ed888c71f221df552750e1834b3654abab8ae345","analyzedAt":"2026-08-15T05:24:21.000Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}