mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: Let part converters also return multiple parts so they can support more usecases
PiperOrigin-RevId: 830882000
This commit is contained in:
committed by
Copybara-Service
parent
fd33610e96
commit
824ab07212
@@ -301,13 +301,15 @@ def convert_a2a_message_to_event(
|
||||
)
|
||||
|
||||
try:
|
||||
parts = []
|
||||
output_parts = []
|
||||
long_running_tool_ids = set()
|
||||
|
||||
for a2a_part in a2a_message.parts:
|
||||
try:
|
||||
part = part_converter(a2a_part)
|
||||
if part is None:
|
||||
parts = part_converter(a2a_part)
|
||||
if not isinstance(parts, list):
|
||||
parts = [parts] if parts else []
|
||||
if not parts:
|
||||
logger.warning("Failed to convert A2A part, skipping: %s", a2a_part)
|
||||
continue
|
||||
|
||||
@@ -321,16 +323,18 @@ def convert_a2a_message_to_event(
|
||||
)
|
||||
is True
|
||||
):
|
||||
long_running_tool_ids.add(part.function_call.id)
|
||||
for part in parts:
|
||||
if part.function_call:
|
||||
long_running_tool_ids.add(part.function_call.id)
|
||||
|
||||
parts.append(part)
|
||||
output_parts.extend(parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to convert A2A part: %s, error: %s", a2a_part, e)
|
||||
# Continue processing other parts instead of failing completely
|
||||
continue
|
||||
|
||||
if not parts:
|
||||
if not output_parts:
|
||||
logger.warning(
|
||||
"No parts could be converted from A2A message %s", a2a_message
|
||||
)
|
||||
@@ -348,7 +352,7 @@ def convert_a2a_message_to_event(
|
||||
else None,
|
||||
content=genai_types.Content(
|
||||
role="model",
|
||||
parts=parts,
|
||||
parts=output_parts,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -387,15 +391,19 @@ def convert_event_to_a2a_message(
|
||||
return None
|
||||
|
||||
try:
|
||||
a2a_parts = []
|
||||
output_parts = []
|
||||
for part in event.content.parts:
|
||||
a2a_part = part_converter(part)
|
||||
if a2a_part:
|
||||
a2a_parts.append(a2a_part)
|
||||
a2a_parts = part_converter(part)
|
||||
if not isinstance(a2a_parts, list):
|
||||
a2a_parts = [a2a_parts] if a2a_parts else []
|
||||
for a2a_part in a2a_parts:
|
||||
output_parts.append(a2a_part)
|
||||
_process_long_running_tool(a2a_part, event)
|
||||
|
||||
if a2a_parts:
|
||||
return Message(message_id=str(uuid.uuid4()), role=role, parts=a2a_parts)
|
||||
if output_parts:
|
||||
return Message(
|
||||
message_id=str(uuid.uuid4()), role=role, parts=output_parts
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to convert event to status message: %s", e)
|
||||
|
||||
@@ -22,7 +22,9 @@ import base64
|
||||
from collections.abc import Callable
|
||||
import json
|
||||
import logging
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from .utils import _get_adk_metadata_key
|
||||
|
||||
@@ -53,10 +55,11 @@ A2A_DATA_PART_METADATA_TYPE_EXECUTABLE_CODE = 'executable_code'
|
||||
|
||||
|
||||
A2APartToGenAIPartConverter = Callable[
|
||||
[a2a_types.Part], Optional[genai_types.Part]
|
||||
[a2a_types.Part], Union[Optional[genai_types.Part], List[genai_types.Part]]
|
||||
]
|
||||
GenAIPartToA2APartConverter = Callable[
|
||||
[genai_types.Part], Optional[a2a_types.Part]
|
||||
[genai_types.Part],
|
||||
Union[Optional[a2a_types.Part], List[a2a_types.Part]],
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -110,12 +110,19 @@ def convert_a2a_request_to_agent_run_request(
|
||||
if request.metadata:
|
||||
custom_metadata['a2a_metadata'] = request.metadata
|
||||
|
||||
output_parts = []
|
||||
for a2a_part in request.message.parts:
|
||||
genai_parts = part_converter(a2a_part)
|
||||
if not isinstance(genai_parts, list):
|
||||
genai_parts = [genai_parts] if genai_parts else []
|
||||
output_parts.extend(genai_parts)
|
||||
|
||||
return AgentRunRequest(
|
||||
user_id=_get_user_id(request),
|
||||
session_id=request.context_id,
|
||||
new_message=genai_types.Content(
|
||||
role='user',
|
||||
parts=[part_converter(part) for part in request.message.parts],
|
||||
parts=output_parts,
|
||||
),
|
||||
run_config=RunConfig(custom_metadata=custom_metadata),
|
||||
)
|
||||
|
||||
@@ -376,9 +376,12 @@ class RemoteA2aAgent(BaseAgent):
|
||||
continue
|
||||
|
||||
for part in event.content.parts:
|
||||
converted_part = self._genai_part_converter(part)
|
||||
if converted_part:
|
||||
message_parts.append(converted_part)
|
||||
converted_parts = self._genai_part_converter(part)
|
||||
if not isinstance(converted_parts, list):
|
||||
converted_parts = [converted_parts] if converted_parts else []
|
||||
|
||||
if converted_parts:
|
||||
message_parts.extend(converted_parts)
|
||||
else:
|
||||
logger.warning("Failed to convert part to A2A format: %s", part)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user