diff --git a/docs/integrations/bedrock.md b/docs/integrations/bedrock.md index 6d25b5f1b..f2ff09f34 100644 --- a/docs/integrations/bedrock.md +++ b/docs/integrations/bedrock.md @@ -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 diff --git a/instructor/client_bedrock.py b/instructor/client_bedrock.py index a90664bda..eede6680b 100644 --- a/instructor/client_bedrock.py +++ b/instructor/client_bedrock.py @@ -9,6 +9,8 @@ import instructor from instructor.client import AsyncInstructor, Instructor +import asyncio + @overload # type: ignore def from_bedrock( @@ -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, @@ -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