Class TaskEvent

java.lang.Object
org.a2aproject.sdk.client.TaskEvent
All Implemented Interfaces:
ClientEvent

public final class TaskEvent extends Object implements ClientEvent
A client event containing the complete state of a task.

TaskEvent represents a snapshot of a task's full state at a point in time. This event type is typically received in two scenarios:

  1. Final task state: When a task reaches a terminal state (COMPLETED, FAILED, CANCELED), the agent may send a TaskEvent with the complete final state
  2. Non-streaming mode: When streaming is disabled, the client receives a single TaskEvent containing the final result after the agent completes processing

Contrast with TaskUpdateEvent: While TaskUpdateEvent provides incremental updates during task execution (status changes, new artifacts), TaskEvent provides the complete task state in a single event.

Example usage:


 client.addConsumer((event, agentCard) -> {
     if (event instanceof TaskEvent te) {
         Task task = te.getTask();
         
         // Check task state
         TaskState state = task.status().state();
         switch (state) {
             case COMPLETED -> {
                 // Task finished successfully
                 if (task.artifact() != null) {
                     System.out.println("Result: " + task.artifact().parts());
                 }
             }
             case FAILED -> {
                 // Task failed
                 String error = task.status().message();
                 System.err.println("Task failed: " + error);
             }
             case CANCELED -> {
                 System.out.println("Task was canceled");
             }
             default -> {
                 System.out.println("Task in state: " + state);
             }
         }
     }
 });
 

Task contents: The contained Task includes:

  • id: Unique task identifier
  • status: Current state (SUBMITTED, WORKING, COMPLETED, FAILED, CANCELED, etc.)
  • artifact: Task results (if available)
  • contextId: Associated session/context identifier
  • metadata: Custom task metadata
  • history: Optional state transition history

Terminal states: When a task reaches a final state, no further updates will be received for that task:

  • COMPLETED - task finished successfully
  • FAILED - task encountered an error
  • CANCELED - task was canceled by user or system
  • REJECTED - task was rejected (e.g., authorization failure)
See Also:
  • Constructor Details

    • TaskEvent

      public TaskEvent(Task task)
      Create a task event.
      Parameters:
      task - the task state received from the agent (required)
  • Method Details

    • getTask

      public Task getTask()
      Get the task contained in this event.
      Returns:
      the complete task state