Python Client SDK for the Mistral AI API with chat completions, embeddings, fine-tuning, and agent capabilities.
—
Upload, manage, and process files for use with fine-tuning, agents, and other AI capabilities. The files API provides comprehensive file management including upload, download, metadata retrieval, and deletion.
Upload files to the Mistral AI platform for use with various services. Maximum file size is 512 MB. Fine-tuning API only supports .jsonl files.
def upload(
file: Union[File, FileTypedDict],
purpose: Optional[FilePurpose] = None,
**kwargs
) -> UploadFileOut:
"""
Upload a file that can be used across various endpoints.
Parameters:
- file: The File object to be uploaded
- purpose: File purpose for filtering and organization
Returns:
UploadFileOut with file metadata and ID
"""List uploaded files with optional filtering and pagination.
def list(
page: Optional[int] = 0,
page_size: Optional[int] = 100,
sample_type: Optional[List[SampleType]] = None,
source: Optional[List[Source]] = None,
search: Optional[str] = None,
purpose: Optional[FilePurpose] = None,
**kwargs
) -> ListFilesOut:
"""
Returns a list of files that belong to the user's organization.
Parameters:
- page: Page number for pagination (default: 0)
- page_size: Number of files per page (default: 100)
- sample_type: Filter by sample types
- source: Filter by source
- search: Search query string for filtering files
- purpose: Filter by file purpose
Returns:
ListFilesOut with file metadata list
"""Get detailed information about a specific file.
def retrieve(file_id: str, **kwargs) -> RetrieveFileOut:
"""
Retrieve file metadata.
Parameters:
- file_id: Unique identifier of the file
Returns:
RetrieveFileOut with detailed file information
"""Download file content from the platform.
def download(file_id: str, **kwargs) -> httpx.Response:
"""
Download a file (returns raw binary data as httpx.Response).
Parameters:
- file_id: The ID of the file to download
Returns:
httpx.Response with binary file content
"""Delete files that are no longer needed.
def delete(file_id: str, **kwargs) -> DeleteFileOut:
"""
Delete a file.
Parameters:
- file_id: Unique identifier of the file to delete
Returns:
DeleteFileOut with deletion confirmation
"""Generate signed URLs for secure file access.
def get_signed_url(
file_id: str,
expiry: Optional[int] = 24,
**kwargs
) -> FileSignedURL:
"""
Get a signed URL for accessing the file.
Parameters:
- file_id: The ID of the file
- expiry: Number of hours before the URL becomes invalid (default: 24)
Returns:
FileSignedURL with secure access URL
"""from mistralai import Mistral
client = Mistral(api_key="your-api-key")
# Upload a JSONL file for fine-tuning
with open("training_data.jsonl", "rb") as f:
upload_result = client.files.upload(
file=f,
purpose="fine-tune",
filename="my_training_data.jsonl"
)
print(f"Uploaded file ID: {upload_result.id}")
print(f"Filename: {upload_result.filename}")
print(f"Size: {upload_result.bytes} bytes")# Upload using file path
upload_result = client.files.upload(
file="/path/to/document.pdf",
purpose="assistants"
)
print(f"File uploaded: {upload_result.id}")
print(f"Purpose: {upload_result.purpose}")
print(f"Status: {upload_result.status}")# List all files
all_files = client.files.list()
print(f"Total files: {len(all_files.data)}")
# Filter by purpose
fine_tune_files = client.files.list(purpose="fine-tune")
print(f"Fine-tuning files: {len(fine_tune_files.data)}")
for file in fine_tune_files.data:
print(f"- {file.filename}: {file.bytes} bytes")
# Paginated listing
recent_files = client.files.list(limit=10)
for file in recent_files.data:
print(f"File: {file.id} - {file.filename}")# Get file information
file_id = "file-abc123"
file_info = client.files.retrieve(file_id)
print(f"File: {file_info.filename}")
print(f"Size: {file_info.bytes} bytes")
print(f"Created: {file_info.created_at}")
# Download file content
file_content = client.files.download(file_id)
# Save to local file
with open(f"downloaded_{file_info.filename}", "wb") as f:
f.write(file_content)
print(f"Downloaded {len(file_content)} bytes")# Generate signed URL for secure access
signed_url = client.files.get_signed_url(
file_id=file_id,
expiration=3600 # 1 hour
)
print(f"Signed URL: {signed_url.url}")
print(f"Expires: {signed_url.expires_at}")
# Use signed URL for direct access (external to SDK)
import requests
response = requests.get(signed_url.url)
if response.status_code == 200:
content = response.content
print(f"Retrieved {len(content)} bytes via signed URL")# Clean up old files
files = client.files.list()
for file in files.data:
# Delete files older than 30 days
if file.created_at < (time.time() - (30 * 24 * 3600)):
result = client.files.delete(file.id)
print(f"Deleted file: {file.filename}")class UploadFileOut:
id: str
object: str
bytes: int
created_at: int
filename: str
purpose: str
status: str
status_details: Optional[str]
class FilePurpose:
FINE_TUNE = "fine-tune"
ASSISTANTS = "assistants"
BATCH = "batch"class ListFilesOut:
object: str
data: List[File]
class File:
id: str
object: str
bytes: int
created_at: int
filename: str
purpose: str
status: str
status_details: Optional[str]class RetrieveFileOut:
id: str
object: str
bytes: int
created_at: int
filename: str
purpose: str
status: str
status_details: Optional[str]
class DeleteFileOut:
id: str
object: str
deleted: bool
class FileSignedURL:
url: str
expires_at: intclass FileStatus:
UPLOADED = "uploaded"
PROCESSED = "processed"
ERROR = "error"
DELETING = "deleting".txt, .jsonl, .json, .csv.pdf, .docx, .mdInstall with Tessl CLI
npx tessl i tessl/pypi-mistralai