Bedrock Invoke class for invoking models without Converse API (e.g. Bedrock Custom Model Import)#1217
Bedrock Invoke class for invoking models without Converse API (e.g. Bedrock Custom Model Import)#1217dgallitelli wants to merge 1 commit into
Conversation
|
Note: |
|
Do we have any update on when this PR will be merged? This will unlock the ability to leverage the new opus 4.5 features like tool search, lazy loading of tools, etc. in Bedrock. Currently it's not possible since only supported via invoke api. |
|
+1 any update from the Strands team on this? |
Updates since original submissionRebased onto current Rebase / merge conflict (
|
|
Pushed a major rework of this PR (force-push, since the head ref is What changed since the last review stateRebased onto current Aligned with current SDK contracts:
Streaming protocol (this is the bulk of the diff):
Tool use & ToolChoice:
Other correctness fixes:
Tests:
Local verificationAbout the still-failing checksThe three failing checks ( cc @pgrayy @Unshure @zastrowm — would appreciate another look. Happy to split this into smaller commits if that's easier to review. |
Adds a new ``BedrockModelInvoke`` provider that talks to Bedrock's native ``InvokeModel``/``InvokeModelWithResponseStream`` APIs instead of ``Converse``/``ConverseStream``. This makes Strands usable with models that do not support Converse, most notably Bedrock Custom Model Import (Llama, Mistral, Qwen, ...) and Anthropic models accessed via the Messages API. Supports both Anthropic Messages and OpenAI Chat Completions request/response formats; the wire format is auto-detected from the model id and can be overridden via the ``model_family`` config key. Streaming is fully wired through the Bedrock Converse-shaped event contract: ``messageStart``, ``contentBlockStart``/``contentBlockDelta``/``contentBlockStop`` for both text and tool-use blocks, ``messageStop`` with the mapped stop reason, and a ``metadata`` event carrying token usage. Non-streaming responses go through the same translation. Tool use, structured output, image inputs (Anthropic family), tool results, ``ToolChoice``, and the standard Bedrock error paths (throttling, context-window overflow, access-denied) are covered. The provider is exposed via lazy ``__getattr__`` import to keep package import time unchanged. Tests: 24 unit tests covering init, family detection, request formatting for both families, Anthropic and OpenAI streaming paths (text and tool use), non-streaming path, error mapping, and structured output. The integration test suite is converted from ``@pytest.mark.skip`` to runnable tests; the imported-model test is gated on the ``STRANDS_BEDROCK_INVOKE_IMPORTED_MODEL_ARN`` environment variable since ARNs are account-specific.
|
Trimmed and force-pushed (commit What I cut to get there (no behavior or test-coverage loss):
Verification: The three remaining red checks are still the protected-environment ones — same situation as before, only a maintainer can release them. cc @pgrayy @Unshure @zastrowm. |
Description
This PR adds a new
BedrockModelInvokeclass that uses AWS Bedrock's nativeInvokeModelandInvokeModelWithResponseStreamAPIs instead of theConverse/ConverseStreamAPIs used by the existingBedrockModelclass. This is particularly ideal to introduce support for Bedrock Custom Model Import.Key Features:
InvokeModelWithResponseStreamUse Case:
This implementation is particularly useful for imported models (via Bedrock's Custom Model Import feature) that may only support native InvokeModel APIs and expect specific request formats like OpenAI ChatCompletion.
Related Issues
N/A
Documentation PR
N/A
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.