|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Example demonstrating typed SDK usage. |
| 4 | +
|
| 5 | +This file is type-checked by mypy in CI to ensure the SDK types are correct. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +from mistralai.client import Mistral |
| 11 | +from mistralai.client.models import ( |
| 12 | + # Entity schemas - the actual resources |
| 13 | + BatchJob, |
| 14 | + FileObject, |
| 15 | + Library, |
| 16 | + # Response schemas - wrappers for list/delete operations |
| 17 | + ListBatchJobsResponse, |
| 18 | + ListFilesResponse, |
| 19 | + ListLibrariesResponse, |
| 20 | + # Request schemas exist but users typically use kwargs instead |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def demo_batch_jobs(client: Mistral) -> None: |
| 25 | + """Demonstrate BatchJob typing.""" |
| 26 | + # list() returns ListBatchJobsResponse |
| 27 | + response: ListBatchJobsResponse = client.batch.jobs.list(page_size=10) |
| 28 | + |
| 29 | + # response.data is List[BatchJob] (may be None) |
| 30 | + jobs: list[BatchJob] = response.data or [] |
| 31 | + |
| 32 | + for job in jobs: |
| 33 | + # BatchJob has typed attributes |
| 34 | + job_id: str = job.id |
| 35 | + status: str = job.status |
| 36 | + created_at: int = job.created_at |
| 37 | + print(f"Job {job_id}: {status} (created: {created_at})") |
| 38 | + |
| 39 | + |
| 40 | +def demo_files(client: Mistral) -> None: |
| 41 | + """Demonstrate File typing.""" |
| 42 | + # list() returns ListFilesResponse |
| 43 | + response: ListFilesResponse = client.files.list(page_size=10) |
| 44 | + |
| 45 | + # response.data is List[FileObject] |
| 46 | + files: list[FileObject] = response.data |
| 47 | + |
| 48 | + for file in files: |
| 49 | + # FileObject has typed attributes |
| 50 | + file_id: str = file.id |
| 51 | + filename: str = file.filename |
| 52 | + size: int = file.size_bytes |
| 53 | + print(f"File {file_id}: {filename} ({size} bytes)") |
| 54 | + |
| 55 | + |
| 56 | +def demo_libraries(client: Mistral) -> None: |
| 57 | + """Demonstrate Library typing.""" |
| 58 | + # list() returns ListLibrariesResponse |
| 59 | + response: ListLibrariesResponse = client.libraries.list() |
| 60 | + |
| 61 | + # response.data is List[Library] |
| 62 | + libraries: list[Library] = response.data |
| 63 | + |
| 64 | + for lib in libraries: |
| 65 | + # Library has typed attributes |
| 66 | + lib_id: str = str(lib.id) |
| 67 | + name: str = lib.name |
| 68 | + print(f"Library {lib_id}: {name}") |
| 69 | + |
| 70 | + |
| 71 | +def main() -> None: |
| 72 | + api_key = os.environ.get("MISTRAL_API_KEY") |
| 73 | + if not api_key: |
| 74 | + print("MISTRAL_API_KEY not set, skipping runtime demo") |
| 75 | + return |
| 76 | + |
| 77 | + client = Mistral(api_key=api_key) |
| 78 | + |
| 79 | + print("=== Batch Jobs ===") |
| 80 | + demo_batch_jobs(client) |
| 81 | + |
| 82 | + print("\n=== Files ===") |
| 83 | + demo_files(client) |
| 84 | + |
| 85 | + print("\n=== Libraries ===") |
| 86 | + demo_libraries(client) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments