.NET: Python: Native support for Bedrock-hosted models (Anthropic, Cohere, etc.) in Python SDK#2560
.NET: Python: Native support for Bedrock-hosted models (Anthropic, Cohere, etc.) in Python SDK#2560duttastunil wants to merge 3 commits intomicrosoft:mainfrom
Conversation
eavanvalkenburg
left a comment
There was a problem hiding this comment.
some comments on the code.
The folder itself should be python/packages/bedrock with agent_framework_bedrock inside, next to the pyproject and other files.
And we are missing creating a folder inside the core package called amazon that lazy loads from agent_framework_bedrock (look at the Anthropic folder for how)
|
|
||
| [project.optional-dependencies] | ||
| dev = [ | ||
| "pytest>=8.3.0", |
There was a problem hiding this comment.
we already install pytest as a dev dependency
There was a problem hiding this comment.
this should just be samples/amazon/<sample name>.py
| @@ -0,0 +1,63 @@ | |||
| from __future__ import annotations | |||
| ToolMode, | ||
| ai_function, | ||
| ) | ||
| from agent_framework_bedrock import BedrockChatClient |
There was a problem hiding this comment.
| from agent_framework_bedrock import BedrockChatClient | |
| from agent_framework.amazon import BedrockChatClient |
| @@ -0,0 +1,504 @@ | |||
| # Copyright (c) Microsoft. All rights reserved. | |||
|
|
|||
| from __future__ import annotations | |||
| access_key: str | None = None, | ||
| secret_key: str | None = None, | ||
| session_token: str | None = None, | ||
| bedrock_runtime_client: BaseClient | None = None, |
There was a problem hiding this comment.
for consistency use:
| bedrock_runtime_client: BaseClient | None = None, | |
| client: BaseClient | None = None, |
| **kwargs: Any, | ||
| ) -> ChatResponse: | ||
| request = self._build_converse_request(messages, chat_options, **kwargs) | ||
| raw_response = await asyncio.to_thread(self._bedrock_client.converse, **request) |
There was a problem hiding this comment.
is there no native async method?
There was a problem hiding this comment.
AWS’s Bedrock runtime SDK is exposed only through boto3/botocore, which are synchronous clients. There isn’t an official async interface (no converse_async or async-aware session) today.
| chat_options: ChatOptions, | ||
| **kwargs: Any, | ||
| ) -> AsyncIterable[ChatResponseUpdate]: | ||
| response = await self._inner_get_response(messages=messages, chat_options=chat_options, **kwargs) |
There was a problem hiding this comment.
and no streaming method?
There was a problem hiding this comment.
I think AWS has a ConverseStream API in preview for select providers, but it isn’t in the generally available boto3 client yet, and it doesn’t cover every model. Until the official SDK exposes a stable streaming method, we can’t add a native streaming implementation. The current shim keeps our interface consistent and can be upgraded once AWS ships wider support.
| payload["system"] = system_prompts | ||
|
|
||
| inference_config: dict[str, Any] = {} | ||
| if chat_options.max_tokens is not None: |
There was a problem hiding this comment.
| if chat_options.max_tokens is not None: | |
| inference_config["maxTokens"] = chat_options.max_tokens if chat_options.max_tokens is not None else DEFAULT_MAX_TOKENS |
| "Bedrock model_id is required. Set via chat options or BEDROCK_CHAT_MODEL_ID environment variable." | ||
| ) | ||
|
|
||
| system_prompts = self._extract_system_prompts(messages) |
There was a problem hiding this comment.
in these two functions, we are looping through the messages twice, maybe combine and return the first part of the payload
|
@eavanvalkenburg - I addressed all the comments on this PR, Please have a look on the latest code/PR. Thanks!! |
|
Updated python/packages/core/agent_framework/_workflows/_validation.py due to the below issue. Poe => ruff check Found 1 error. |
|
@eavanvalkenburg - Since I ran precommit check command and other commands on my local, it seems multiple file changed, https://github.com/microsoft/agent-framework/pull/2610/files |
Motivation and Context
The current Python SDK for the Microsoft Agent Framework requires users to route requests to Bedrock models via the generic OpenAI-compatible API interface. This approach has several drawbacks:
It does not natively recognize specific model IDs (e.g., anthropic.claude-3-5-sonnet-20240620-v1:0).
It may lack native support for provider-specific parameters or functionality (like Anthropic's specific messaging API structure, context windows, or token cost tracking).
It creates a less intuitive configuration experience compared to native integrations for Azure OpenAI or generic OpenAI services.
I would like to request dedicated, native client classes and configuration support within the Python SDK to directly integrate models hosted on Amazon Bedrock.
Description
Below are the details of the BedrockChatClient:
Contribution Checklist