BedrockV2Client
autogen.llm_clients.bedrock_v2.BedrockV2Client #
BedrockV2Client(aws_region=None, aws_access_key=None, aws_secret_key=None, aws_session_token=None, aws_profile_name=None, timeout=None, total_max_attempts=5, max_attempts=5, mode='standard', response_format=None, **kwargs)
Bases: ModelClient
AWS Bedrock Converse API client implementing ModelClientV2 protocol.
This client works with AWS Bedrock's Converse API (bedrock_runtime.converse) which returns structured output with tool calls, multimodal content, and more.
Key Features: - Preserves text and image content as typed content blocks - Handles tool calls and structured outputs - Supports system prompts (model-dependent) - Provides backward compatibility via create_v1_compatible() - Supports additional model request fields for model-specific features
Example
client = BedrockV2Client( aws_region="us-east-1", aws_access_key="...", aws_secret_key="..." )
Get rich response#
response = client.create({ "model": "anthropic.claude-sonnet-4-5-20250929-v1:0", "messages": [{"role": "user", "content": "Hello"}] })
Access text content#
print(f"Response: {response.text}")
Access tool calls#
for tool_call in response.get_content_by_type("tool_call"): print(f"Tool: {tool_call.name}")
Initialize AWS Bedrock Converse API client.
| PARAMETER | DESCRIPTION |
|---|---|
aws_region | AWS region (required, or set AWS_REGION env var) TYPE: |
aws_access_key | AWS access key (or set AWS_ACCESS_KEY env var) TYPE: |
aws_secret_key | AWS secret key (or set AWS_SECRET_KEY env var) TYPE: |
aws_session_token | AWS session token (or set AWS_SESSION_TOKEN env var) TYPE: |
aws_profile_name | AWS profile name for credentials TYPE: |
timeout | Request timeout in seconds (default: 60) TYPE: |
total_max_attempts | Total max retry attempts (default: 5) TYPE: |
max_attempts | Max attempts per retry (default: 5) TYPE: |
mode | Retry mode - "standard", "adaptive", or "legacy" (default: "standard") TYPE: |
response_format | Optional response format (Pydantic model or JSON schema) for structured outputs TYPE: |
**kwargs | Additional arguments passed to boto3 client TYPE: |
Source code in autogen/llm_clients/bedrock_v2.py
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 | |
RESPONSE_USAGE_KEYS class-attribute instance-attribute #
bedrock_runtime instance-attribute #
ModelClientResponseProtocol #
parse_custom_params #
Parses custom parameters for logic in this client class.
Source code in autogen/llm_clients/bedrock_v2.py
parse_params #
Loads the valid parameters required to invoke Bedrock Converse.
Source code in autogen/llm_clients/bedrock_v2.py
create #
Create a completion and return UnifiedResponse with all features preserved.
This method implements ModelClient.create() but returns UnifiedResponse instead of ModelClientResponseProtocol. The rich UnifiedResponse structure is compatible via duck typing - it has .model attribute and works with message_retrieval().
| PARAMETER | DESCRIPTION |
|---|---|
params | Request parameters including: - model: Model ID (e.g., "anthropic.claude-sonnet-4-5-20250929-v1:0") - messages: List of message dicts - temperature: Optional temperature - max_tokens: Optional max completion tokens - tools: Optional tool definitions - response_format: Optional Pydantic BaseModel or JSON schema dict - supports_system_prompts: Whether model supports system prompts (default: True) - price: Optional [input_price_per_1k, output_price_per_1k] for cost calculation - additional_model_request_fields: Optional model-specific fields - **other Bedrock parameters |
| RETURNS | DESCRIPTION |
|---|---|
UnifiedResponse | UnifiedResponse with text, images, tool calls, and all content preserved |
Source code in autogen/llm_clients/bedrock_v2.py
create_v1_compatible #
Create completion in backward-compatible ChatCompletionExtended format.
This method provides compatibility with existing AG2 code that expects ChatCompletionExtended format.
| PARAMETER | DESCRIPTION |
|---|---|
params | Same parameters as create() |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | ChatCompletionExtended-compatible dict (flattened response) |
Warning
This method loses information (images, rich content) when converting to the legacy format. Prefer create() for new code.
Source code in autogen/llm_clients/bedrock_v2.py
cost #
Calculate cost from response usage.
Implements ModelClient.cost() but accepts UnifiedResponse via duck typing.
| PARAMETER | DESCRIPTION |
|---|---|
response | UnifiedResponse with usage information TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
float | Cost in USD for the API call |
Source code in autogen/llm_clients/bedrock_v2.py
get_usage staticmethod #
Extract usage statistics from response.
Implements ModelClient.get_usage() but accepts UnifiedResponse via duck typing.
| PARAMETER | DESCRIPTION |
|---|---|
response | UnifiedResponse from create() TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Dict with keys from RESPONSE_USAGE_KEYS |
Source code in autogen/llm_clients/bedrock_v2.py
message_retrieval #
Retrieve messages from response in OpenAI-compatible format.
Returns list of strings for text-only messages, or list of dicts when tool calls or complex content is present.
| PARAMETER | DESCRIPTION |
|---|---|
response | UnifiedResponse from create() TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[str] | list[dict[str, Any]] | List of strings (for text-only) OR list of message dicts (for tool calls/complex content) |