Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/integrations/bedrock.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ print(user)
> **Warning:**
> AWS Bedrock's official SDK (`boto3`) does not support async natively. If you need to call Bedrock from async code, you can use `asyncio.to_thread` to run synchronous Bedrock calls in a non-blocking way.

AWS's boto3 Bedrock client does **not** natively support asynchronous operations. To enable async usage, Instructor wraps the synchronous client using Python's `asyncio.to_thread`, which runs blocking calls in a thread pool. This allows you to use Bedrock in async applications without blocking the event loop, but note that true network-level async is not possible until boto3 adds support.

```python
import instructor
from pydantic import BaseModel
Expand Down
18 changes: 16 additions & 2 deletions instructor/client_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import instructor
from instructor.client import AsyncInstructor, Instructor

import asyncio


@overload # type: ignore
def from_bedrock(
Expand Down Expand Up @@ -47,7 +49,19 @@ def from_bedrock(
**kwargs: Any,
) -> Instructor | AsyncInstructor:
"""
Accepts both 'async_client' (preferred) and '_async' (deprecated) for async mode.
Create an Instructor or AsyncInstructor client for AWS Bedrock.

Args:
client (BaseClient): A boto3 Bedrock client instance.
mode (instructor.Mode): The mode to use (BEDROCK_TOOLS or BEDROCK_JSON).
_async (bool): If True, returns an AsyncInstructor that runs client.converse in a thread using asyncio.to_thread.
**kwargs: Additional keyword arguments passed to the Instructor/AsyncInstructor.

Returns:
Instructor or AsyncInstructor: The appropriate client for synchronous or asynchronous usage.

Note:
The async client is a wrapper around the synchronous boto3 client, using asyncio.to_thread to enable non-blocking calls in async applications.
"""
valid_modes = {
instructor.Mode.BEDROCK_TOOLS,
Expand Down Expand Up @@ -83,7 +97,7 @@ def from_bedrock(
use_async = async_client or (_async is not None and _async is True)

async def async_wrapper(**kwargs: Any):
return client.converse(**kwargs)
return await asyncio.to_thread(client.converse, **kwargs)

create = client.converse

Expand Down