Skip to content

AsyncAnthropicBedrock blocks the event loop during AWS credential resolution or signing #1770

Description

@jjurm

Description

AsyncAnthropicBedrock performs AWS credential resolution and SigV4 signing synchronously on the event-loop thread.

Expected: AsyncAnthropicBedrock should not execute potentially blocking credential resolution or refresh on the event-loop thread.

Reproduction

import asyncio
import time
from unittest.mock import patch

from anthropic import AsyncAnthropicBedrock


async def heartbeat():
    while True:
        await asyncio.sleep(0.5)
        print("heartbeat")


def slow_get_credentials(*args, **kwargs):
    time.sleep(5)
    raise RuntimeError()


async def main():
    client = AsyncAnthropicBedrock(aws_region="us-east-1")

    with patch(
        "botocore.session.Session.get_credentials",
        side_effect=slow_get_credentials,
    ):
        await asyncio.gather(
            heartbeat(),
            client.messages.create(
                model="anthropic.claude-3-haiku-20240307-v1:0",
                max_tokens=1,
                messages=[{"role": "user", "content": "hi"}],
            ),
        )


asyncio.run(main())

No heartbeat output appears during the five-second credential call.

Cause: AsyncAnthropicBedrock._prepare_request() calls the synchronous credential/signing path directly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions