Skip to content

Commit a7800a0

Browse files
committed
feat: Add Input Validation for Task Context IDs in new_task Function
1 parent 4d5b92c commit a7800a0

2 files changed

Lines changed: 59 additions & 4 deletions

File tree

src/a2a/utils/task.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def new_task(request: Message) -> Task:
1818
1919
Raises:
2020
TypeError: If the message role is None.
21-
ValueError: If the message parts are empty or if any part has empty content.
21+
ValueError: If the message parts are empty, if any part has empty content, or if the provided context_id is invalid.
2222
"""
2323
if not request.role:
2424
raise TypeError('Message role cannot be None')
@@ -28,12 +28,25 @@ def new_task(request: Message) -> Task:
2828
if isinstance(part.root, TextPart) and not part.root.text:
2929
raise ValueError('TextPart content cannot be empty')
3030

31+
context_id_str = request.context_id
32+
if context_id_str is not None:
33+
try:
34+
# Validate that the provided context_id is a valid UUID
35+
uuid.UUID(context_id_str)
36+
context_id = context_id_str
37+
except (ValueError, AttributeError, TypeError):
38+
# Catch a variety of potential issues with the UUID validation
39+
raise ValueError(
40+
f"Invalid context_id: '{context_id_str}' is not a valid UUID."
41+
)
42+
else:
43+
# Generate a new UUID if no context_id is provided
44+
context_id = str(uuid.uuid4())
45+
3146
return Task(
3247
status=TaskStatus(state=TaskState.submitted),
3348
id=(request.task_id if request.task_id else str(uuid.uuid4())),
34-
context_id=(
35-
request.context_id if request.context_id else str(uuid.uuid4())
36-
),
49+
context_id=context_id,
3750
history=[request],
3851
)
3952

tests/utils/test_task.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,48 @@ def test_completed_task_invalid_artifact_type(self):
188188
history=[],
189189
)
190190

191+
def test_new_task_with_invalid_context_id(self):
192+
"""Test that new_task raises a ValueError with an invalid context_id."""
193+
with pytest.raises(
194+
ValueError,
195+
match="Invalid context_id: 'not-a-uuid' is not a valid UUID.",
196+
):
197+
new_task(
198+
Message(
199+
role=Role.user,
200+
parts=[Part(root=TextPart(text='test message'))],
201+
message_id=str(uuid.uuid4()),
202+
context_id='not-a-uuid',
203+
)
204+
)
205+
206+
def test_new_task_with_empty_string_context_id(self):
207+
"""Test that new_task raises a ValueError with an empty string context_id."""
208+
with pytest.raises(
209+
ValueError, match="Invalid context_id: '' is not a valid UUID."
210+
):
211+
new_task(
212+
Message(
213+
role=Role.user,
214+
parts=[Part(root=TextPart(text='test message'))],
215+
message_id=str(uuid.uuid4()),
216+
context_id='',
217+
)
218+
)
219+
220+
def test_new_task_with_valid_context_id(self):
221+
"""Test that new_task accepts a valid context_id."""
222+
valid_uuid = '123e4567-e89b-12d3-a456-426614174000'
223+
task = new_task(
224+
Message(
225+
role=Role.user,
226+
parts=[Part(root=TextPart(text='test message'))],
227+
message_id=str(uuid.uuid4()),
228+
context_id=valid_uuid,
229+
)
230+
)
231+
self.assertEqual(task.context_id, valid_uuid)
232+
191233

192234
if __name__ == '__main__':
193235
unittest.main()

0 commit comments

Comments
 (0)