CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mistralai

Python Client SDK for the Mistral AI API with chat completions, embeddings, fine-tuning, and agent capabilities.

Pending
Overview
Eval results
Files

files.mddocs/

Files

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.

Capabilities

File Upload

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
    """

File Listing

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
    """

File Retrieval

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
    """

File Download

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
    """

File Deletion

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
    """

Signed URLs

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
    """

Usage Examples

Upload Training Data

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 from File Path

# 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 and Filter Files

# 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}")

Download and Process Files

# 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")

Secure File Access

# 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")

File Management

# 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}")

Types

File Upload Types

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"

File Listing Types

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]

File Operations

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: int

File Status

class FileStatus:
    UPLOADED = "uploaded"
    PROCESSED = "processed"
    ERROR = "error"
    DELETING = "deleting"

File Management Best Practices

Supported File Types

  • Text files: .txt, .jsonl, .json, .csv
  • Documents: .pdf, .docx, .md
  • Data files: Various formats depending on purpose

File Size Limits

  • Maximum file size varies by purpose and plan
  • Check current limits in the API documentation
  • Consider splitting large datasets for better processing

Purpose Guidelines

  • fine-tune: Training data in JSONL format for model fine-tuning
  • assistants: Documents and files for agent knowledge bases
  • batch: Input files for batch processing operations

Security Considerations

  • Files are encrypted at rest and in transit
  • Signed URLs provide time-limited secure access
  • Regular cleanup of unused files is recommended
  • Access is controlled through API key authentication

Install with Tessl CLI

npx tessl i tessl/pypi-mistralai

docs

agents.md

audio.md

batch.md

beta.md

chat-completions.md

classification.md

embeddings.md

files.md

fim.md

fine-tuning.md

index.md

models.md

ocr.md

tile.json