Skip to content

Commit 3e507f2

Browse files
fix: return 422 for invalid historyLength in REST get_task
Replace unhandled int() conversion with Pydantic validation so non-numeric historyLength query params (e.g. ?historyLength=abc) return 422 InvalidParamsError instead of 500.
1 parent 6086f96 commit 3e507f2

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/a2a/server/request_handlers/rest_handler.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
SubscribeToTaskRequest,
3232
)
3333
from a2a.utils import proto_utils
34-
from a2a.utils.errors import ServerError, TaskNotFoundError
34+
from a2a.utils.errors import InvalidParamsError, ServerError, TaskNotFoundError
3535
from a2a.utils.helpers import validate
3636
from a2a.utils.telemetry import SpanKind, trace_class
3737

@@ -249,7 +249,12 @@ async def on_get_task(
249249
"""
250250
task_id = request.path_params['id']
251251
history_length_str = request.query_params.get('historyLength')
252-
history_length = int(history_length_str) if history_length_str else None
252+
try:
253+
history_length = int(history_length_str) if history_length_str else None
254+
except ValueError:
255+
raise ServerError(
256+
error=InvalidParamsError(message='historyLength must be a valid integer')
257+
)
253258
params = GetTaskRequest(id=task_id, history_length=history_length)
254259
task = await self.request_handler.on_get_task(params, context)
255260
if task:

tests/server/apps/rest/test_rest_fastapi_app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,5 +396,17 @@ async def test_send_message_rejected_task(
396396
assert expected_response == actual_response
397397

398398

399+
@pytest.mark.anyio
400+
async def test_get_task_invalid_history_length_returns_422(
401+
client: AsyncClient,
402+
) -> None:
403+
"""Non-numeric historyLength query param returns 422 InvalidParamsError."""
404+
response = await client.get('/v1/tasks/some-task-id?historyLength=abc')
405+
assert response.status_code == 422
406+
data = response.json()
407+
assert 'message' in data
408+
assert 'historylength' in data['message'].lower()
409+
410+
399411
if __name__ == '__main__':
400412
pytest.main([__file__])

0 commit comments

Comments
 (0)