{"record":{"id":"49ff338f6b7f2abf","repo":"oobabooga/textgen","slug":"missing-required-input","errorCode":null,"errorMessage":"Missing required input","messagePattern":"Missing required input","errorType":"exception","errorClass":"InvalidRequestError","httpStatus":400,"severity":"error","filePath":"modules/api/completions.py","lineNumber":823,"sourceCode":"    if prompt_str not in body or body[prompt_str] is None:\n        if 'messages' in body:\n            # Convert messages format to prompt for completions endpoint\n            prompt_text = \"\"\n            for message in body.get('messages', []):\n                if isinstance(message, dict) and 'content' in message:\n                    # Extract text content from multimodal messages\n                    content = message['content']\n                    if isinstance(content, str):\n                        prompt_text += content\n                    elif isinstance(content, list):\n                        for item in content:\n                            if isinstance(item, dict) and item.get('type') == 'text':\n                                prompt_text += item.get('text', '')\n\n            # Allow empty prompts for image-only requests\n            body[prompt_str] = prompt_text\n        else:\n            raise InvalidRequestError(\"Missing required input\", param=prompt_str)\n\n    # common params\n    generate_params = process_parameters(body, is_legacy=is_legacy)\n    max_tokens = generate_params['max_new_tokens']\n    if max_tokens is None:\n        generate_params['max_new_tokens'] = 512\n        generate_params['auto_max_new_tokens'] = True\n        max_tokens = 512\n    elif max_tokens < 0:\n        raise InvalidRequestError(message=\"max_tokens must be greater than or equal to 0.\", param=\"max_tokens\")\n    elif max_tokens == 0 and body.get('logprobs') is None:\n        raise InvalidRequestError(message=\"max_tokens is 0 but no logprobs parameter was specified.\", param=\"max_tokens\")\n\n    generate_params['stream'] = stream\n    if stop_event is not None:\n        generate_params['stop_event'] = stop_event\n    requested_model = generate_params.pop('model')\n    logprob_proc = generate_params.pop('logprob_proc', None)","sourceCodeStart":805,"sourceCodeEnd":841,"githubUrl":"https://github.com/oobabooga/textgen/blob/ed888c71f221df552750e1834b3654abab8ae345/modules/api/completions.py#L805-L841","documentation":"In the text completions path (completions_common), the request must include either a prompt ('prompt' or legacy 'context') or a 'messages' array (from which a prompt is extracted). When neither is present, InvalidRequestError (400) is raised with param=prompt_str pointing at the missing field name.","triggerScenarios":"POST /v1/completions with a body lacking both 'prompt' and 'messages' (e.g. only model + sampling params), or the legacy endpoint without 'context'/'prompt'. Sending a messages array with only image items yields an empty-string prompt, which is allowed; total absence is not.","commonSituations":"Sending chat-style bodies without messages to /v1/completions; a client that conditionally includes prompt only when non-empty and passing prompt=None; wrong endpoint chosen for the payload shape.","solutions":["Include a prompt: {\"model\": ..., \"prompt\": \"Once upon a time\"}.","Or include a messages array; its text content is concatenated into the prompt (empty allowed for image-only requests).","For chat use-cases, call /v1/chat/completions instead.","Verify the client is not dropping falsy fields (empty-string prompt counts as present)."],"exampleFix":"# before\ncurl $URL/v1/completions -d '{\"model\": \"x\", \"temperature\": 0.7}'\n\n# after\ncurl $URL/v1/completions -d '{\"model\": \"x\", \"temperature\": 0.7, \"prompt\": \"Once upon a time\"}'","handlingStrategy":"validation","validationCode":"def has_prompt_input(body: dict) -> bool:\n    return any(k in body and body[k] is not None for k in ('prompt', 'context', 'messages'))","typeGuard":"def is_completion_ready(body: dict) -> bool:\n    return isinstance(body.get('prompt'), (str, list)) or isinstance(body.get('messages'), list)","tryCatchPattern":"try:\n    resp = client.completions.create(model=m, prompt=p)\nexcept openai.BadRequestError as e:\n    if 'Missing required input' in str(e):\n        raise ValueError('completion body has neither prompt nor messages')\n    raise","preventionTips":["Assert prompt is present in your completion builder.","Route chat-shaped payloads to /v1/chat/completions.","Don't let clients strip empty-string prompts (empty string is valid and allowed)."],"tags":["openai-api","completions","required-field","validation"],"backgroundTag":null,"analyzedSha":"ed888c71f221df552750e1834b3654abab8ae345","analyzedAt":"2026-08-15T05:24:21.000Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}