Skip to content

Commit 03e6b3b

Browse files
committed
feat: Add get_data_parts() and get_file_parts() helper methods
1 parent ecbbb7e commit 03e6b3b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/a2a/utils/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
create_task_obj,
1818
)
1919
from a2a.utils.message import (
20+
get_data_parts,
21+
get_file_parts,
2022
get_message_text,
2123
get_text_parts,
2224
new_agent_parts_message,
@@ -37,6 +39,8 @@
3739
'build_text_artifact',
3840
'completed_task',
3941
'create_task_obj',
42+
'get_data_parts',
43+
'get_file_parts',
4044
'get_message_text',
4145
'get_text_parts',
4246
'new_agent_parts_message',

src/a2a/utils/message.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import uuid
44

5+
from typing import Any
6+
57
from a2a.types import (
8+
DataPart,
9+
FilePart,
10+
FileWithBytes,
11+
FileWithUri,
612
Message,
713
Part,
814
Role,
@@ -70,6 +76,30 @@ def get_text_parts(parts: list[Part]) -> list[str]:
7076
return [part.root.text for part in parts if isinstance(part.root, TextPart)]
7177

7278

79+
def get_data_parts(parts: list[Part]) -> list[dict[str, Any]]:
80+
"""Extracts dictionary data from all DataPart objects in a list of Parts.
81+
82+
Args:
83+
parts: A list of `Part` objects.
84+
85+
Returns:
86+
A list of dictionaries containing the data from any `DataPart` objects found.
87+
"""
88+
return [part.root.data for part in parts if isinstance(part.root, DataPart)]
89+
90+
91+
def get_file_parts(parts: list[Part]) -> list[FileWithBytes | FileWithUri]:
92+
"""Extracts file data from all FilePart objects in a list of Parts.
93+
94+
Args:
95+
parts: A list of `Part` objects.
96+
97+
Returns:
98+
A list of `FileWithBytes` or `FileWithUri` objects containing the file data from any `FilePart` objects found.
99+
"""
100+
return [part.root.file for part in parts if isinstance(part.root, FilePart)]
101+
102+
73103
def get_message_text(message: Message, delimiter: str = '\n') -> str:
74104
"""Extracts and joins all text content from a Message's parts.
75105

0 commit comments

Comments
 (0)