{"record":{"id":"c8def54ee54c7333","repo":"oobabooga/textgen","slug":"messages-missing-text-in-content-item","errorCode":null,"errorMessage":"messages: missing text in content item","messagePattern":"messages: missing text in content item","errorType":"exception","errorClass":"InvalidRequestError","httpStatus":400,"severity":"error","filePath":"modules/api/completions.py","lineNumber":519,"sourceCode":"\n        # Handle multimodal content validation\n        content = m.get('content')\n        if content is None:\n            # OpenAI allows content: null on assistant messages when tool_calls is present\n            if m['role'] == 'assistant' and m.get('tool_calls'):\n                m['content'] = ''\n            else:\n                raise InvalidRequestError(message=\"messages: missing content\", param='messages')\n\n        # Validate multimodal content structure\n        if isinstance(content, list):\n            for item in content:\n                if not isinstance(item, dict) or 'type' not in item:\n                    raise InvalidRequestError(message=\"messages: invalid content item format\", param='messages')\n                if item['type'] not in ['text', 'image_url']:\n                    raise InvalidRequestError(message=\"messages: unsupported content type\", param='messages')\n                if item['type'] == 'text' and 'text' not in item:\n                    raise InvalidRequestError(message=\"messages: missing text in content item\", param='messages')\n                if item['type'] == 'image_url' and ('image_url' not in item or 'url' not in item['image_url']):\n                    raise InvalidRequestError(message=\"messages: missing image_url in content item\", param='messages')\n\n    # Chat Completions\n    object_type = 'chat.completion' if not stream else 'chat.completion.chunk'\n    created_time = int(time.time())\n    cmpl_id = \"chatcmpl-%d\" % (int(time.time() * 1000000000))\n    resp_list = 'data' if is_legacy else 'choices'\n\n    # generation parameters\n    generate_params = process_parameters(body, is_legacy=is_legacy)\n    if stop_event is not None:\n        generate_params['stop_event'] = stop_event\n    continue_ = body['continue_']\n\n    # Instruction template\n    if body['instruction_template_str']:\n        instruction_template_str = body['instruction_template_str']","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/oobabooga/textgen/blob/ed888c71f221df552750e1834b3654abab8ae345/modules/api/completions.py#L501-L537","documentation":"A content item with type 'text' must include a 'text' key holding the string. Its absence raises InvalidRequestError (400, param='messages') during multimodal content validation.","triggerScenarios":"POST /v1/chat/completions with {\"type\": \"text\"} or {\"type\": \"text\", \"content\": \"hi\"} (wrong key name) inside a content list.","commonSituations":"Using 'content' or 'value' instead of 'text' as the key; constructing items from templates where the text field is conditionally omitted; partial conversion from another API's block schema.","solutions":["Include the text key: {\"type\": \"text\", \"text\": \"hello\"}.","If the field may be empty, still send an empty string rather than omitting the key.","Use a client-side content-item builder that always sets both keys."],"exampleFix":"# before\n{\"type\": \"text\", \"content\": \"describe this image\"}\n\n# after\n{\"type\": \"text\", \"text\": \"describe this image\"}","handlingStrategy":"validation","validationCode":"def text_item(text: str) -> dict:\n    return {'type': 'text', 'text': text or ''}","typeGuard":"def is_valid_text_item(i) -> bool:\n    return isinstance(i, dict) and i.get('type') == 'text' and isinstance(i.get('text'), str)","tryCatchPattern":"try:\n    resp = client.chat.completions.create(model=m, messages=msgs)\nexcept openai.BadRequestError as e:\n    if 'missing text in content item' in str(e):\n        raise ValueError('content builder omitted text field')\n    raise","preventionTips":["Never hand-write {'type': 'text'} inline; use a builder that sets both keys.","Use 'text', not 'content' or 'value', as the payload key.","Allow empty strings but never omit the key."],"tags":["openai-api","chat-completions","multimodal","validation"],"backgroundTag":null,"analyzedSha":"ed888c71f221df552750e1834b3654abab8ae345","analyzedAt":"2026-08-15T05:24:21.000Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}