Skip to content

Commit 46e3b1c

Browse files
author
Lou Kratz
authored
fix: Run Bedrock calls in executor for async (#3884)
1 parent bae8d19 commit 46e3b1c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/phoenix-evals/src/phoenix/evals/models/bedrock.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
import functools
13
import json
24
import logging
35
from dataclasses import dataclass, field
@@ -21,8 +23,8 @@ class BedrockModel(BaseModel):
2123
AWS API are dynamically throttled when encountering rate limit errors. Requires the `boto3`
2224
package to be installed.
2325
24-
Supports Async:
25-
`boto3` does not support async calls
26+
Supports Async: 🟡
27+
`boto3` does not support async calls, so it's wrapped in an executor.
2628
2729
Args:
2830
model_id (str): The model name to use.
@@ -109,7 +111,17 @@ def _generate(self, prompt: str, **kwargs: Dict[str, Any]) -> str:
109111
return self._parse_output(response) or ""
110112

111113
async def _async_generate(self, prompt: str, **kwargs: Dict[str, Any]) -> str:
112-
return self._generate(prompt, **kwargs)
114+
loop = asyncio.get_event_loop()
115+
return await loop.run_in_executor(
116+
None,
117+
functools.partial(
118+
self._generate,
119+
**{
120+
"prompt": prompt,
121+
**kwargs,
122+
},
123+
),
124+
)
113125

114126
def _rate_limited_completion(self, **kwargs: Any) -> Any:
115127
"""Use tenacity to retry the completion call."""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
import asyncio
2+
13
import boto3
4+
import pytest
25
from phoenix.evals import BedrockModel
36

47

58
def test_bedrock_model_can_be_instantiated():
69
session = boto3.Session(region_name="us-west-2")
710
model = BedrockModel(session=session)
811
assert model
12+
13+
14+
def test_bedrock_async_propagates_errors():
15+
with pytest.raises(AttributeError, match="'NoneType' object has no attribute 'invoke_model'"):
16+
session = boto3.Session(region_name="us-west-2")
17+
client = session.client("bedrock-runtime")
18+
model = BedrockModel(session=session, client=client)
19+
model.client = None
20+
asyncio.run(model._async_generate("prompt"))

0 commit comments

Comments
 (0)