Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions python/packages/kagent-adk/src/kagent/adk/models/_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,32 @@ async def generate_content_async(
if self.additional_model_request_fields:
kwargs["additionalModelRequestFields"] = self.additional_model_request_fields

def _run_converse_stream(**kw):
resp = client.converse_stream(**kw)
return list(resp.get("stream", []))

try:
if stream:
stream_body = await asyncio.to_thread(_run_converse_stream, **kwargs)
q: asyncio.Queue = asyncio.Queue()
Comment thread
mesutoezdil marked this conversation as resolved.
loop = asyncio.get_running_loop()

def _produce():
try:
resp = client.converse_stream(**kwargs)
for event in resp.get("stream", []):
loop.call_soon_threadsafe(q.put_nowait, event)
Comment thread
mesutoezdil marked this conversation as resolved.
except Exception as exc:
loop.call_soon_threadsafe(q.put_nowait, exc)
Comment thread
mesutoezdil marked this conversation as resolved.
finally:
loop.call_soon_threadsafe(q.put_nowait, None)

loop.run_in_executor(None, _produce)
Comment thread
mesutoezdil marked this conversation as resolved.

aggregated_text = ""
tool_uses: dict[str, dict] = {} # toolUseId -> {name, input_json}
current_tool_id: Optional[str] = None
stop_reason = "end_turn"
usage_metadata: Optional[types.GenerateContentResponseUsageMetadata] = None

for event in stream_body:
while (event := await q.get()) is not None:
if isinstance(event, Exception):
raise event
Comment thread
mesutoezdil marked this conversation as resolved.
Comment thread
mesutoezdil marked this conversation as resolved.
if "contentBlockStart" in event:
start = event["contentBlockStart"].get("start", {})
if "toolUse" in start:
Expand Down
Loading