a2a.server.request_handlers.response_helpers module

Helper functions for building A2A JSON-RPC responses.

a2a.server.request_handlers.response_helpers.EventTypes = a2a.types.Task | a2a.types.Message | a2a.types.TaskArtifactUpdateEvent | a2a.types.TaskStatusUpdateEvent | a2a.types.TaskPushNotificationConfig | a2a.types.A2AError | a2a.types.JSONRPCError | list[a2a.types.TaskPushNotificationConfig]

Type alias for possible event types produced by handlers.

class a2a.server.request_handlers.response_helpers.RT

Type variable for RootModel response types.

alias of TypeVar(‘RT’, ~a2a.types.GetTaskResponse, ~a2a.types.CancelTaskResponse, ~a2a.types.SendMessageResponse, ~a2a.types.SetTaskPushNotificationConfigResponse, ~a2a.types.GetTaskPushNotificationConfigResponse, ~a2a.types.SendStreamingMessageResponse, ~a2a.types.ListTaskPushNotificationConfigResponse, ~a2a.types.DeleteTaskPushNotificationConfigResponse)

class a2a.server.request_handlers.response_helpers.SPT

Type variable for SuccessResponse types.

alias of TypeVar(‘SPT’, ~a2a.types.GetTaskSuccessResponse, ~a2a.types.CancelTaskSuccessResponse, ~a2a.types.SendMessageSuccessResponse, ~a2a.types.SetTaskPushNotificationConfigSuccessResponse, ~a2a.types.GetTaskPushNotificationConfigSuccessResponse, ~a2a.types.SendStreamingMessageSuccessResponse, ~a2a.types.ListTaskPushNotificationConfigSuccessResponse, ~a2a.types.DeleteTaskPushNotificationConfigSuccessResponse)

a2a.server.request_handlers.response_helpers.build_error_response(request_id: str | int | None, error: A2AError | JSONRPCError, response_wrapper_type: type[RT]) RT

Helper method to build a JSONRPCErrorResponse wrapped in the appropriate response type.

Parameters:
  • request_id – The ID of the request that caused the error.

  • error – The A2AError or JSONRPCError object.

  • response_wrapper_type – The Pydantic RootModel type that wraps the response for the specific RPC method (e.g., SendMessageResponse).

Returns:

A Pydantic model representing the JSON-RPC error response, wrapped in the specified response type.

a2a.server.request_handlers.response_helpers.prepare_response_object(request_id: str | int | None, response: Task | Message | TaskArtifactUpdateEvent | TaskStatusUpdateEvent | TaskPushNotificationConfig | A2AError | JSONRPCError | list[TaskPushNotificationConfig], success_response_types: tuple[type, ...], success_payload_type: type[SPT], response_type: type[RT]) RT

Helper method to build appropriate JSONRPCResponse object for RPC methods.

Based on the type of the response object received from the handler, it constructs either a success response wrapped in the appropriate payload type or an error response.

Parameters:
  • request_id – The ID of the request.

  • response – The object received from the request handler.

  • success_response_types – A tuple of expected Pydantic model types for a successful result.

  • success_payload_type – The Pydantic model type for the success payload (e.g., SendMessageSuccessResponse).

  • response_type – The Pydantic RootModel type that wraps the final response (e.g., SendMessageResponse).

Returns:

A Pydantic model representing the final JSON-RPC response (success or error).