File tree Expand file tree Collapse file tree
src/a2a/server/request_handlers Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33from collections .abc import AsyncIterable , AsyncIterator
44from typing import TYPE_CHECKING , Any
55
6+ from pydantic import ValidationError
67from google .protobuf .json_format import MessageToDict , MessageToJson , Parse
78
89
2122from a2a .types import (
2223 AgentCard ,
2324 GetTaskPushNotificationConfigParams ,
25+ InvalidParamsError ,
2426 TaskIdParams ,
2527 TaskNotFoundError ,
2628 TaskQueryParams ,
@@ -257,8 +259,15 @@ async def on_get_task(
257259 """
258260 task_id = request .path_params ['id' ]
259261 history_length_str = request .query_params .get ('historyLength' )
260- history_length = int (history_length_str ) if history_length_str else None
261- params = TaskQueryParams (id = task_id , history_length = history_length )
262+ try :
263+ params = TaskQueryParams (
264+ id = task_id ,
265+ history_length = history_length_str if history_length_str else None ,
266+ )
267+ except ValidationError :
268+ raise ServerError (
269+ error = InvalidParamsError (message = 'historyLength must be a valid integer' )
270+ )
262271 task = await self .request_handler .on_get_task (params , context )
263272 if task :
264273 return MessageToDict (proto_utils .ToProto .task (task ))
Original file line number Diff line number Diff line change @@ -399,5 +399,17 @@ async def test_send_message_rejected_task(
399399 assert expected_response == actual_response
400400
401401
402+ @pytest .mark .anyio
403+ async def test_get_task_invalid_history_length_returns_422 (
404+ client : AsyncClient ,
405+ ) -> None :
406+ """Non-numeric historyLength query param returns 422 InvalidParamsError."""
407+ response = await client .get ('/v1/tasks/some-task-id?historyLength=abc' )
408+ assert response .status_code == 422
409+ data = response .json ()
410+ assert 'message' in data
411+ assert 'historylength' in data ['message' ].lower ()
412+
413+
402414if __name__ == '__main__' :
403415 pytest .main ([__file__ ])
You can’t perform that action at this time.
0 commit comments