Skip to content

Commit cfff700

Browse files
Merge pull request #377 from cnoe-io/fix-bedrock
fix: correctly handle aws bedrock streaming format
2 parents 3b09490 + c690568 commit cfff700

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

ai_platform_engineering/multi_agents/platform_engineer/protocol_bindings/a2a/agent.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,27 @@ async def stream(self, query, context_id, trace_id=None) -> AsyncIterable[dict[s
105105
"content": "",
106106
}
107107
elif isinstance(message, AIMessageChunk):
108+
# Normalize content to string (AWS Bedrock returns list, OpenAI returns string)
109+
content = message.content
110+
if isinstance(content, list):
111+
# If content is a list (AWS Bedrock), extract text from content blocks
112+
text_parts = []
113+
for item in content:
114+
if isinstance(item, dict):
115+
# Extract text from Bedrock content block: {"type": "text", "text": "..."}
116+
text_parts.append(item.get('text', ''))
117+
elif isinstance(item, str):
118+
text_parts.append(item)
119+
else:
120+
text_parts.append(str(item))
121+
content = ''.join(text_parts)
122+
elif not isinstance(content, str):
123+
content = str(content) if content else ''
124+
108125
yield {
109126
"is_task_complete": False,
110127
"require_user_input": False,
111-
"content": message.content,
128+
"content": content,
112129
}
113130

114131
except Exception as e:

ai_platform_engineering/multi_agents/platform_engineer/protocol_bindings/a2a/agent_executor.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ async def execute(
135135
logger.debug("Executor: Received A2A Task event; enqueuing.")
136136
await self._safe_enqueue_event(event_queue, event)
137137
continue
138+
# Normalize content to string (handle cases where AWS Bedrock returns list)
139+
# This is due to AWS Bedrock having a different format for the content for streaming compared to Azure OpenAI.
140+
content = event.get('content', '')
141+
if isinstance(content, list):
142+
# If content is a list (AWS Bedrock), extract text from content blocks
143+
text_parts = []
144+
for item in content:
145+
if isinstance(item, dict):
146+
# Extract text from Bedrock content block: {"type": "text", "text": "..."}
147+
text_parts.append(item.get('text', ''))
148+
elif isinstance(item, str):
149+
text_parts.append(item)
150+
else:
151+
text_parts.append(str(item))
152+
content = ''.join(text_parts)
153+
elif not isinstance(content, str):
154+
content = str(content) if content else ''
155+
138156
if event['is_task_complete']:
139157
logger.info("Task complete event received. Enqueuing TaskArtifactUpdateEvent and TaskStatusUpdateEvent.")
140158
await self._safe_enqueue_event(
@@ -147,7 +165,7 @@ async def execute(
147165
artifact=new_text_artifact(
148166
name='current_result',
149167
description='Result of request to agent.',
150-
text=event['content'],
168+
text=content,
151169
),
152170
)
153171
)
@@ -169,7 +187,7 @@ async def execute(
169187
status=TaskStatus(
170188
state=TaskState.input_required,
171189
message=new_agent_text_message(
172-
event['content'],
190+
content,
173191
task.context_id,
174192
task.id,
175193
),
@@ -188,7 +206,7 @@ async def execute(
188206
status=TaskStatus(
189207
state=TaskState.working,
190208
message=new_agent_text_message(
191-
event['content'],
209+
content,
192210
task.context_id,
193211
task.id,
194212
),

0 commit comments

Comments
 (0)