Implementation:Mistralai Client python Files Upload
| Knowledge Sources | |
|---|---|
| Domains | Fine_Tuning, File_Management |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Concrete tool for uploading training data files to Mistral AI servers provided by the Files resource.
Description
The Files.upload() and Files.upload_async() methods upload a file to Mistral's servers using multipart form encoding. The file is wrapped in a File object (with file_name and content as bytes). An optional purpose parameter categorizes the file (e.g., "fine-tune"). The method returns an UploadFileOut object with the file's id (needed for job creation), filename, bytes count, and created_at timestamp.
Usage
Call client.files.upload() with a File object containing the JSONL training data. Use the returned id in TrainingFile objects for fine_tuning.jobs.create().
Code Reference
Source Location
- Repository: client-python
- File: src/mistralai/client/files.py
- Lines: L22-122 (sync), L124-224 (async)
Signature
class Files:
def upload(
self,
*,
file: Union[File, FileTypedDict],
purpose: Optional[FilePurpose] = None,
) -> UploadFileOut:
...
async def upload_async(
self,
*,
file: Union[File, FileTypedDict],
purpose: Optional[FilePurpose] = None,
) -> UploadFileOut:
...
Import
from mistralai import Mistral
from mistralai.models import File
# Access via: client.files.upload(...)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| file | File | Yes | File object with file_name and content (bytes) |
| purpose | Optional[FilePurpose] | No | File purpose (e.g., "fine-tune") |
Outputs
| Name | Type | Description |
|---|---|---|
| result | UploadFileOut | Contains id, filename, bytes, created_at |
| result.id | str | File ID for use in fine-tuning jobs |
Usage Examples
Upload Training File
from mistralai import Mistral
from mistralai.models import File
client = Mistral(api_key="your-key")
# Read and upload training data
with open("train.jsonl", "rb") as f:
uploaded = client.files.upload(
file=File(file_name="train.jsonl", content=f.read()),
purpose="fine-tune",
)
print(f"File ID: {uploaded.id}")
print(f"Size: {uploaded.bytes} bytes")