Skip to main content

openai_types/batch/
manual.rs

1// Manual: hand-crafted batch types (enums, builders, response structs).
2
3use serde::{Deserialize, Serialize};
4
5/// Status of a batch job.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
8#[non_exhaustive]
9pub enum BatchStatus {
10    #[serde(rename = "validating")]
11    Validating,
12    #[serde(rename = "failed")]
13    Failed,
14    #[serde(rename = "in_progress")]
15    InProgress,
16    #[serde(rename = "finalizing")]
17    Finalizing,
18    #[serde(rename = "completed")]
19    Completed,
20    #[serde(rename = "expired")]
21    Expired,
22    #[serde(rename = "cancelling")]
23    Cancelling,
24    #[serde(rename = "cancelled")]
25    Cancelled,
26}
27
28/// Request body for `POST /batches`.
29#[derive(Debug, Clone, Serialize)]
30#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
31pub struct BatchCreateRequest {
32    /// File ID of the input JSONL file.
33    pub input_file_id: String,
34
35    /// API endpoint (e.g. "/v1/chat/completions").
36    pub endpoint: String,
37
38    /// Completion window (currently only "24h").
39    pub completion_window: String,
40
41    /// Metadata.
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub metadata: Option<std::collections::HashMap<String, String>>,
44}
45
46impl BatchCreateRequest {
47    pub fn new(
48        input_file_id: impl Into<String>,
49        endpoint: impl Into<String>,
50        completion_window: impl Into<String>,
51    ) -> Self {
52        Self {
53            input_file_id: input_file_id.into(),
54            endpoint: endpoint.into(),
55            completion_window: completion_window.into(),
56            metadata: None,
57        }
58    }
59}
60
61/// A batch object.
62#[derive(Debug, Clone, Deserialize)]
63#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
64pub struct Batch {
65    pub id: String,
66    pub object: String,
67    pub endpoint: String,
68    pub input_file_id: String,
69    pub completion_window: String,
70    pub status: BatchStatus,
71    pub created_at: i64,
72    #[serde(default)]
73    pub output_file_id: Option<String>,
74    #[serde(default)]
75    pub error_file_id: Option<String>,
76    #[serde(default)]
77    pub in_progress_at: Option<i64>,
78    #[serde(default)]
79    pub completed_at: Option<i64>,
80    #[serde(default)]
81    pub failed_at: Option<i64>,
82    #[serde(default)]
83    pub cancelled_at: Option<i64>,
84    #[serde(default)]
85    pub expired_at: Option<i64>,
86    #[serde(default)]
87    pub request_counts: Option<BatchRequestCounts>,
88    #[serde(default)]
89    pub metadata: Option<std::collections::HashMap<String, String>>,
90}
91
92/// Request counts for a batch.
93#[derive(Debug, Clone, Deserialize)]
94#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
95pub struct BatchRequestCounts {
96    pub total: i64,
97    pub completed: i64,
98    pub failed: i64,
99}
100
101/// List of batches.
102#[derive(Debug, Clone, Deserialize)]
103#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
104pub struct BatchList {
105    pub object: String,
106    pub data: Vec<Batch>,
107    /// Whether there are more results available.
108    #[serde(default)]
109    pub has_more: Option<bool>,
110    /// ID of the first object in the list.
111    #[serde(default)]
112    pub first_id: Option<String>,
113    /// ID of the last object in the list.
114    #[serde(default)]
115    pub last_id: Option<String>,
116}
117
118/// Parameters for listing batches with pagination.
119#[derive(Debug, Clone, Default)]
120#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
121pub struct BatchListParams {
122    /// Cursor for pagination -- fetch results after this batch ID.
123    pub after: Option<String>,
124    /// Maximum number of results per page (1-100).
125    pub limit: Option<i64>,
126}
127
128impl BatchListParams {
129    pub fn new() -> Self {
130        Self::default()
131    }
132
133    pub fn after(mut self, after: impl Into<String>) -> Self {
134        self.after = Some(after.into());
135        self
136    }
137
138    pub fn limit(mut self, limit: i64) -> Self {
139        self.limit = Some(limit);
140        self
141    }
142
143    /// Convert to query parameter pairs for the HTTP request.
144    pub fn to_query(&self) -> Vec<(String, String)> {
145        let mut q = Vec::new();
146        if let Some(ref after) = self.after {
147            q.push(("after".into(), after.clone()));
148        }
149        if let Some(limit) = self.limit {
150            q.push(("limit".into(), limit.to_string()));
151        }
152        q
153    }
154}