dynamo_async_openai/types/upload.rs
1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use crate::error::OpenAIError;
12use derive_builder::Builder;
13use serde::{Deserialize, Serialize};
14
15use super::{InputSource, OpenAIFile};
16
17/// Request to create an upload object that can accept byte chunks in the form of Parts.
18#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
19#[builder(name = "CreateUploadRequestArgs")]
20#[builder(pattern = "mutable")]
21#[builder(setter(into, strip_option), default)]
22#[builder(derive(Debug))]
23#[builder(build_fn(error = "OpenAIError"))]
24pub struct CreateUploadRequest {
25 /// The name of the file to upload.
26 pub filename: String,
27
28 /// The intended purpose of the uploaded file.
29 ///
30 /// See the [documentation on File purposes](https://platform.openai.com/docs/api-reference/files/create#files-create-purpose).
31 pub purpose: UploadPurpose,
32
33 /// The number of bytes in the file you are uploading.
34 pub bytes: u64,
35
36 /// The MIME type of the file.
37 ///
38 /// This must fall within the supported MIME types for your file purpose. See the supported MIME
39 /// types for assistants and vision.
40 pub mime_type: String,
41}
42
43/// The intended purpose of the uploaded file.
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
45#[serde(rename_all = "snake_case")]
46pub enum UploadPurpose {
47 /// For use with Assistants and Message files
48 Assistants,
49 /// For Assistants image file inputs
50 Vision,
51 /// For use with the Batch API
52 Batch,
53 /// For use with Fine-tuning
54 #[default]
55 FineTune,
56}
57
58/// The Upload object can accept byte chunks in the form of Parts.
59#[derive(Debug, Serialize, Deserialize)]
60pub struct Upload {
61 /// The Upload unique identifier, which can be referenced in API endpoints
62 pub id: String,
63
64 /// The Unix timestamp (in seconds) for when the Upload was created
65 pub created_at: u32,
66
67 /// The name of the file to be uploaded
68 pub filename: String,
69
70 /// The intended number of bytes to be uploaded
71 pub bytes: u64,
72
73 /// The intended purpose of the file. [Pelase refer here]([Please refer here](/docs/api-reference/files/object#files/object-purpose) for acceptable values.)
74 pub purpose: UploadPurpose,
75
76 /// The status of the Upload.
77 pub status: UploadStatus,
78
79 /// The Unix timestamp (in seconds) for when the Upload was created
80 pub expires_at: u32,
81
82 /// The object type, which is always "upload"
83 pub object: String,
84
85 /// The ready File object after the Upload is completed
86 #[serde(skip_serializing_if = "Option::is_none")]
87 pub file: Option<OpenAIFile>,
88}
89
90/// The status of an upload
91#[derive(Debug, Serialize, Deserialize)]
92#[serde(rename_all = "lowercase")]
93pub enum UploadStatus {
94 /// Upload is pending
95 Pending,
96 /// Upload has completed successfully
97 Completed,
98 /// Upload was cancelled
99 Cancelled,
100 /// Upload has expired
101 Expired,
102}
103
104/// The upload Part represents a chunk of bytes we can add to an Upload object.
105#[derive(Debug, Serialize, Deserialize)]
106pub struct UploadPart {
107 /// The upload Part unique identifier, which can be referenced in API endpoints
108 pub id: String,
109
110 /// The Unix timestamp (in seconds) for when the Part was created
111 pub created_at: u32,
112
113 /// The ID of the Upload object that this Part was added to
114 pub upload_id: String,
115
116 /// The object type, which is always `upload.part`
117 pub object: String,
118}
119
120/// Request parameters for adding a part to an Upload
121#[derive(Debug, Clone)]
122pub struct AddUploadPartRequest {
123 /// The chunk of bytes for this Part
124 pub data: InputSource,
125}
126
127/// Request parameters for completing an Upload
128#[derive(Debug, Serialize)]
129pub struct CompleteUploadRequest {
130 /// The ordered list of Part IDs
131 pub part_ids: Vec<String>,
132
133 /// The optional md5 checksum for the file contents to verify if the bytes uploaded matches what you expect
134 #[serde(skip_serializing_if = "Option::is_none")]
135 pub md5: Option<String>,
136}