{"record":{"id":"afb4d736e27a12b8","repo":"BerriAI/litellm","slug":"file-id-and-file-data-are-both-none","errorCode":null,"errorMessage":"file_id and file_data are both None","messagePattern":"file_id and file_data are both None","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/prompt_templates/common_utils.py","lineNumber":1376,"sourceCode":"    Migrate file to image_url\n    \"\"\"\n    from litellm.types.llms.openai import (\n        ChatCompletionImageObject,\n        ChatCompletionImageUrlObject,\n    )\n\n    file_sub: Final = message.get(\"file\")\n    if file_sub is None:\n        raise litellm.BadRequestError(\n            message=\"Content block has type='file' but is missing the required 'file' field\",\n            model=None,\n            llm_provider=None,\n        )\n    file_id: Final = file_sub.get(\"file_id\")\n    file_data: Final = file_sub.get(\"file_data\")\n    format: Final = file_sub.get(\"format\")\n    if not file_id and not file_data:\n        raise ValueError(\"file_id and file_data are both None\")\n    image_url_object: Final = ChatCompletionImageObject(\n        type=\"image_url\",\n        image_url=ChatCompletionImageUrlObject(\n            url=cast(str, file_id or file_data),\n        ),\n    )\n    if format and isinstance(image_url_object[\"image_url\"], dict):\n        image_url_object[\"image_url\"][\"format\"] = format\n    return image_url_object\n\n\ndef get_last_user_message(messages: list[AllMessageValues]) -> str | None:\n    \"\"\"\n    Get the last consecutive block of messages from the user.\n\n    Example:\n    messages = [\n        {\"role\": \"user\", \"content\": \"Hello, how are you?\"},","sourceCodeStart":1358,"sourceCodeEnd":1394,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/prompt_templates/common_utils.py#L1358-L1394","documentation":"When converting an OpenAI-style {'type':'file'} content block (used by Anthropic-style document inputs), litellm reads the 'file' sub-dict and requires either 'file_id' or 'file_data' to be truthy. The code first raises BadRequestError if the 'file' key itself is missing; this ValueError fires when the 'file' dict exists but both file_id and file_data are absent or empty — there is nothing to turn into the image_url/file object it builds next.","triggerScenarios":"Sending {'type':'file','file':{}} or {'type':'file','file':{'format':'pdf'}} with no id/data; template code building file blocks conditionally where both branches failed to set a value; None values for both keys.","commonSituations":"Dynamically constructed messages where the source field is None; frontend forms letting users attach a file but the upload failed silently, leaving an empty file block; refactors renaming 'file_id' to something else (e.g. 'id') breaking the key contract.","solutions":["Include file_id (a URL or provider file reference) or file_data (bytes/Path/tuple) in the file sub-dict.","Validate messages before sending: drop or reject file blocks whose file dict has neither key.","Check your message-building code for the exact key names — they must be 'file_id' / 'file_data' inside 'file'."],"exampleFix":"// before\n{'type':'file','file':{'format':'pdf'}}\n\n# after\n{'type':'file','file':{'file_id':'https://example.com/doc.pdf','format':'pdf'}}","handlingStrategy":"validation","validationCode":"def file_block_valid(block: dict) -> bool:\n    f = block.get('file')\n    return isinstance(f, dict) and bool(f.get('file_id') or f.get('file_data'))","typeGuard":null,"tryCatchPattern":"try:\n    resp = litellm.completion(model=m, messages=msgs)\nexcept ValueError as e:\n    if 'file_id and file_data are both None' in str(e):\n        msgs = strip_or_fill_file_blocks(msgs)\n        resp = litellm.completion(model=m, messages=msgs)\n    else:\n        raise","preventionTips":["Validate file content blocks (file_id or file_data present) before building the request.","When constructing file blocks dynamically, fail loudly at build time if the source is None.","Use the exact key names 'file_id'/'file_data' inside the 'file' dict."],"tags":["files","message-format","input-validation","anthropic"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}