uploadOpenAIFile

Uploads a single file to the OpenAI Files API.

Return

An OpenAIFileObject representing the uploaded file.

Parameters

request

The upload request containing the file and its purpose.

onProgress

Optional listener for upload progress (0.0–1.0).

Throws

if the request fails.

Samples

// Load a file from path.
val file = runCatching {
    KoryFile.fromPath("path/to/file.ext")
}.getOrElse {
    // From path function can throw a FileNotFoundException
    error("Error while loading file: ${it.message}")
}

// Upload file to OpenAI files API.
val result = client.uploadOpenAIFile(
    request = OpenAIUploadFileRequest(file, purpose = OpenAIFilePurpose.FINE_TUNE)
) { uploadingProgress ->
    println("Uploading file progress: ${uploadingProgress.percentage}%")
}

println("File successfully loaded, id: ${result.id}")

suspend fun uploadOpenAIFile(onProgress: UploadFileProgressListener<SingleFileProgress>? = null, purpose: OpenAIFilePurpose = OpenAIFilePurpose.FINE_TUNE, block: OpenAIUploadFileRequestBuilder.() -> Unit): OpenAIFileObject

Uploads a single file to the OpenAI Files API using the request DSL.

Return

An OpenAIFileObject representing the uploaded file.

Parameters

onProgress

Optional listener for upload progress (0.0–1.0).

purpose

The purpose of the file. Defaults to OpenAIFilePurpose.FINE_TUNE.

block

DSL builder for configuring the upload request.

See also

Throws

if the request fails.

Samples

// Upload file to OpenAI files API with DSL.
val result = client.uploadOpenAIFile(
    onProgress = { progress -> println("Uploading file progress: ${progress.percentage}%") },
) {
    fileFromPath("path/to/file.ext") // This function can be thrown a FileNotFoundException.
}

println("File successfully loaded, id: ${result.id}")