a2a.utils package

Submodules

Module contents

Utility functions for the A2A Python SDK.

a2a.utils.append_artifact_to_task(task: Task, event: TaskArtifactUpdateEvent) None

Helper method for updating a Task object with new artifact data from an event.

Handles creating the artifacts list if it doesn’t exist, adding new artifacts, and appending parts to existing artifacts based on the append flag in the event.

Parameters:
  • task – The Task object to modify.

  • event – The TaskArtifactUpdateEvent containing the artifact data.

a2a.utils.are_modalities_compatible(server_output_modes: list[str] | None, client_output_modes: list[str] | None) bool

Checks if server and client output modalities (MIME types) are compatible.

Modalities are compatible if: 1. The client specifies no preferred output modes (client_output_modes is None or empty). 2. The server specifies no supported output modes (server_output_modes is None or empty). 3. There is at least one common modality between the server’s supported list and the client’s preferred list.

Parameters:
  • server_output_modes – A list of MIME types supported by the server/agent for output. Can be None or empty if the server doesn’t specify.

  • client_output_modes – A list of MIME types preferred by the client for output. Can be None or empty if the client accepts any.

Returns:

True if the modalities are compatible, False otherwise.

a2a.utils.build_text_artifact(text: str, artifact_id: str) Artifact

Helper to create a text artifact.

Parameters:
  • text – The text content for the artifact.

  • artifact_id – The ID for the artifact.

Returns:

An Artifact object containing a single TextPart.

a2a.utils.completed_task(task_id: str, context_id: str, artifacts: list[Artifact], history: list[Message] | None = None) Task

Creates a Task object in the ‘completed’ state.

Useful for constructing a final Task representation when the agent finishes and produces artifacts.

Parameters:
  • task_id – The ID of the task.

  • context_id – The context ID of the task.

  • artifacts – A list of Artifact objects produced by the task.

  • history – An optional list of Message objects representing the task history.

Returns:

A Task object with status set to ‘completed’.

a2a.utils.create_task_obj(message_send_params: MessageSendParams) Task

Create a new task object from message send params.

Generates UUIDs for task and context IDs if they are not already present in the message.

Parameters:

message_send_params – The MessageSendParams object containing the initial message.

Returns:

A new Task object initialized with ‘submitted’ status and the input message in history.

a2a.utils.get_artifact_text(artifact: Artifact, delimiter: str = '\n') str

Extracts and joins all text content from an Artifact’s parts.

Parameters:
  • artifact – The Artifact object.

  • delimiter – The string to use when joining text from multiple TextParts.

Returns:

A single string containing all text content, or an empty string if no text parts are found.

a2a.utils.get_data_parts(parts: list[Part]) list[dict[str, Any]]

Extracts dictionary data from all DataPart objects in a list of Parts.

Parameters:

parts – A list of Part objects.

Returns:

A list of dictionaries containing the data from any DataPart objects found.

a2a.utils.get_file_parts(parts: list[Part]) list[FileWithBytes | FileWithUri]

Extracts file data from all FilePart objects in a list of Parts.

Parameters:

parts – A list of Part objects.

Returns:

A list of FileWithBytes or FileWithUri objects containing the file data from any FilePart objects found.

a2a.utils.get_message_text(message: Message, delimiter: str = '\n') str

Extracts and joins all text content from a Message’s parts.

Parameters:
  • message – The Message object.

  • delimiter – The string to use when joining text from multiple TextParts.

Returns:

A single string containing all text content, or an empty string if no text parts are found.

a2a.utils.get_text_parts(parts: list[Part]) list[str]

Extracts text content from all TextPart objects in a list of Parts.

Parameters:

parts – A list of Part objects.

Returns:

A list of strings containing the text content from any TextPart objects found.

a2a.utils.new_agent_parts_message(parts: list[Part], context_id: str | None = None, task_id: str | None = None) Message

Creates a new agent message containing a list of Parts.

Parameters:
  • parts – The list of Part objects for the message content.

  • context_id – The context ID for the message.

  • task_id – The task ID for the message.

Returns:

A new Message object with role ‘agent’.

a2a.utils.new_agent_text_message(text: str, context_id: str | None = None, task_id: str | None = None) Message

Creates a new agent message containing a single TextPart.

Parameters:
  • text – The text content of the message.

  • context_id – The context ID for the message.

  • task_id – The task ID for the message.

Returns:

A new Message object with role ‘agent’.

a2a.utils.new_artifact(parts: list[Part], name: str, description: str | None = None) Artifact

Creates a new Artifact object.

Parameters:
  • parts – The list of Part objects forming the artifact’s content.

  • name – The human-readable name of the artifact.

  • description – An optional description of the artifact.

Returns:

A new Artifact object with a generated artifact_id.

a2a.utils.new_data_artifact(name: str, data: dict[str, Any], description: str | None = None) Artifact

Creates a new Artifact object containing only a single DataPart.

Parameters:
  • name – The human-readable name of the artifact.

  • data – The structured data content of the artifact.

  • description – An optional description of the artifact.

Returns:

A new Artifact object with a generated artifact_id.

a2a.utils.new_task(request: Message) Task

Creates a new Task object from an initial user message.

Generates task and context IDs if not provided in the message.

Parameters:

request – The initial Message object from the user.

Returns:

A new Task object initialized with ‘submitted’ status and the input message in history.

Raises:
  • TypeError – If the message role is None.

  • ValueError – If the message parts are empty, if any part has empty content, or if the provided context_id is invalid.

a2a.utils.new_text_artifact(name: str, text: str, description: str | None = None) Artifact

Creates a new Artifact object containing only a single TextPart.

Parameters:
  • name – The human-readable name of the artifact.

  • text – The text content of the artifact.

  • description – An optional description of the artifact.

Returns:

A new Artifact object with a generated artifact_id.