diff options
| -rw-r--r-- | config.example.yaml | 9 | ||||
| -rw-r--r-- | src/client/bedrock.rs | 3 | ||||
| -rw-r--r-- | src/client/common.rs | 11 |
3 files changed, 20 insertions, 3 deletions
diff --git a/config.example.yaml b/config.example.yaml index 24e4cf9..7101f3b 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -118,6 +118,7 @@ clients: # connect_timeout: 10 # Set timeout in seconds for connect to api # timeout: 600 # Set total request timeout in seconds (0 or omitted disables) # retries: 3 # Number of retries after the first failed attempt (0 disables) + # idempotency: true # Send an Idempotency-Key header so retries reuse the same submission # retry_delay_ms: 1000 # Base delay between retries in milliseconds (default 1000) # retry_max_delay_ms: 60000 # Cap exponential backoff delay in milliseconds (default uncapped) @@ -125,9 +126,11 @@ clients: # A non-streaming request may hold one HTTP connection for minutes. If the # connection is dropped by an intermediary, the client otherwise waits # forever. Set `timeout` to bound each attempt, and `retries` to re-submit - # when an attempt fails. Be aware that retrying a non-idempotent request - # may resubmit the same work; prefer streaming (stream: true) for - # long-running requests so data flows continuously over the wire. + # when an attempt fails. Set `idempotency: true` (and `retries` > 0) so each + # retry reuses the same Idempotency-Key and the provider does not start + # duplicate work — required for providers such as Sail Research. Prefer + # streaming (stream: true) for long-running requests so data flows + # continuously over the wire. # See https://platform.openai.com/docs/quickstart - type: openai diff --git a/src/client/bedrock.rs b/src/client/bedrock.rs index 88d57d9..3f99713 100644 --- a/src/client/bedrock.rs +++ b/src/client/bedrock.rs @@ -61,6 +61,9 @@ impl BedrockClient { let mut request_data = RequestData::new("", body); self.patch_request_data(&mut request_data); + if self.extra_config().is_some_and(|v| v.idempotency()) { + request_data.header("idempotency-key", uuid::Uuid::new_v4().to_string()); + } let RequestData { url: _, headers, diff --git a/src/client/common.rs b/src/client/common.rs index a561ed4..d0c23e9 100644 --- a/src/client/common.rs +++ b/src/client/common.rs @@ -224,6 +224,9 @@ pub trait Client: Sync + Send { mut request_data: RequestData, ) -> RequestBuilder { self.patch_request_data(&mut request_data); + if self.extra_config().is_some_and(|v| v.idempotency()) { + request_data.header("idempotency-key", uuid::Uuid::new_v4().to_string()); + } request_data.into_builder(client) } @@ -273,6 +276,7 @@ pub struct ExtraConfig { pub connect_timeout: Option<u64>, pub timeout: Option<u64>, pub retries: Option<u64>, + pub idempotency: Option<bool>, pub retry_delay_ms: Option<u64>, pub retry_max_delay_ms: Option<u64>, } @@ -295,6 +299,13 @@ impl ExtraConfig { self.retries.unwrap_or(0) } + /// Whether to send an `Idempotency-Key` header so retries reuse the same + /// submission instead of starting duplicate work (supported by providers + /// such as Sail Research). Defaults to false. + pub fn idempotency(&self) -> bool { + self.idempotency.unwrap_or(false) + } + /// Base delay between retries in milliseconds. Defaults to 1000. pub fn retry_delay(&self) -> Duration { Duration::from_millis(self.retry_delay_ms.unwrap_or(1000)) |
