diff --git a/src/a2a/server/request_handlers/rest_handler.py b/src/a2a/server/request_handlers/rest_handler.py index 3f7ce6b5c..6738f208b 100644 --- a/src/a2a/server/request_handlers/rest_handler.py +++ b/src/a2a/server/request_handlers/rest_handler.py @@ -31,7 +31,7 @@ SubscribeToTaskRequest, ) from a2a.utils import proto_utils -from a2a.utils.errors import ServerError, TaskNotFoundError +from a2a.utils.errors import InvalidParamsError, ServerError, TaskNotFoundError from a2a.utils.helpers import validate from a2a.utils.telemetry import SpanKind, trace_class @@ -249,7 +249,16 @@ async def on_get_task( """ task_id = request.path_params['id'] history_length_str = request.query_params.get('historyLength') - history_length = int(history_length_str) if history_length_str else None + try: + history_length = ( + int(history_length_str) if history_length_str else None + ) + except ValueError: + raise ServerError( + error=InvalidParamsError( + message='historyLength must be a valid integer' + ) + ) from None params = GetTaskRequest(id=task_id, history_length=history_length) task = await self.request_handler.on_get_task(params, context) if task: diff --git a/tests/server/apps/rest/test_rest_fastapi_app.py b/tests/server/apps/rest/test_rest_fastapi_app.py index b6b0ad525..12b060e57 100644 --- a/tests/server/apps/rest/test_rest_fastapi_app.py +++ b/tests/server/apps/rest/test_rest_fastapi_app.py @@ -396,5 +396,17 @@ async def test_send_message_rejected_task( assert expected_response == actual_response +@pytest.mark.anyio +async def test_get_task_invalid_history_length_returns_422( + client: AsyncClient, +) -> None: + """Non-numeric historyLength query param returns 422 InvalidParamsError.""" + response = await client.get('/v1/tasks/some-task-id?historyLength=abc') + assert response.status_code == 422 + data = response.json() + assert 'message' in data + assert 'historylength' in data['message'].lower() + + if __name__ == '__main__': pytest.main([__file__])