{"record":{"id":"cc379b011ade9351","repo":"BerriAI/litellm","slug":"message-i-must-have-a-content-field-cc379b","errorCode":null,"errorMessage":"Message {i} must have a 'content' field","messagePattern":"Message (.+?) must have a 'content' field","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/bedrock/count_tokens/transformation.py","lineNumber":277,"sourceCode":"\n        if input_type == \"converse\":\n            # Validate Converse format (messages-based)\n            messages: Final = request_data.get(\"messages\", [])\n            if not messages:\n                raise ValueError(\"messages parameter is required for Converse input\")\n\n            if not isinstance(messages, list):\n                raise ValueError(\"messages must be a list\")\n\n            for i, message in enumerate(messages):\n                if not isinstance(message, dict):\n                    raise ValueError(f\"Message {i} must be a dictionary\")\n\n                if \"role\" not in message:\n                    raise ValueError(f\"Message {i} must have a 'role' field\")\n\n                if \"content\" not in message:\n                    raise ValueError(f\"Message {i} must have a 'content' field\")\n        else:\n            # For InvokeModel format, we need at least some content to count tokens\n            # The content structure varies by model, so we do minimal validation\n            if len(request_data) <= 1:  # Only has 'model' field\n                raise ValueError(\"Request must contain content to count tokens\")\n","sourceCodeStart":259,"sourceCodeEnd":283,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/bedrock/count_tokens/transformation.py#L259-L283","documentation":"Raised when message i in a Converse-format count-tokens request is a dict with a 'role' but no 'content' key. Content is what actually gets tokenized, so a message without it cannot contribute to a count; the error names the index of the offending message.","triggerScenarios":"messages=[{'role': 'user'}] (role-only); content stored under different keys ('text', 'parts', 'blocks'); messages constructed from templates where the content placeholder was never filled and the key was omitted entirely.","commonSituations":"Migrating from APIs whose content field is named 'text'; emptying a message for moderation/redaction and deleting the key instead of setting it to '' or []; optional-content code paths that use dict.pop('content').","solutions":["Add content to message i, even if empty: {'role': 'user', 'content': ''} (empty content passes this validator)","Rename alternate keys to 'content' when porting from other formats","When deleting content for redaction, keep the key with an empty value rather than removing it"],"exampleFix":"# before\nmessages = [{'role': 'user'}]\n\n# after\nmessages = [{'role': 'user', 'content': 'Hello, world'}]","handlingStrategy":"type-guard","validationCode":"def assert_content_present(messages: list[dict]) -> None:\n    for i, m in enumerate(messages):\n        if 'content' not in m:\n            raise ValueError(f'message {i} missing content (use \"\" if empty by design)')","typeGuard":"def messages_have_content(messages: list) -> bool:\n    return all(isinstance(m, dict) and 'content' in m for m in messages)","tryCatchPattern":"try:\n    handler.count_tokens(body)\nexcept ValueError as e:\n    m = re.search(r\"Message (\\d+) must have a 'content' field\", str(e))\n    if m:\n        i = int(m.group(1))\n        body['messages'][i].setdefault('content', '')\n        handler.count_tokens(body)\n    else:\n        raise","preventionTips":["Map 'text'/'parts' fields to 'content' in your provider adapter","Keep empty content as '' instead of deleting the key","Build messages via a constructor that always sets both role and content"],"tags":["bedrock","count-tokens","validation","messages","missing-field"],"backgroundTag":"schema-validation-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}