{"record":{"id":"fdd48e3d14c14068","repo":"BerriAI/litellm","slug":"file-data-is-required","errorCode":null,"errorMessage":"File data is required","messagePattern":"File data is required","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/anthropic/files/transformation.py","lineNumber":141,"sourceCode":"        return optional_params\n\n    def transform_create_file_request(\n        self,\n        model: str,\n        create_file_data: CreateFileRequest,\n        optional_params: dict,\n        litellm_params: dict,\n    ) -> dict:\n        \"\"\"\n        Transform to multipart form data for Anthropic file upload.\n\n        Anthropic expects: POST /v1/files with multipart form-data\n        - file: the file content\n        - purpose: \"messages\" (defaults to \"messages\" if not provided)\n        \"\"\"\n        file_data: Final = create_file_data.get(\"file\")\n        if file_data is None:\n            raise ValueError(\"File data is required\")\n\n        extracted: Final = extract_file_data(file_data)\n        filename: Final = extracted[\"filename\"] or f\"file_{int(time.time())}\"\n        content: Final = extracted[\"content\"]\n        content_type: Final = extracted.get(\"content_type\", \"application/octet-stream\")\n\n        purpose: Final = create_file_data.get(\"purpose\", \"messages\")\n\n        return {\n            \"file\": (filename, content, content_type),\n            \"purpose\": (None, purpose),\n        }\n\n    def transform_create_file_response(\n        self,\n        model: str | None,\n        raw_response: httpx.Response,\n        logging_obj: LiteLLMLoggingObj,","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/anthropic/files/transformation.py#L123-L159","documentation":"Multipart-building validation for Anthropic file upload. The transformation expects the file content under the 'file' key of create_file_data; a None value raises immediately. Note this check tests identity with None — an empty bytes object passes here and fails server-side instead.","triggerScenarios":"Calling the Anthropic files transformation with create_file_data missing the 'file' key or file=None — e.g. passing {'filename': ..., 'content': ...} (OpenAI-style structured body) instead of {'file': ...}.","commonSituations":"Reusing OpenAI file-upload payload shapes; code that reads a path but passes the variable before assignment; optional-file flows where the file handle is None.","solutions":["Provide the file under the 'file' key: {'file': file_obj_or_bytes, 'purpose': 'messages'}.","If you have raw bytes and a filename, a (filename, bytes, content_type) tuple or File-like object works — extract_file_data handles common shapes.","Assert the file argument is not None before calling upload."],"exampleFix":"# before\ncreate_file_data = {\"filename\": \"data.jsonl\", \"content\": file_bytes, \"purpose\": \"messages\"}\n\n# after\ncreate_file_data = {\"file\": file_bytes, \"purpose\": \"messages\"}\n# or: {\"file\": (\"data.jsonl\", file_bytes, \"application/json\")}","handlingStrategy":"validation","validationCode":"def build_create_file_data(file: object) -> dict:\n    if file is None:\n        raise ValueError(\"file payload is required\")\n    return {\"file\": file, \"purpose\": \"messages\"}","typeGuard":"def has_file_payload(create_file_data: object) -> bool:\n    return isinstance(create_file_data, dict) and create_file_data.get(\"file\") is not None","tryCatchPattern":"try:\n    resp = litellm.create_file(file=f, purpose=\"messages\")\nexcept ValueError as e:\n    if \"File data is required\" in str(e):\n        return http_error(400, \"multipart 'file' field is required\")\n    raise","preventionTips":["Always use the 'file' key for Anthropic uploads; do not port OpenAI body shapes.","Assert the file handle/bytes variable is assigned before building the payload.","Guard optional-file flows: skip the upload call when the file is None."],"tags":["anthropic","files","upload","validation"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}