Skip to content

Commit 590167e

Browse files
committed
Moving boto3 import to within _complete_chat_async
1 parent 78af630 commit 590167e

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

pyrit/prompt_target/aws_bedrock_claude_chat_target.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import base64
66
import json
77
import logging
8-
from typing import TYPE_CHECKING, MutableSequence, Optional
8+
from typing import MutableSequence, Optional
99

1010
from pyrit.chat_message_normalizer import ChatMessageNop, ChatMessageNormalizer
1111
from pyrit.models import (
@@ -17,10 +17,6 @@
1717

1818
logger = logging.getLogger(__name__)
1919

20-
if TYPE_CHECKING:
21-
import boto3
22-
from botocore.exceptions import ClientError
23-
2420

2521
class AWSBedrockClaudeChatTarget(PromptChatTarget):
2622
"""
@@ -64,13 +60,6 @@ def __init__(
6460

6561
self._valid_image_types = ["jpeg", "png", "webp", "gif"]
6662

67-
try:
68-
import boto3 # noqa: F401
69-
from botocore.exceptions import ClientError # noqa: F401
70-
except ModuleNotFoundError as e:
71-
logger.error("Could not import boto. You may need to install it via 'pip install pyrit[all] or pyrit[aws]'")
72-
raise e
73-
7463
@limit_requests_per_minute
7564
async def send_prompt_async(self, *, prompt_request: PromptRequestResponse) -> PromptRequestResponse:
7665

@@ -101,11 +90,16 @@ def _validate_request(self, *, prompt_request: PromptRequestResponse) -> None:
10190

10291
async def _complete_chat_async(self, messages: list[ChatMessageListDictContent]) -> str:
10392
try:
104-
brt = boto3.client(
105-
service_name="bedrock-runtime", region_name="us-east-1", verify=self._enable_ssl_verification
106-
)
107-
except Exception as e:
108-
raise RuntimeError(f"An error occurred when initializing boto3 client: {str(e)}") from e
93+
import boto3 # noqa: F401
94+
from botocore.exceptions import ClientError # noqa: F401
95+
except ModuleNotFoundError as e:
96+
logger.error("Could not import boto. You may need to install it via 'pip install pyrit[all] or pyrit[aws]'")
97+
raise e
98+
99+
brt = boto3.client(
100+
service_name="bedrock-runtime", region_name="us-east-1", verify=self._enable_ssl_verification
101+
)
102+
109103
native_request = self._construct_request_body(messages)
110104

111105
request = json.dumps(native_request)

0 commit comments

Comments
 (0)