1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! Batch Messages API
//!
//! Provides asynchronous batch processing for up to 100,000 message requests.
//! Batch processing allows efficient handling of large-scale operations with
//! server-side request management and result retrieval.
#[ cfg( feature = "batch-processing" ) ]
mod private
{
use serde::{ Serialize, Deserialize };
/// Request item for batch processing
///
/// Each batch request item contains a custom ID for result matching
/// and the message request parameters.
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct BatchRequestItem
{
/// User-defined ID for matching results (max 256 characters)
pub custom_id : String,
/// Message request parameters
pub params : crate::CreateMessageRequest,
}
impl BatchRequestItem
{
/// Create new batch request item
#[ must_use ]
pub fn new( custom_id : String, params : crate::CreateMessageRequest ) -> Self
{
Self { custom_id, params }
}
/// Validate batch request item
///
/// # Errors
///
/// Returns error if `custom_id` exceeds 256 characters or params are invalid
pub fn validate( &self ) -> crate::AnthropicResult< () >
{
if self.custom_id.len() > 256
{
return Err( crate::AnthropicError::InvalidArgument(
format!( "custom_id length {} exceeds maximum of 256 characters", self.custom_id.len() )
) );
}
if self.custom_id.is_empty()
{
return Err( crate::AnthropicError::InvalidArgument(
"custom_id cannot be empty".to_string()
) );
}
// Delegate to CreateMessageRequest validation if available
#[ cfg( feature = "error-handling" ) ]
{
self.params.validate()?;
}
Ok( () )
}
}
/// Batch creation request
///
/// Contains an array of message requests to process asynchronously.
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct CreateBatchRequest
{
/// Array of batch request items (up to 100,000 items, 256 MB max)
pub requests : Vec< BatchRequestItem >,
}
impl CreateBatchRequest
{
/// Create new batch request
#[ must_use ]
pub fn new( requests : Vec< BatchRequestItem > ) -> Self
{
Self { requests }
}
/// Validate batch request
///
/// # Errors
///
/// Returns error if request count exceeds limits or items are invalid
pub fn validate( &self ) -> crate::AnthropicResult< () >
{
const MAX_REQUESTS : usize = 100_000;
if self.requests.is_empty()
{
return Err( crate::AnthropicError::InvalidArgument(
"Batch request cannot be empty".to_string()
) );
}
if self.requests.len() > MAX_REQUESTS
{
return Err( crate::AnthropicError::InvalidArgument(
format!( "Batch contains {} requests, exceeding maximum of {}", self.requests.len(), MAX_REQUESTS )
) );
}
// Validate each request item
for ( idx, item ) in self.requests.iter().enumerate()
{
item.validate().map_err( | e |
crate::AnthropicError::InvalidArgument(
format!( "Batch request item {idx} invalid : {e}" )
)
)?;
}
Ok( () )
}
}
/// Request counts for batch status
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq, Eq ) ]
pub struct RequestCounts
{
/// Total number of requests in batch
pub processing : u32,
/// Number of successfully completed requests
pub succeeded : u32,
/// Number of requests with errors
pub errored : u32,
/// Number of canceled requests
pub canceled : u32,
/// Number of expired requests
pub expired : u32,
}
/// Batch processing status
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq, Eq ) ]
#[ serde( rename_all = "snake_case" ) ]
pub enum BatchProcessingStatus
{
/// Batch is currently being processed
InProgress,
/// Batch processing has completed (success or failure)
Ended,
/// Batch processing was canceled
Canceling,
/// Batch has expired
Expired,
}
/// Batch response from API
///
/// Returned when creating or retrieving batch status.
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq, Eq ) ]
pub struct BatchResponse
{
/// Unique batch identifier
pub id : String,
/// Object type (always `"message_batch"`)
pub r#type : String,
/// Current processing status
pub processing_status : BatchProcessingStatus,
/// Request count statistics
pub request_counts : RequestCounts,
/// When batch processing ended (if completed)
pub ended_at : Option< String >,
/// When batch was created
pub created_at : String,
/// When batch will expire
pub expires_at : String,
/// URL to download results (available when ended)
pub results_url : Option< String >,
}
impl BatchResponse
{
/// Check if batch processing is complete
#[ must_use ]
pub fn is_completed( &self ) -> bool
{
matches!( self.processing_status, BatchProcessingStatus::Ended )
}
/// Check if results are available for download
#[ must_use ]
pub fn has_results( &self ) -> bool
{
self.results_url.is_some()
}
/// Get total request count
#[ must_use ]
pub fn total_requests( &self ) -> u32
{
self.request_counts.processing
+ self.request_counts.succeeded
+ self.request_counts.errored
+ self.request_counts.canceled
+ self.request_counts.expired
}
}
/// Individual batch result from JSONL file
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct BatchResult
{
/// Custom ID from original request
pub custom_id : String,
/// Result type ("succeeded" or "errored")
pub result_type : String,
/// Message response (if succeeded)
pub message : Option< crate::CreateMessageResponse >,
/// Error information (if errored)
pub error : Option< BatchResultError >,
}
/// Error information in batch result
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq, Eq ) ]
pub struct BatchResultError
{
/// Error type
pub r#type : String,
/// Error message
pub message : String,
}
/// Batch list response
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq, Eq ) ]
pub struct BatchListResponse
{
/// List of batches
pub data : Vec< BatchResponse >,
/// Whether there are more results
pub has_more : bool,
/// ID of first item in list
pub first_id : Option< String >,
/// ID of last item in list
pub last_id : Option< String >,
}
}
#[ cfg( feature = "batch-processing" ) ]
crate::mod_interface!
{
exposed use
{
BatchRequestItem,
CreateBatchRequest,
RequestCounts,
BatchProcessingStatus,
BatchResponse,
BatchResult,
BatchResultError,
BatchListResponse,
};
}
#[ cfg( not( feature = "batch-processing" ) ) ]
crate::mod_interface!
{
// Empty - types not available without feature
}