Skip to main content

openai_compat/types/
uploads.rs

1//! Resumable upload types, mirroring `openai-python/src/openai/types/upload.py`
2//! and `types/uploads/upload_part.py`.
3
4use serde::{Deserialize, Serialize};
5
6use crate::pagination::HasId;
7use crate::types::files::FileObject;
8
9/// Default chunk size for splitting a large upload into parts (64 MiB).
10pub const DEFAULT_PART_SIZE: usize = 64 * 1024 * 1024;
11
12/// The status of an [`Upload`].
13///
14/// Unrecognized values deserialize to [`UploadStatus::Unknown`].
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(rename_all = "snake_case")]
17pub enum UploadStatus {
18    Pending,
19    Completed,
20    Cancelled,
21    Expired,
22    /// A status not recognized by this client version.
23    #[serde(other)]
24    Unknown,
25}
26
27/// A resumable upload: created, filled with parts, then completed.
28#[derive(Debug, Clone, Deserialize)]
29#[non_exhaustive]
30pub struct Upload {
31    pub id: String,
32    #[serde(default)]
33    pub bytes: i64,
34    #[serde(default)]
35    pub created_at: i64,
36    #[serde(default)]
37    pub expires_at: i64,
38    #[serde(default)]
39    pub filename: String,
40    #[serde(default)]
41    pub object: String,
42    #[serde(default)]
43    pub purpose: String,
44    pub status: UploadStatus,
45    /// The ready [`FileObject`], present once the upload is completed.
46    #[serde(default)]
47    pub file: Option<FileObject>,
48}
49
50impl HasId for Upload {
51    fn id(&self) -> Option<&str> {
52        Some(&self.id)
53    }
54}
55
56/// A single uploaded part of an [`Upload`].
57#[derive(Debug, Clone, Deserialize)]
58#[non_exhaustive]
59pub struct UploadPart {
60    pub id: String,
61    #[serde(default)]
62    pub created_at: i64,
63    #[serde(default)]
64    pub upload_id: String,
65    #[serde(default)]
66    pub object: String,
67}
68
69impl HasId for UploadPart {
70    fn id(&self) -> Option<&str> {
71        Some(&self.id)
72    }
73}
74
75/// Parameters for `POST /uploads`.
76#[derive(Debug, Clone, Serialize)]
77pub struct UploadCreateParams {
78    /// The total number of bytes that will be uploaded across all parts.
79    pub bytes: i64,
80    /// The name of the file to be uploaded.
81    pub filename: String,
82    /// The MIME type of the file, e.g. `application/jsonl`.
83    pub mime_type: String,
84    /// The intended purpose, e.g. `assistants`, `batch`, `fine-tune`, `vision`,
85    /// `user_data`, or `evals`.
86    pub purpose: String,
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub expires_after: Option<crate::types::common::ExpiresAfter>,
89}
90
91impl UploadCreateParams {
92    pub fn new(
93        filename: impl Into<String>,
94        bytes: i64,
95        mime_type: impl Into<String>,
96        purpose: impl Into<String>,
97    ) -> Self {
98        Self {
99            bytes,
100            filename: filename.into(),
101            mime_type: mime_type.into(),
102            purpose: purpose.into(),
103            expires_after: None,
104        }
105    }
106
107    pub fn expires_after(mut self, expires_after: crate::types::common::ExpiresAfter) -> Self {
108        self.expires_after = Some(expires_after);
109        self
110    }
111}