openai_tools/batch/response.rs
1//! OpenAI Batch API Response Types
2//!
3//! This module defines the response types for the OpenAI Batch API.
4
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8/// The status of a batch job.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11#[non_exhaustive]
12pub enum BatchStatus {
13 /// The batch is being validated.
14 Validating,
15 /// The batch failed validation.
16 Failed,
17 /// The batch is currently being processed.
18 InProgress,
19 /// The batch is being finalized.
20 Finalizing,
21 /// The batch has been completed successfully.
22 Completed,
23 /// The batch has expired (24h window passed).
24 Expired,
25 /// The batch is being cancelled.
26 Cancelling,
27 /// The batch has been cancelled.
28 Cancelled,
29 /// A status this version of the library does not know about.
30 ///
31 /// OpenAI can add lifecycle states at any time; capturing the raw value
32 /// keeps the surrounding response parseable.
33 #[serde(untagged)]
34 Other(String),
35}
36
37/// Counts of requests in different states within the batch.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct RequestCounts {
40 /// Total number of requests in the batch.
41 pub total: u32,
42 /// Number of requests that have been completed successfully.
43 pub completed: u32,
44 /// Number of requests that have failed.
45 pub failed: u32,
46}
47
48/// An error that occurred during batch processing.
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct BatchError {
51 /// A machine-readable error code.
52 pub code: String,
53 /// A human-readable error message.
54 pub message: String,
55 /// The parameter related to the error, if any.
56 pub param: Option<String>,
57 /// The line number in the input file, if applicable.
58 pub line: Option<u32>,
59}
60
61/// A collection of errors from batch processing.
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct BatchErrors {
64 /// The type of object (always "list").
65 pub object: Option<String>,
66 /// The list of errors.
67 pub data: Vec<BatchError>,
68}
69
70/// A batch object representing an async batch job.
71///
72/// The Batch API allows you to send asynchronous groups of requests
73/// with 50% lower costs and higher rate limits.
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct BatchObject {
76 /// The unique identifier for the batch.
77 pub id: String,
78
79 /// The object type (always "batch").
80 pub object: String,
81
82 /// The API endpoint used for the batch.
83 pub endpoint: String,
84
85 /// Any errors that occurred during batch processing.
86 pub errors: Option<BatchErrors>,
87
88 /// The ID of the input file containing the requests.
89 pub input_file_id: String,
90
91 /// The time window for batch completion (e.g., "24h").
92 pub completion_window: String,
93
94 /// The current status of the batch.
95 pub status: BatchStatus,
96
97 /// The ID of the output file containing the results.
98 pub output_file_id: Option<String>,
99
100 /// The ID of the error file containing failed requests.
101 pub error_file_id: Option<String>,
102
103 /// The Unix timestamp when the batch was created.
104 pub created_at: i64,
105
106 /// The Unix timestamp when processing started.
107 pub in_progress_at: Option<i64>,
108
109 /// The Unix timestamp when the batch expires.
110 pub expires_at: Option<i64>,
111
112 /// The Unix timestamp when finalization started.
113 pub finalizing_at: Option<i64>,
114
115 /// The Unix timestamp when the batch completed.
116 pub completed_at: Option<i64>,
117
118 /// The Unix timestamp when the batch failed.
119 pub failed_at: Option<i64>,
120
121 /// The Unix timestamp when the batch expired.
122 pub expired_at: Option<i64>,
123
124 /// The Unix timestamp when cancellation started.
125 pub cancelling_at: Option<i64>,
126
127 /// The Unix timestamp when the batch was cancelled.
128 pub cancelled_at: Option<i64>,
129
130 /// Counts of requests in different states.
131 pub request_counts: Option<RequestCounts>,
132
133 /// User-defined metadata attached to the batch.
134 pub metadata: Option<HashMap<String, String>>,
135}
136
137/// Response for listing batch jobs.
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct BatchListResponse {
140 /// The object type (always "list").
141 pub object: String,
142
143 /// The list of batch objects.
144 pub data: Vec<BatchObject>,
145
146 /// The ID of the first batch in the list.
147 pub first_id: Option<String>,
148
149 /// The ID of the last batch in the list.
150 pub last_id: Option<String>,
151
152 /// Whether there are more batches to retrieve.
153 pub has_more: bool,
154}