rig-bedrock 0.46.0

AWS Bedrock model provider for Rig integration.
Documentation
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
//! The Bedrock Converse completion wire and model identifiers.
//! Model availability and inference-profile support depend on the AWS region.
//!
//! A request is built as Converse JSON and a reply is read as the JSON
//! Bedrock sent. The AWS SDK signs and transports both; its typed request
//! and response shapes play no part.
//!
//! ```no_run
//! use rig_bedrock::{client::BedrockRuntime, completion::{AMAZON_NOVA_LITE, Converse}};
//! use rig_core::Model;
//!
//! let model = BedrockRuntime::from_env().completion(AMAZON_NOVA_LITE);
//! # let _ = model;
//! ```

use aws_sdk_bedrockruntime::config::http::HttpResponse;
use aws_sdk_bedrockruntime::error::{ProvideErrorMetadata, SdkError};
use aws_sdk_bedrockruntime::operation::RequestId;
use rig_core::catalog::{ModelFacts, ModelSpec};
use rig_core::completion::options::FinalBody;
use rig_core::completion::{Accepts, CompletionRequest, Media, Pairing, ReplayTarget};
use rig_core::driver::{Exchange, Opened, Opening, Transport};
use rig_core::error::{EncodeError, ProviderError};
use rig_core::json_utils::Lenient;
use rig_core::message::{Api, DocumentData, DocumentSourceKind, Origin, ToolChoice};
use rig_core::operation::Completion;
use rig_core::wire::{Descriptor, Mode, Wire};
use serde_json::Value;

use crate::capture::{self, Capture, Events};
use crate::client::BedrockRuntime;
use crate::request;
use crate::streaming::StreamState;
use crate::types::errors::sdk_error;

/// Stable descriptor name reported on normalized Bedrock responses.
pub const PROVIDER_NAME: &str = "aws_bedrock";

// Profile identifiers with a us. prefix route inference within the US region
// family; callers elsewhere must select a supported regional profile.

/// `amazon.nova-lite-v1:0`
pub const AMAZON_NOVA_LITE: &str = "amazon.nova-lite-v1:0";
/// `amazon.nova-micro-v1:0`
pub const AMAZON_NOVA_MICRO: &str = "amazon.nova-micro-v1:0";
/// `amazon.nova-pro-v1:0`
pub const AMAZON_NOVA_PRO: &str = "amazon.nova-pro-v1:0";

/// `us.anthropic.claude-haiku-4-5-20251001-v1:0` (cross-region profile)
pub const ANTHROPIC_CLAUDE_HAIKU_4_5: &str = "us.anthropic.claude-haiku-4-5-20251001-v1:0";
/// `us.anthropic.claude-sonnet-4-5-20250929-v1:0` (cross-region profile)
pub const ANTHROPIC_CLAUDE_SONNET_4_5: &str = "us.anthropic.claude-sonnet-4-5-20250929-v1:0";
/// `us.anthropic.claude-opus-4-5-20251101-v1:0` (cross-region profile)
pub const ANTHROPIC_CLAUDE_OPUS_4_5: &str = "us.anthropic.claude-opus-4-5-20251101-v1:0";
/// `us.anthropic.claude-sonnet-4-6` (cross-region profile)
pub const ANTHROPIC_CLAUDE_SONNET_4_6: &str = "us.anthropic.claude-sonnet-4-6";
/// `us.anthropic.claude-sonnet-5` (cross-region profile)
pub const ANTHROPIC_CLAUDE_SONNET_5: &str = "us.anthropic.claude-sonnet-5";
/// `us.anthropic.claude-opus-5` (cross-region profile)
pub const ANTHROPIC_CLAUDE_OPUS_5: &str = "us.anthropic.claude-opus-5";

/// `us.deepseek.r1-v1:0` (cross-region profile)
pub const DEEPSEEK_R1: &str = "us.deepseek.r1-v1:0";

/// `meta.llama3-8b-instruct-v1:0`
pub const LLAMA_3_8B_INSTRUCT: &str = "meta.llama3-8b-instruct-v1:0";
/// `meta.llama3-70b-instruct-v1:0`
pub const LLAMA_3_70B_INSTRUCT: &str = "meta.llama3-70b-instruct-v1:0";
/// `meta.llama3-1-8b-instruct-v1:0`
pub const LLAMA_3_1_8B_INSTRUCT: &str = "meta.llama3-1-8b-instruct-v1:0";
/// `meta.llama3-1-70b-instruct-v1:0`
pub const LLAMA_3_1_70B_INSTRUCT: &str = "meta.llama3-1-70b-instruct-v1:0";
/// `us.meta.llama3-3-70b-instruct-v1:0` (cross-region profile)
pub const META_LLAMA_3_3_70B_INSTRUCT: &str = "us.meta.llama3-3-70b-instruct-v1:0";
/// `us.meta.llama4-maverick-17b-instruct-v1:0` (cross-region profile)
pub const META_LLAMA_4_MAVERICK_17B_INSTRUCT: &str = "us.meta.llama4-maverick-17b-instruct-v1:0";
/// `us.meta.llama4-scout-17b-instruct-v1:0` (cross-region profile)
pub const META_LLAMA_4_SCOUT_17B_INSTRUCT: &str = "us.meta.llama4-scout-17b-instruct-v1:0";

/// `mistral.mistral-7b-instruct-v0:2`
pub const MISTRAL_7B_INSTRUCT: &str = "mistral.mistral-7b-instruct-v0:2";
/// `mistral.mistral-large-2402-v1:0`
pub const MISTRAL_LARGE_24_02: &str = "mistral.mistral-large-2402-v1:0";
/// `mistral.mistral-small-2402-v1:0`
pub const MISTRAL_SMALL_24_02: &str = "mistral.mistral-small-2402-v1:0";
/// `mistral.mixtral-8x7b-instruct-v0:1`
pub const MISTRAL_MIXTRAL_8X7B_INSTRUCT_V0: &str = "mistral.mixtral-8x7b-instruct-v0:1";
/// `us.mistral.pixtral-large-2502-v1:0` (cross-region profile)
pub const MISTRAL_PIXTRAL_LARGE_2502: &str = "us.mistral.pixtral-large-2502-v1:0";

/// `us.writer.palmyra-x4-v1:0` (cross-region profile)
pub const WRITER_PALMYRA_X4: &str = "us.writer.palmyra-x4-v1:0";
/// `us.writer.palmyra-x5-v1:0` (cross-region profile)
pub const WRITER_PALMYRA_X5: &str = "us.writer.palmyra-x5-v1:0";

/// The model family behind a Converse model id. It decides what history a
/// model reads back: Claude reads reasoning signatures, rejects unsigned
/// reasoning, and reads images; Nova reads S3 objects and video; both read
/// a tool result's status.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Family {
    /// Anthropic Claude.
    Claude,
    /// Amazon Nova.
    Nova,
    /// Any other model.
    Other,
}

impl Family {
    /// The family of the provider a Bedrock model id names. A base model id
    /// or a system inference profile is `[geography.]provider.model`, also
    /// the last part of a foundation-model or inference-profile ARN. An
    /// application inference profile or provisioned model ARN names no
    /// provider, so it is [`Family::Other`] unless the caller states its
    /// family with [`Converse::with_family`].
    ///
    /// ```
    /// use rig_bedrock::completion::{AMAZON_NOVA_PRO, ANTHROPIC_CLAUDE_SONNET_4_5, Family};
    ///
    /// assert_eq!(Family::of(ANTHROPIC_CLAUDE_SONNET_4_5), Family::Claude);
    /// assert_eq!(Family::of(AMAZON_NOVA_PRO), Family::Nova);
    /// assert_eq!(
    ///     Family::of("arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/a1b2c3"),
    ///     Family::Other
    /// );
    /// ```
    pub fn of(model: &str) -> Self {
        let id = model.rsplit('/').next().unwrap_or(model);
        let mut parts = id.rsplit('.');
        match (parts.next(), parts.next()) {
            (_, Some("anthropic")) => Self::Claude,
            (Some(name), Some("amazon")) if name.starts_with("nova") => Self::Nova,
            _ => Self::Other,
        }
    }
}

/// The Converse endpoint for one model: `Converse` for a unary call,
/// `ConverseStream` for a streamed one.
#[derive(Clone, Debug)]
pub struct Converse {
    pub model: String,
    /// The family of `model` when the caller states it, which an
    /// application inference profile ARN needs: its id names no provider.
    /// Set through [`Converse::with_family`]; otherwise [`Family::of`]
    /// the model id decides.
    pub family: Option<Family>,
    /// The model facts the encoder reads and replies are priced by.
    pub facts: ModelFacts,
}

impl Converse {
    pub fn new(model: impl Into<String>) -> Self {
        Self {
            model: model.into(),
            family: None,
            facts: ModelFacts::default(),
        }
    }

    /// The same wire, encoding with `facts` and pricing its replies by
    /// them.
    pub fn with_facts(mut self, facts: ModelFacts) -> Self {
        self.facts = facts;
        self
    }

    /// State the family of this wire's model, for a model id that names no
    /// provider, such as an application inference profile ARN.
    ///
    /// ```
    /// use rig_bedrock::completion::{Converse, Family};
    ///
    /// let profile = "arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/a1b2c3";
    /// let wire = Converse::new(profile).with_family(Family::Claude);
    /// assert_eq!(wire.family(profile), Family::Claude);
    /// ```
    pub fn with_family(mut self, family: Family) -> Self {
        self.family = Some(family);
        self
    }

    /// The family of `model`: the stated one for this wire's own model,
    /// otherwise [`Family::of`] its id.
    pub fn family(&self, model: &str) -> Family {
        match self.family {
            Some(family) if model == self.model => family,
            _ => Family::of(model),
        }
    }
}

/// One Converse request: the model it addresses and its JSON body.
#[derive(Clone, Debug)]
pub struct ConverseRequest {
    pub model: String,
    pub body: FinalBody,
}

/// One unit of a Converse reply, as the JSON Bedrock sent.
#[derive(Clone, Debug)]
pub enum ConverseFrame {
    /// A whole unary reply. It is also the response's `raw`.
    Whole(Value),
    /// One stream event or in-band exception, as `{"<type>": <payload>}`.
    Event(Value),
}

impl Wire for Converse {
    type Op = Completion;
    type Payload = ConverseRequest;
    type Frame = ConverseFrame;
    type Event = ConverseFrame;
    type Decoder<'id> = StreamState;
    type Reassembler = crate::streaming::document::ConverseOutput;

    fn describe(&self) -> Descriptor<'_> {
        Descriptor::new(PROVIDER_NAME)
            .model(self.model.as_str())
            .facts(&self.facts)
            .replay(self)
    }

    fn encode(
        &self,
        request: CompletionRequest,
        _mode: Mode,
    ) -> Result<ConverseRequest, EncodeError> {
        let model = request.model.clone().unwrap_or_else(|| self.model.clone());
        let body = request::body(self, &request, &model)?;
        Ok(ConverseRequest { model, body })
    }

    fn decoder<'id>(&self) -> Self::Decoder<'id> {
        StreamState::default()
    }
}

/// Model families pi's catalog lists as text-only on Bedrock, separated by
/// spaces: the rule for a model the catalog does not list under Bedrock (a
/// region inference profile, a dated or `-v2` revision).
const TEXT_ONLY: &str = "amazon.nova-micro deepseek. meta.llama3-8b meta.llama3-70b \
    meta.llama3-1- meta.llama3-3- minimax. mistral.devstral mistral.mistral-7b \
    mistral.mistral-large-2402 mistral.mistral-small-2402 mistral.mixtral mistral.voxtral \
    moonshot.kimi-k2-thinking nvidia.nemotron-nano-3 nvidia.nemotron-nano-9b \
    nvidia.nemotron-super openai.gpt-oss qwen.qwen3-2 qwen.qwen3-3 qwen.qwen3-coder \
    qwen.qwen3-next writer.palmyra zai.glm";

impl Converse {
    /// The facts the encoder reads for the Bedrock `model`: a base model id
    /// or inference profile, or the last part of its ARN, looked up under
    /// Bedrock in the wire's facts ([`ModelFacts::for_model`]). An id they
    /// do not list is read as Bedrock names models: a cross-region profile
    /// (`jp.<base id>`) as its base model, and a `-vN[:M]` revision as the
    /// model it revises. A Claude row carries the Anthropic model's
    /// reasoning options and wire facts, so its reasoning, sampling and
    /// context binding match Anthropic's own API. `None` for a model the
    /// catalog does not list.
    pub fn spec(&self, model: &str) -> Option<&ModelSpec> {
        spec_in(&self.facts, model)
    }
}

/// [`Converse::spec`] in `facts`.
fn spec_in<'f>(facts: &'f ModelFacts, model: &str) -> Option<&'f ModelSpec> {
    let id = model.rsplit('/').next().unwrap_or(model);
    let base = id
        .split_once('.')
        .filter(|(geo, base)| {
            !geo.is_empty()
                && geo.bytes().all(|byte| byte.is_ascii_lowercase())
                && base.contains('.')
        })
        .map(|(_, base)| base);
    [Some(id), base]
        .into_iter()
        .flatten()
        .flat_map(|id| [Some(id), unrevised(id)])
        .flatten()
        .find_map(|id| facts.for_model(PROVIDER_NAME, id))
}

/// `id` without a trailing Bedrock revision, `-v1` or `-v1:0`.
fn unrevised(id: &str) -> Option<&str> {
    let (model, revision) = id.rsplit_once("-v")?;
    let digits = |part: &str| !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit());
    let (major, minor) = revision.split_once(':').unwrap_or((revision, "0"));
    (digits(major) && digits(minor)).then_some(model)
}

impl ReplayTarget for Converse {
    /// Section 6.5 of the typed-options design: reasoning in the model's
    /// own request fields, by family and Claude class.
    fn map_options(
        &self,
        request: &CompletionRequest,
        fields: rig_core::completion::options::OptionFields<'_>,
    ) -> rig_core::completion::options::OptionMap {
        let model = request.model.as_deref().unwrap_or(&self.model);
        crate::options::converse(self, self.family(model), model, request, fields)
    }

    fn facts(&self) -> Option<&ModelFacts> {
        Some(&self.facts)
    }

    fn api(&self) -> Api {
        Api::from_static("bedrock.converse")
    }

    fn provider(&self) -> &str {
        PROVIDER_NAME
    }

    fn model(&self) -> &str {
        &self.model
    }

    /// Converse reads images in user turns and tool results, never in
    /// assistant turns. Claude reads them; so does every other model but
    /// those the catalog lists as reading no images, or, for a model the
    /// catalog does not list (a region inference profile such as
    /// `eu.meta.llama3-3-70b-instruct-v1:0`), the text-only families.
    fn accepts(&self, model: &str) -> Accepts {
        let images = self.family(model) == Family::Claude
            || self.spec(model).map_or_else(
                || {
                    !TEXT_ONLY
                        .split_whitespace()
                        .any(|family| model.contains(family))
                },
                |spec| spec.input.image,
            );
        Accepts {
            user_images: images,
            assistant_images: false,
            tool_result_images: images,
            tools: true,
            // Converse replays reasoning only with its signature.
            foreign_reasoning: false,
        }
    }

    /// Claude binds its thinking to the request's tools and system prompt
    /// on Converse as on Anthropic's own API.
    fn binds_context(&self, model: &str) -> bool {
        self.family(model) == Family::Claude
            && self
                .spec(model)
                .is_some_and(|spec| spec.compat.binds_context)
    }

    /// Converse rejects a conversation that does not start with a user
    /// message.
    fn starts_with_user(&self) -> bool {
        true
    }

    /// A later system message goes as user text where it stands, so adding
    /// one never changes the cached prefix before it.
    fn later_system(&self, _model: &str) -> rig_core::completion::LaterSystem {
        rig_core::completion::LaterSystem::UserText
    }

    /// Converse takes user and assistant messages only in alternation.
    fn alternates_roles(&self) -> bool {
        true
    }

    /// Converse takes a hosted tool's use and result only beside a
    /// `toolConfig`, which only the request's own tools make.
    fn hosted_needs_tools(&self) -> bool {
        true
    }

    /// Only the request's tools reach Converse's `toolConfig`;
    /// `additional_params` go to `additionalModelRequestFields`. Converse
    /// has no `none` tool choice, so `ToolChoice::None` sends no
    /// `toolConfig`, and Converse rejects tool blocks without one.
    fn declares_tools(&self, request: &CompletionRequest) -> bool {
        !request.tools.is_empty() && !matches!(request.tool_choice, Some(ToolChoice::None))
    }

    fn sends_alone(&self, block: &rig_core::message::AssistantContent) -> bool {
        crate::request::sends(block, self)
    }

    /// Converse carries images in its four formats, documents in a format it
    /// lists, and inline data, which must be valid base64. Only Nova reads S3
    /// objects and video. Converse rejects a document's text source and
    /// takes no audio, so a text document goes as its text.
    fn encodes(&self, model: &str, media: Media<'_>) -> bool {
        let nova = self.family(model) == Family::Nova;
        let stored = |data: &DocumentSourceKind| {
            nova || !matches!(data, DocumentSourceKind::Url(url) if url.starts_with("s3://"))
        };
        match media {
            Media::Image(image, _) => stored(&image.data) && request::image(image).is_ok(),
            Media::Document(document) => {
                matches!(&document.data, DocumentData::File(data) if stored(data))
                    && request::document(document).is_ok()
            }
            Media::Video(video) => {
                nova && self.accepts(model).user_images && request::video(video).is_ok()
            }
            Media::Audio(_) => false,
        }
    }

    /// Converse tool-use ids match `[a-zA-Z0-9_-]{1,64}`.
    fn normalize_tool_call_id(&self, id: &str, _model: &str, _source: Option<&Origin>) -> String {
        rig_core::providers::internal::wire_ids::legal_call_id(id, 64)
    }

    fn call_id_slot(&self) -> Option<&'static str> {
        Some("/toolUse/toolUseId")
    }

    fn blob_slots(&self) -> &'static [&'static str] {
        &[
            "/reasoningContent/reasoningText/signature",
            "/reasoningContent/redactedContent",
        ]
    }

    /// A hosted tool's use is a typed `toolUse`; its result is the
    /// `toolResult` in the same turn. Converse rejects one without the other.
    fn hosted_pair(&self, item: &Value) -> Option<(Pairing, String)> {
        let (side, body) = match item.get("toolUse") {
            Some(body) => (Pairing::Use, body),
            None => (Pairing::Result, item.get("toolResult")?),
        };
        Some((side, body.str("toolUseId")?.to_owned()))
    }
}

/// The request id of a sent Converse call, and its failure, if the SDK
/// read one.
fn sent<O: RequestId, E: ProvideErrorMetadata>(
    sent: Result<O, SdkError<E, HttpResponse>>,
) -> (Option<String>, Option<ProviderError>) {
    match sent {
        Ok(output) => (output.request_id().map(str::to_owned), None),
        Err(error) => (
            error.request_id().map(str::to_owned),
            Some(sdk_error(error)),
        ),
    }
}

impl Transport<Converse> for BedrockRuntime {
    fn send(&self, payload: ConverseRequest, exchange: Exchange) -> Opening<ConverseFrame> {
        let ConverseRequest { model, body } = payload;
        let capture = match serde_json::to_vec(&body) {
            Ok(body) => Capture::new(body),
            Err(error) => return Opening::failed(ProviderError::request(error)),
        };
        let runtime = self.clone();
        Opening::new(async move {
            let client = runtime.inner().await;
            let unary = exchange.mode == Mode::Unary;
            let interceptor = capture.clone();
            let (request_id, failure) = if unary {
                let call = client.converse().model_id(model).customize();
                sent(call.interceptor(interceptor).send().await)
            } else {
                let call = client.converse_stream().model_id(model).customize();
                sent(call.interceptor(interceptor).send().await)
            };
            // A success's body is never the SDK's to read, so the SDK fails a
            // unary call it could not deserialize; the reply is decoded here.
            let Some(mut body) = capture.reply() else {
                let failure = failure.unwrap_or_else(|| {
                    ProviderError::Response("Converse sent no reply".to_owned())
                });
                return Ok(Opened::failed(failure));
            };
            if unary {
                let mut bytes = Vec::new();
                while let Some(chunk) = capture::chunk(&mut body).await {
                    bytes.extend(chunk?);
                }
                let document: Value = serde_json::from_slice(&bytes)
                    .map_err(|error| ProviderError::Json(error.into()))?;
                let frames = futures::stream::iter([Ok(ConverseFrame::Whole(document.clone()))]);
                return Ok(Opened::new(frames)
                    .with_request_id(request_id)
                    .with_document(document));
            }
            let frames = async_stream::stream! {
                let mut events = Events::default();
                while let Some(chunk) = capture::chunk(&mut body).await {
                    match chunk {
                        Ok(chunk) => {
                            for event in events.read(&chunk) {
                                yield Ok(ConverseFrame::Event(event));
                            }
                        }
                        Err(error) => {
                            yield Err(error);
                            break;
                        }
                    }
                }
            };
            Ok(Opened::new(frames).with_request_id(request_id))
        })
    }
}

#[cfg(test)]
mod tests;