Skip to main content

otari/types/
batch.rs

1//! Batch operation types for the gateway provider.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Status of a batch job.
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum BatchStatus {
10    /// Batch is being validated.
11    Validating,
12    /// Batch validation or processing failed.
13    Failed,
14    /// Batch is currently being processed.
15    InProgress,
16    /// Batch is being finalized.
17    Finalizing,
18    /// Batch has completed successfully.
19    Completed,
20    /// Batch has expired.
21    Expired,
22    /// Batch is being cancelled.
23    Cancelling,
24    /// Batch has been cancelled.
25    Cancelled,
26}
27
28/// Request counts for a batch job.
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct BatchRequestCounts {
31    /// Total number of requests in the batch.
32    pub total: u32,
33    /// Number of completed requests.
34    pub completed: u32,
35    /// Number of failed requests.
36    pub failed: u32,
37}
38
39/// A batch job returned by the gateway.
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct Batch {
42    /// Unique identifier for the batch.
43    pub id: String,
44    /// The object type (always "batch").
45    pub object: String,
46    /// The API endpoint used for the batch requests.
47    pub endpoint: String,
48    /// Current status of the batch.
49    pub status: BatchStatus,
50    /// Unix timestamp of when the batch was created.
51    pub created_at: i64,
52    /// The time window for batch completion.
53    pub completion_window: String,
54    /// The provider that is processing this batch.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub provider: Option<String>,
57    /// The input file ID, if applicable.
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub input_file_id: Option<String>,
60    /// The output file ID, if applicable.
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub output_file_id: Option<String>,
63    /// The error file ID, if applicable.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub error_file_id: Option<String>,
66    /// Counts of requests by status.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub request_counts: Option<BatchRequestCounts>,
69    /// User-provided metadata.
70    #[serde(skip_serializing_if = "Option::is_none")]
71    pub metadata: Option<HashMap<String, String>>,
72    /// Unix timestamp of when the batch started processing.
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub in_progress_at: Option<i64>,
75    /// Unix timestamp of when the batch completed.
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub completed_at: Option<i64>,
78}
79
80/// Parameters for creating a batch job.
81#[derive(Debug, Clone, Serialize)]
82pub struct CreateBatchParams {
83    /// The model to use, in `provider:model` format (e.g., "openai:gpt-4o-mini").
84    pub model: String,
85    /// The list of requests to process.
86    pub requests: Vec<BatchRequestItem>,
87    /// The time window for batch completion (e.g., "24h").
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub completion_window: Option<String>,
90    /// User-provided metadata key-value pairs.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub metadata: Option<HashMap<String, String>>,
93}
94
95impl CreateBatchParams {
96    /// Create new batch params with required fields.
97    pub fn new(model: impl Into<String>, requests: Vec<BatchRequestItem>) -> Self {
98        Self {
99            model: model.into(),
100            requests,
101            completion_window: None,
102            metadata: None,
103        }
104    }
105
106    /// Set the completion window (e.g., "24h").
107    pub fn completion_window(mut self, window: impl Into<String>) -> Self {
108        self.completion_window = Some(window.into());
109        self
110    }
111
112    /// Set metadata key-value pairs.
113    pub fn metadata(mut self, metadata: HashMap<String, String>) -> Self {
114        self.metadata = Some(metadata);
115        self
116    }
117}
118
119/// A single request within a batch.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct BatchRequestItem {
122    /// A unique identifier for this request within the batch.
123    pub custom_id: String,
124    /// The request body (typically containing `messages`, `max_tokens`, etc.).
125    pub body: serde_json::Value,
126}
127
128/// Options for listing batch jobs.
129#[derive(Debug, Clone, Default)]
130pub struct ListBatchesOptions {
131    /// Cursor for pagination — return batches after this ID.
132    pub after: Option<String>,
133    /// Maximum number of batches to return.
134    pub limit: Option<u32>,
135}
136
137/// An error for a single request within a batch.
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct BatchResultError {
140    /// The error code.
141    pub code: String,
142    /// A human-readable error message.
143    pub message: String,
144}
145
146/// The result of a single request within a batch.
147#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct BatchResultItem {
149    /// The custom ID matching the original request.
150    pub custom_id: String,
151    /// The successful completion result, if any.
152    #[serde(skip_serializing_if = "Option::is_none")]
153    pub result: Option<super::completion::ChatCompletion>,
154    /// The error, if the request failed.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub error: Option<BatchResultError>,
157}
158
159/// The results of a completed batch job.
160#[derive(Debug, Clone, Serialize, Deserialize)]
161pub struct BatchResult {
162    /// The list of results, one per request in the batch.
163    pub results: Vec<BatchResultItem>,
164}