BerriAI/litellm · error · ValueError
File data is required
Error message
File data is required
What it means
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.
Source
Thrown at litellm/llms/anthropic/files/transformation.py:141
return optional_params
def transform_create_file_request(
self,
model: str,
create_file_data: CreateFileRequest,
optional_params: dict,
litellm_params: dict,
) -> dict:
"""
Transform to multipart form data for Anthropic file upload.
Anthropic expects: POST /v1/files with multipart form-data
- file: the file content
- purpose: "messages" (defaults to "messages" if not provided)
"""
file_data: Final = create_file_data.get("file")
if file_data is None:
raise ValueError("File data is required")
extracted: Final = extract_file_data(file_data)
filename: Final = extracted["filename"] or f"file_{int(time.time())}"
content: Final = extracted["content"]
content_type: Final = extracted.get("content_type", "application/octet-stream")
purpose: Final = create_file_data.get("purpose", "messages")
return {
"file": (filename, content, content_type),
"purpose": (None, purpose),
}
def transform_create_file_response(
self,
model: str | None,
raw_response: httpx.Response,
logging_obj: LiteLLMLoggingObj,View on GitHub (pinned to 6c2dcb801b)
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.
Example fix
# before
create_file_data = {"filename": "data.jsonl", "content": file_bytes, "purpose": "messages"}
# after
create_file_data = {"file": file_bytes, "purpose": "messages"}
# or: {"file": ("data.jsonl", file_bytes, "application/json")} Defensive patterns
Strategy: validation
Validate before calling
def build_create_file_data(file: object) -> dict:
if file is None:
raise ValueError("file payload is required")
return {"file": file, "purpose": "messages"} Type guard
def has_file_payload(create_file_data: object) -> bool:
return isinstance(create_file_data, dict) and create_file_data.get("file") is not None Try / catch
try:
resp = litellm.create_file(file=f, purpose="messages")
except ValueError as e:
if "File data is required" in str(e):
return http_error(400, "multipart 'file' field is required")
raise Prevention
- 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.
When it happens
Trigger: 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': ...}.
Common situations: 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.
Related errors
- Unable to parse anthropic file message: {message}
- Either file_data or file_id must be present in the file mess
- file_id is required in file_content_request
- file content is required
- file is required
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/fdd48e3d14c14068.
Report an issue: GitHub.