Skip to main content

openai_interface/files/create/
request.rs

1//! Create a `CreateFileRequest` object for uploading a file.
2
3use serde::Serialize;
4use std::path::PathBuf;
5use url::Url;
6
7use crate::errors::OapiError;
8use crate::rest::post::{Post, PostNoStream};
9
10/// Upload a file that can be used across various endpoints.
11///
12/// Individual files can be up to 512 MB, and the size of all files uploaded by one
13/// organization can be up to 1 TB.
14///
15/// The Assistants API supports files up to 2 million tokens and of specific file
16/// types. See the
17/// [OpenAI Assistants Tools guide](https://platform.openai.com/docs/assistants/tools) for
18/// details.
19///
20/// The Fine-tuning API only supports `.jsonl` files. The input also has certain
21/// required formats for fine-tuning
22/// [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input) or
23/// [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input)
24/// models.
25///
26/// The Batch API only supports `.jsonl` files up to 200 MB in size. The input also
27/// has a specific required
28/// [format](https://platform.openai.com/docs/api-reference/batch/request-input).
29///
30/// Please [contact OpenAI](https://help.openai.com/) if you need to increase these
31/// storage limits.
32#[derive(Debug, Serialize, Clone, Default)]
33pub struct CreateFileRequest {
34    /// The File object (not file name) to be uploaded.
35    #[serde(skip_serializing)]
36    pub file: PathBuf,
37    /// The intended purpose of the uploaded file. One of: - `assistants`: Used in the
38    /// Assistants API - `batch`: Used in the Batch API - `fine-tune`: Used for
39    /// fine-tuning - `vision`: Images used for vision fine-tuning - `user_data`:
40    /// Flexible file type for any purpose - `evals`: Used for eval data sets
41    pub purpose: FilePurpose,
42    /// The expiration policy for a file. By default, files with `purpose=batch` expire
43    /// after 30 days and all other files are persisted until they are manually deleted.
44    ///
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub expires_after: Option<ExpiresAfter>,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
49}
50
51#[derive(Debug, Serialize, Clone, Default)]
52pub enum FilePurpose {
53    #[serde(rename = "assistants")]
54    Assistants,
55    #[serde(rename = "batch")]
56    #[default]
57    Batch,
58    #[serde(rename = "fine-tune")]
59    FineTune,
60    #[serde(rename = "vision")]
61    Vision,
62    #[serde(rename = "user_data")]
63    UserData,
64    #[serde(rename = "evals")]
65    Evals,
66    #[serde(untagged)]
67    Other(String),
68}
69
70// #[derive(Debug, Serialize, Clone)]
71// pub enum FileTypes {
72//     /// file (or bytes)
73//     FileContent(Vec<u8>),
74//     /// (filename, file (or bytes))
75//     FileNameAndContent(String, Vec<u8>),
76//     /// (filename, file (or bytes), content_type)
77//     FileNameAndContentAndType(String, Vec<u8>, String),
78//     /// (filename, file (or bytes), content_type, headers)
79//     FileNameAndContentAndTypeAndHeaders(String, Vec<u8>, String, HashMap<String, String>),
80// }
81
82/// The expiration policy for a file.
83///
84/// By default, files with `purpose=batch` expire after 30 days and all other files
85/// are persisted until they are manually deleted.
86///
87/// Matches the official multipart form fields `expires_after[anchor]` and
88/// `expires_after[seconds]`.
89#[derive(Debug, Serialize, Clone)]
90#[serde(tag = "anchor", rename_all = "snake_case")]
91pub enum ExpiresAfter {
92    /// Anchor timestamp after which the expiration policy applies.
93    /// Supported anchors: `created_at`.
94    CreatedAt {
95        /// The number of seconds after the anchor time that the file will expire.
96        /// Must be between 3600 (1 hour) and 2592000 (30 days).
97        seconds: usize,
98    },
99}
100
101impl Post for CreateFileRequest {
102    #[inline]
103    fn is_streaming(&self) -> bool {
104        false
105    }
106
107    /// Builds the URL for the request.
108    ///
109    /// `base_url` should be like <https://api.openai.com/v1>
110    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
111        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
112        url.path_segments_mut()
113            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
114            .push("files");
115
116        Ok(url.to_string())
117    }
118}
119
120impl PostNoStream for CreateFileRequest {
121    type Response = crate::files::FileObject;
122
123    /// Sends a file upload POST request using multipart/form-data format.
124    /// This implementation handles the actual file upload with proper file handling.
125    async fn get_response_string(
126        &self,
127        client: &reqwest::Client,
128        url: &str,
129        key: &str,
130    ) -> Result<String, OapiError> {
131        // Read file content
132        let file_content = tokio::fs::read(&self.file).await?;
133
134        // Get file name from path
135        let file_name = self
136            .file
137            .file_name()
138            .and_then(|name| name.to_str())
139            .ok_or_else(|| OapiError::ResponseError("Invalid file name".to_string()))?
140            .to_string();
141
142        // Create multipart form with file and purpose
143        let file_part = reqwest::multipart::Part::bytes(file_content).file_name(file_name);
144
145        let mut form = reqwest::multipart::Form::new().part("file", file_part);
146
147        // Add purpose field
148        let purpose_str = serde_json::to_string(&self.purpose)
149            .map_err(|e| OapiError::ResponseError(format!("Failed to serialize purpose: {}", e)))?;
150        let trimmed_purpose = purpose_str.trim_matches('"').to_string();
151        form = form.text("purpose", trimmed_purpose);
152
153        // Add expires_after if present, using the official multipart form
154        // fields `expires_after[anchor]` and `expires_after[seconds]`.
155        if let Some(expires_after) = &self.expires_after {
156            let (anchor, seconds) = match expires_after {
157                ExpiresAfter::CreatedAt { seconds } => ("created_at", *seconds),
158            };
159            form = form
160                .text("expires_after[anchor]", anchor)
161                .text("expires_after[seconds]", seconds.to_string());
162        }
163
164        // Extra body properties are sent as additional multipart text
165        // fields, matching the official SDK (strings verbatim, other JSON
166        // values as their serialized form).
167        if let Some(extra_body) = &self.extra_body {
168            for (key, value) in extra_body {
169                let text = match value {
170                    serde_json::Value::String(s) => s.clone(),
171                    other => other.to_string(),
172                };
173                form = form.text(key.clone(), text);
174            }
175        }
176
177        let response = client
178            .post(url)
179            .header("Accept", "application/json")
180            .bearer_auth(key)
181            .multipart(form)
182            .send()
183            .await?;
184
185        crate::rest::response_text_checked(response).await
186    }
187}