Skip to content

Bedrock async client not working when created with fromBedrock function #1495

@ebamberg-business

Description

@ebamberg-business
  • [ X] This is actually a bug report.
  • [ -] I am not getting good LLM Results
  • [ -] I have tried asking for help in the community on discord or discussions and have not received a response.
  • [ X] I have tried searching the documentation and have not found an answer.

What Model are you using?

  • gpt-3.5-turbo
  • gpt-4-turbo
  • gpt-4
  • [X ] Other Bedrock with Claude

Describe the bug

As I understand bedrock async client should be supported.
When I trying to patch a async-bedrock-client (aioboto3 client) instructor doesn't return a AsyncInstructor. Comparing to other clients like OpenAI or Gemini the patch-function returns a AsyncInstructor object but for bedrock it doesn't.
This results in exception when trying to call instructor.
Looking at the bedrock-client it seems that this implementation is missing there. The function doesn't check for is boto3-client async.

See here:

def from_bedrock(
client: BaseClient,
mode: instructor.Mode = instructor.Mode.BEDROCK_JSON,
**kwargs: Any,
) -> Instructor | AsyncInstructor:
assert (
mode
in {
instructor.Mode.BEDROCK_TOOLS,
instructor.Mode.BEDROCK_JSON,
}
), "Mode must be one of {instructor.Mode.BEDROCK_TOOLS, instructor.Mode.BEDROCK_JSON}"
assert isinstance(
client,
BaseClient,
), "Client must be an instance of boto3.client"
create = client.converse # Example method, replace with actual method

return Instructor(
    client=client,
    create=instructor.patch(create=create, mode=mode),
    provider=instructor.Provider.BEDROCK,
    mode=mode,
    **kwargs,
)

When I try to patch the async-boto3 client in my own code the client work as expected for mode BEDROCK_JSON.
This is my code how I create a async client and works as expected:
def from_bedrock_async(
client: BaseClient,
mode: instructor.Mode = instructor.Mode.BEDROCK_JSON,
**kwargs: Any)-> instructor.AsyncInstructor:
async def async_wrapper(*args: Any, **kwargs: Any): # type:ignore
return await client.converse(*args, **kwargs) # type:ignore

return instructor.AsyncInstructor(
        client=client,
        create=instructor.patch(create=async_wrapper, mode=mode),
        provider=instructor.Provider.BEDROCK,
        mode=mode,
        **kwargs,
) 

can we add a code that return a AsyncInstructor for bedrock in the bedrock-client code.

Another problem is that BEDROCK_TOOL throws an exception with the AsyncInstructor I created with above code. Shall I raise another ticket for that issue or can we handle this is one ticket ?

To Reproduce
create a async boto3 bedrock client in python.
patch it with

patched_client=from_bedrock(my_async_client)

try any kind of structured output using this patched-client will cause an exception cause the code doesn't get "await"

Expected behavior
returns a AsyncInstructor that I can use without exceptions

Screenshots

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions