Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Togethercomputer Together python Files Upload For Batch

From Leeroopedia
Attribute Value
Type Implementation
Domains Batch_Processing, Inference, API_Client
Repository togethercomputer/together-python
Source src/together/resources/files.py:L28-59
Last Updated 2026-02-15 16:00 GMT

Overview

Implementation of file upload for batch inference input files using the Files.upload() method with purpose="batch-api".

Code Reference

class Files:
    def __init__(self, client: TogetherClient) -> None:
        self._client = client

    def upload(
        self,
        file: Path | str,
        *,
        purpose: FilePurpose | str = FilePurpose.FineTune,
        check: bool = True,
    ) -> FileResponse:

        if check and purpose == FilePurpose.FineTune:
            report_dict = check_file(file)
            if not report_dict["is_check_passed"]:
                raise FileTypeError(
                    f"Invalid file supplied, failed to upload. Report:\n{pformat(report_dict)}"
                )

        if isinstance(file, str):
            file = Path(file)

        if isinstance(purpose, str):
            purpose = FilePurpose(purpose)

        assert isinstance(purpose, FilePurpose)

        file_size = os.stat(file).st_size
        file_size_gb = file_size / NUM_BYTES_IN_GB

        if file_size_gb > MULTIPART_THRESHOLD_GB:
            multipart_manager = MultipartUploadManager(self._client)
            return multipart_manager.upload("files", file, purpose)
        else:
            upload_manager = UploadManager(self._client)
            return upload_manager.upload("files", file, purpose=purpose, redirect=True)

Source: src/together/resources/files.py:L28-59

API Signature

Files.upload(
    file: Path | str,
    *,
    purpose: FilePurpose | str = FilePurpose.FineTune,
    check: bool = True,
) -> FileResponse
Parameter Type Default Description
file str (required) Path to the JSONL input file
purpose str FilePurpose.FineTune Must be "batch-api" or FilePurpose.BatchAPI for batch inference
check bool True File validation flag; ignored when purpose is not "fine-tune"

I/O Contract

Input:

  • A local file path to a valid JSONL batch input file.
  • The purpose parameter set to "batch-api" or FilePurpose.BatchAPI.

Output: A FileResponse object with the following fields:

Field Type Description
id string The file identifier to use when creating a batch job
object string Object type identifier
created_at int or None Creation timestamp
type FileType or None File type (e.g., jsonl)
purpose FilePurpose or None The purpose tag (batch-api)
filename string or None Original filename
bytes int or None File size in bytes

Key Behavior

Validation bypass: The check_file() validation is only called when both check=True and purpose == FilePurpose.FineTune. For batch-api uploads, the condition purpose == FilePurpose.FineTune evaluates to False, so check_file() is never invoked regardless of the check parameter:

if check and purpose == FilePurpose.FineTune:
    report_dict = check_file(file)
    # ... only reached for fine-tune uploads

Automatic multipart upload: Files larger than MULTIPART_THRESHOLD_GB are automatically uploaded via MultipartUploadManager instead of the standard UploadManager.

Usage Examples

Basic Batch File Upload

from together import Together

client = Together()

# Upload the batch input file with batch-api purpose
file_response = client.files.upload(
    file="batch_input.jsonl",
    purpose="batch-api",
)

# Extract file ID for batch job creation
file_id = file_response.id
print(f"Uploaded file ID: {file_id}")

Using the FilePurpose Enum

from together import Together
from together.types import FilePurpose

client = Together()

file_response = client.files.upload(
    file="batch_input.jsonl",
    purpose=FilePurpose.BatchAPI,
)

Related

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment