langchainrust 0.7.0

A LangChain-inspired framework for building LLM applications in Rust. Supports OpenAI, Agents, Tools, Memory, Chains, RAG, BM25, Hybrid Retrieval, LangGraph, HyDE, Reranking, MultiQuery, and native Function Calling.
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// src/core/batch/client.rs
//! BatchClient struct and all its method implementations.

use super::types::*;
use crate::core::language_models::LLMResult;
use crate::schema::Message;
use serde_json::json;

/// Client for submitting, polling, and retrieving batch results.
pub struct BatchClient {
    pub(crate) http: reqwest::Client,
    // M35: API key stored as String — could leak via debug/display.
    // Consider using a Zeroize wrapper or secret type in a future refactor.
    pub(crate) api_key: String,
    pub(crate) provider: BatchProvider,
    pub(crate) base_url: String,
}

impl BatchClient {
    /// Creates a new `BatchClient` for the given provider.
    pub fn new(provider: BatchProvider, api_key: impl Into<String>) -> Self {
        let base_url = match provider {
            BatchProvider::OpenAI => "https://api.openai.com/v1".to_string(),
            BatchProvider::Anthropic => "https://api.anthropic.com/v1".to_string(),
        };
        Self {
            http: reqwest::Client::new(),
            api_key: api_key.into(),
            provider,
            base_url,
        }
    }

    /// Overrides the base URL (useful for proxies or compatible endpoints).
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    // -- Auth header helpers ------------------------------------------------

    fn auth_headers(&self) -> Result<reqwest::header::HeaderMap, BatchError> {
        let mut headers = reqwest::header::HeaderMap::new();
        match self.provider {
            BatchProvider::OpenAI => {
                let value = format!("Bearer {}", self.api_key);
                let v = reqwest::header::HeaderValue::from_str(&value).map_err(|e| {
                    BatchError::Api(format!("invalid API key for Authorization header: {}", e))
                })?;
                headers.insert("Authorization", v);
            }
            BatchProvider::Anthropic => {
                let v = reqwest::header::HeaderValue::from_str(&self.api_key).map_err(|e| {
                    BatchError::Api(format!("invalid API key for x-api-key header: {}", e))
                })?;
                headers.insert("x-api-key", v);
                headers.insert(
                    "anthropic-version",
                    reqwest::header::HeaderValue::from_static("2023-06-01"),
                );
            }
        }
        Ok(headers)
    }

    // -- Message conversion -------------------------------------------------

    /// Convert a [`Message`] to the OpenAI chat format JSON value.
    pub(crate) fn message_to_openai(msg: &Message) -> serde_json::Value {
        match &msg.message_type {
            crate::schema::MessageType::System => json!({
                "role": "system",
                "content": msg.content,
            }),
            crate::schema::MessageType::Human => json!({
                "role": "user",
                "content": msg.content,
            }),
            crate::schema::MessageType::AI => {
                let mut m = json!({
                    "role": "assistant",
                    "content": msg.content,
                });
                if let Some(tc) = &msg.tool_calls {
                    m["tool_calls"] = serde_json::to_value(tc).unwrap_or(serde_json::Value::Null);
                }
                m
            }
            crate::schema::MessageType::Tool { tool_call_id } => json!({
                "role": "tool",
                "tool_call_id": tool_call_id,
                "content": msg.content,
            }),
        }
    }

    /// Convert a [`Message`] to the Anthropic chat format JSON value.
    ///
    /// Anthropic uses a `system` top-level parameter for system messages,
    /// but in the messages array it only accepts `user` and `assistant` roles.
    /// System messages are mapped to `user` with a `[System]` prefix to
    /// preserve the instruction while conforming to the API constraints.
    /// Tool results are mapped to `user` role per Anthropic's convention.
    pub(crate) fn message_to_anthropic(msg: &Message) -> serde_json::Value {
        match &msg.message_type {
            // H40: System messages should be extracted to top-level `system` parameter
            // by the caller (submit_anthropic), not mapped as user messages.
            // Here we map them as system role for identification.
            crate::schema::MessageType::System => json!({
                "role": "system",
                "content": msg.content,
            }),
            crate::schema::MessageType::Human => json!({
                "role": "user",
                "content": msg.content,
            }),
            crate::schema::MessageType::AI => json!({
                "role": "assistant",
                "content": msg.content,
            }),
            // H40: Tool messages should use Anthropic tool_result content block format
            crate::schema::MessageType::Tool { tool_call_id } => json!({
                "role": "user",
                "content": [{
                    "type": "tool_result",
                    "tool_use_id": tool_call_id,
                    "content": msg.content,
                }],
            }),
        }
    }

    // -- Submit -------------------------------------------------------------

    /// Submit a batch of requests. Returns the batch ID.
    ///
    /// For **OpenAI**: uploads a JSONL file via the Files API, then creates a
    /// batch via `POST /v1/batches` referencing the uploaded file.
    ///
    /// For **Anthropic**: creates a batch via `POST /v1/messages/batches` with
    /// the requests array inline.
    pub async fn submit(&self, requests: Vec<BatchRequest>) -> Result<BatchId, BatchError> {
        match self.provider {
            BatchProvider::OpenAI => self.submit_openai(requests).await,
            BatchProvider::Anthropic => self.submit_anthropic(requests).await,
        }
    }

    async fn submit_openai(&self, requests: Vec<BatchRequest>) -> Result<BatchId, BatchError> {
        // Build JSONL content for the input file.
        let jsonl_lines: Vec<String> = requests
            .iter()
            .map(|req| {
                let openai_msgs: Vec<serde_json::Value> =
                    req.messages.iter().map(Self::message_to_openai).collect();
                let mut body = json!({
                    "model": req.model,
                    "messages": openai_msgs,
                });
                if let Some(t) = req.temperature {
                    body["temperature"] = json!(t);
                }
                if let Some(m) = req.max_tokens {
                    body["max_tokens"] = json!(m);
                }
                let line = json!({
                    "custom_id": req.custom_id,
                    "method": "POST",
                    "url": "/v1/chat/completions",
                    "body": body,
                });
                // H39: Return error instead of empty string on serialization failure
                serde_json::to_string(&line).map_err(BatchError::Serialization)
            })
            .collect::<Result<Vec<String>, BatchError>>()?;
        let jsonl_content = jsonl_lines.join("\n");

        // Step 1: Upload the JSONL file.
        let file_id = self.upload_openai_file(&jsonl_content).await?;

        // Step 2: Create the batch.
        let headers = self.auth_headers()?;
        let body = json!({
            "input_file_id": file_id,
            "endpoint": "/v1/chat/completions",
            "completion_window": "24h",
        });

        let resp = self
            .http
            .post(format!("{}/batches", self.base_url))
            .headers(headers)
            .json(&body)
            .send()
            .await?;

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(BatchError::NotFound(
                "batches endpoint not found".to_string(),
            ));
        }

        let status = resp.status();
        let text = resp.text().await?;

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "batch creation failed ({}): {}",
                status, text
            )));
        }

        let batch: OpenAIBatchResponse = serde_json::from_str(&text)?;
        Ok(BatchId(batch.id))
    }

    /// Upload a JSONL file to the OpenAI Files API and return the file ID.
    async fn upload_openai_file(&self, jsonl_content: &str) -> Result<String, BatchError> {
        let headers = self.auth_headers()?;
        let file_part = reqwest::multipart::Part::text(jsonl_content.to_string())
            .file_name("batch_input.jsonl")
            .mime_str("application/jsonl")
            .map_err(|e| BatchError::Api(format!("mime error: {e}")))?;

        let form = reqwest::multipart::Form::new()
            .text("purpose", "batch")
            .part("file", file_part);

        let resp = self
            .http
            .post(format!("{}/files", self.base_url))
            .headers(headers)
            .multipart(form)
            .send()
            .await?;

        let status = resp.status();
        let text = resp.text().await?;

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "file upload failed ({}): {}",
                status, text
            )));
        }

        let file_resp: OpenAIFileResponse = serde_json::from_str(&text)?;
        Ok(file_resp.id)
    }

    async fn submit_anthropic(&self, requests: Vec<BatchRequest>) -> Result<BatchId, BatchError> {
        let req_items: Vec<serde_json::Value> = requests
            .iter()
            .map(|req| {
                // H40: Extract system messages to top-level `system` parameter,
                // and filter them from the messages array (Anthropic API requirement).
                let mut system_text = String::new();
                let anthropic_msgs: Vec<serde_json::Value> = req
                    .messages
                    .iter()
                    .filter_map(|msg| {
                        if matches!(msg.message_type, crate::schema::MessageType::System) {
                            if !system_text.is_empty() {
                                system_text.push('\n');
                            }
                            system_text.push_str(&msg.content);
                            None
                        } else {
                            Some(Self::message_to_anthropic(msg))
                        }
                    })
                    .collect();
                let mut body = json!({
                    "model": req.model,
                    "max_tokens": req.max_tokens.unwrap_or(4096),
                    "messages": anthropic_msgs,
                });
                if !system_text.is_empty() {
                    body["system"] = json!(system_text);
                }
                if let Some(t) = req.temperature {
                    body["temperature"] = json!(t);
                }
                json!({
                    "custom_id": req.custom_id,
                    "params": body,
                })
            })
            .collect();

        let headers = self.auth_headers()?;
        let body = json!({
            "requests": req_items,
        });

        let resp = self
            .http
            .post(format!("{}/messages/batches", self.base_url))
            .headers(headers)
            .json(&body)
            .send()
            .await?;

        let status = resp.status();
        let text = resp.text().await?;

        if status == reqwest::StatusCode::NOT_FOUND {
            return Err(BatchError::NotFound(
                "messages/batches endpoint not found".to_string(),
            ));
        }

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "batch creation failed ({}): {}",
                status, text
            )));
        }

        let batch: AnthropicBatchResponse = serde_json::from_str(&text)?;
        Ok(BatchId(batch.id))
    }

    // -- Poll ---------------------------------------------------------------

    /// Poll the status of a batch job.
    pub async fn poll(&self, id: &BatchId) -> Result<BatchStatus, BatchError> {
        match self.provider {
            BatchProvider::OpenAI => self.poll_openai(id).await,
            BatchProvider::Anthropic => self.poll_anthropic(id).await,
        }
    }

    async fn poll_openai(&self, id: &BatchId) -> Result<BatchStatus, BatchError> {
        let headers = self.auth_headers()?;
        let resp = self
            .http
            .get(format!("{}/batches/{}", self.base_url, id.0))
            .headers(headers)
            .send()
            .await?;

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(BatchError::NotFound(id.0.clone()));
        }

        let status = resp.status();
        let text = resp.text().await?;

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "poll failed ({}): {}",
                status, text
            )));
        }

        let batch: OpenAIBatchResponse = serde_json::from_str(&text)?;
        Ok(match batch.status.as_str() {
            "in_progress" | "validating" | "finalizing" => BatchStatus::InProgress,
            "completed" => BatchStatus::Completed,
            "failed" => BatchStatus::Failed,
            "expired" => BatchStatus::Expired,
            "cancelling" | "cancelled" => BatchStatus::Cancelled,
            other => return Err(BatchError::Api(format!("unknown batch status: {}", other))),
        })
    }

    async fn poll_anthropic(&self, id: &BatchId) -> Result<BatchStatus, BatchError> {
        let headers = self.auth_headers()?;
        let resp = self
            .http
            .get(format!("{}/messages/batches/{}", self.base_url, id.0))
            .headers(headers)
            .send()
            .await?;

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(BatchError::NotFound(id.0.clone()));
        }

        let status = resp.status();
        let text = resp.text().await?;

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "poll failed ({}): {}",
                status, text
            )));
        }

        let batch: AnthropicBatchResponse = serde_json::from_str(&text)?;
        let proc = batch.processing_status.as_deref().unwrap_or("in_progress");
        Ok(match proc {
            "in_progress" => BatchStatus::InProgress,
            "ended" => {
                // Determine the terminal state from request_counts.
                if let Some(counts) = batch.request_counts {
                    if counts.errored > 0 && counts.succeeded == 0 {
                        BatchStatus::Failed
                    } else if counts.expired > 0 && counts.succeeded == 0 {
                        BatchStatus::Expired
                    } else {
                        BatchStatus::Completed
                    }
                } else {
                    BatchStatus::Completed
                }
            }
            other => {
                return Err(BatchError::Api(format!(
                    "unknown processing_status: {}",
                    other
                )))
            }
        })
    }

    // -- Results ------------------------------------------------------------

    /// Retrieve results of a completed batch.
    pub async fn results(&self, id: &BatchId) -> Result<Vec<BatchResult>, BatchError> {
        match self.provider {
            BatchProvider::OpenAI => self.results_openai(id).await,
            BatchProvider::Anthropic => self.results_anthropic(id).await,
        }
    }

    async fn results_openai(&self, id: &BatchId) -> Result<Vec<BatchResult>, BatchError> {
        // First, get the batch metadata to find the output file ID.
        let headers = self.auth_headers()?;
        let resp = self
            .http
            .get(format!("{}/batches/{}", self.base_url, id.0))
            .headers(headers.clone())
            .send()
            .await?;

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(BatchError::NotFound(id.0.clone()));
        }

        let status = resp.status();
        let text = resp.text().await?;

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "fetch batch metadata failed ({}): {}",
                status, text
            )));
        }

        let batch: OpenAIBatchResponse = serde_json::from_str(&text)?;

        // If the batch has an error file but no output file, report failure.
        let output_file_id = match batch.output_file_id {
            Some(fid) => fid,
            None => {
                if let Some(err_fid) = batch.error_file_id {
                    // Download the error file for details.
                    return self.download_openai_error_file(&err_fid, &headers).await;
                }
                return Err(BatchError::Failed(
                    "batch has no output file and no error file".to_string(),
                ));
            }
        };

        // Download the output JSONL file.
        let file_resp = self
            .http
            .get(format!(
                "{}/files/{}/content",
                self.base_url, output_file_id
            ))
            .headers(headers)
            .send()
            .await?;

        let file_status = file_resp.status();
        let file_text = file_resp.text().await?;

        if !file_status.is_success() {
            return Err(BatchError::Api(format!(
                "download output file failed ({}): {}",
                file_status, file_text
            )));
        }

        self.parse_openai_results_jsonl(&file_text)
    }

    pub(crate) fn parse_openai_results_jsonl(
        &self,
        text: &str,
    ) -> Result<Vec<BatchResult>, BatchError> {
        let mut results = Vec::new();
        for line in text.lines() {
            if line.trim().is_empty() {
                continue;
            }
            let parsed: OpenAIResultLine = serde_json::from_str(line)?;
            let result = if let Some(err) = parsed.error {
                Err(err.message.unwrap_or_else(|| "unknown error".to_string()))
            } else if let Some(resp_body) = parsed.response {
                if let Some(inner) = resp_body.body {
                    let content = inner
                        .choices
                        .first()
                        .and_then(|c| c.message.as_ref())
                        .and_then(|m| m.content.clone())
                        .unwrap_or_default();

                    let token_usage =
                        inner
                            .usage
                            .map(|u| crate::core::language_models::TokenUsage {
                                prompt_tokens: u.prompt_tokens,
                                completion_tokens: u.completion_tokens,
                                total_tokens: u.total_tokens,
                            });

                    Ok(LLMResult {
                        content,
                        model: inner.model.unwrap_or_default(),
                        token_usage,
                        tool_calls: None,
                        thinking_content: None,
                    })
                } else {
                    Err("empty response body".to_string())
                }
            } else {
                Err("no response and no error".to_string())
            };
            results.push(BatchResult {
                custom_id: parsed.custom_id,
                result,
            });
        }
        Ok(results)
    }

    async fn download_openai_error_file(
        &self,
        error_file_id: &str,
        headers: &reqwest::header::HeaderMap,
    ) -> Result<Vec<BatchResult>, BatchError> {
        let resp = self
            .http
            .get(format!("{}/files/{}/content", self.base_url, error_file_id))
            .headers(headers.clone())
            .send()
            .await?;

        let status = resp.status();
        let text = resp.text().await?;

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "download error file failed ({}): {}",
                status, text
            )));
        }

        // Parse error JSONL the same way as output.
        self.parse_openai_results_jsonl(&text)
    }

    async fn results_anthropic(&self, id: &BatchId) -> Result<Vec<BatchResult>, BatchError> {
        let headers = self.auth_headers()?;
        let resp = self
            .http
            .get(format!(
                "{}/messages/batches/{}/results",
                self.base_url, id.0
            ))
            .headers(headers)
            .send()
            .await?;

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(BatchError::NotFound(id.0.clone()));
        }

        let status = resp.status();
        let text = resp.text().await?;

        if !status.is_success() {
            return Err(BatchError::Api(format!(
                "fetch results failed ({}): {}",
                status, text
            )));
        }

        self.parse_anthropic_results_jsonl(&text)
    }

    pub(crate) fn parse_anthropic_results_jsonl(
        &self,
        text: &str,
    ) -> Result<Vec<BatchResult>, BatchError> {
        let mut results = Vec::new();
        for line in text.lines() {
            if line.trim().is_empty() {
                continue;
            }
            let parsed: AnthropicResultLine = serde_json::from_str(line)?;
            let result = match parsed.result.result_type.as_str() {
                "succeeded" => {
                    if let Some(msg) = parsed.result.message {
                        let content = msg
                            .content
                            .iter()
                            .filter_map(|b| {
                                if b.block_type == "text" {
                                    b.text.clone()
                                } else {
                                    None
                                }
                            })
                            .collect::<Vec<_>>()
                            .join("");

                        let token_usage =
                            msg.usage.map(|u| crate::core::language_models::TokenUsage {
                                prompt_tokens: u.input_tokens,
                                completion_tokens: u.output_tokens,
                                total_tokens: u.input_tokens + u.output_tokens,
                            });

                        Ok(LLMResult {
                            content,
                            model: msg.model,
                            token_usage,
                            tool_calls: None,
                            thinking_content: None,
                        })
                    } else {
                        Err("succeeded result missing message body".to_string())
                    }
                }
                "errored" => {
                    let err_msg = parsed
                        .result
                        .error
                        .and_then(|e| e.message)
                        .unwrap_or_else(|| "unknown error".to_string());
                    Err(err_msg)
                }
                "expired" => Err("request expired".to_string()),
                "canceled" => Err("request canceled".to_string()),
                other => Err(format!("unknown result type: {}", other)),
            };
            results.push(BatchResult {
                custom_id: parsed.custom_id,
                result,
            });
        }
        Ok(results)
    }

    // -- Cancel -------------------------------------------------------------

    /// Cancel a batch job.
    pub async fn cancel(&self, id: &BatchId) -> Result<(), BatchError> {
        match self.provider {
            BatchProvider::OpenAI => self.cancel_openai(id).await,
            BatchProvider::Anthropic => Err(BatchError::Api(
                "Anthropic batch API does not support cancellation".to_string(),
            )),
        }
    }

    async fn cancel_openai(&self, id: &BatchId) -> Result<(), BatchError> {
        let headers = self.auth_headers()?;
        let resp = self
            .http
            .post(format!("{}/batches/{}/cancel", self.base_url, id.0))
            .headers(headers)
            .send()
            .await?;

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(BatchError::NotFound(id.0.clone()));
        }

        let status = resp.status();
        if !status.is_success() {
            let text = resp.text().await.unwrap_or_default();
            return Err(BatchError::Api(format!(
                "cancel failed ({}): {}",
                status, text
            )));
        }

        Ok(())
    }

    // -- Convenience --------------------------------------------------------

    /// Submit and wait until complete, then return results.
    ///
    /// Polls every `poll_interval_ms` milliseconds. Returns
    /// [`BatchError::Timeout`] if `max_wait_ms` is exceeded.
    pub async fn submit_and_wait(
        &self,
        requests: Vec<BatchRequest>,
        poll_interval_ms: u64,
        max_wait_ms: u64,
    ) -> Result<Vec<BatchResult>, BatchError> {
        let batch_id = self.submit(requests).await?;
        let start = std::time::Instant::now();
        let poll_duration = std::time::Duration::from_millis(poll_interval_ms);
        let max_duration = std::time::Duration::from_millis(max_wait_ms);

        loop {
            let status = self.poll(&batch_id).await?;

            match status {
                BatchStatus::Completed => {
                    return self.results(&batch_id).await;
                }
                BatchStatus::Failed => {
                    return Err(BatchError::Failed(format!("batch {} failed", batch_id.0)));
                }
                BatchStatus::Expired => {
                    return Err(BatchError::Expired);
                }
                BatchStatus::Cancelled => {
                    return Err(BatchError::Api(format!(
                        "batch {} was cancelled",
                        batch_id.0
                    )));
                }
                BatchStatus::InProgress => {
                    // Continue polling.
                }
            }

            if start.elapsed() >= max_duration {
                return Err(BatchError::Timeout(max_wait_ms));
            }

            tokio::time::sleep(poll_duration).await;
        }
    }
}