Skip to main content

MessageBuilder

What it is

MessageBuilder is a fluent builder for A2A Message objects — the envelope that carries a user prompt plus any structured data (files, session context, form submissions, execution context) to a remote agent. An A2A message is usually two parts: a TextPart with the prompt and a DataPart with metadata. The builder makes assembling that second part ergonomic so you don’t hand-write DataPart JSON every time.

When to use it

  • You’re calling another agent with a file attachment (PDF, image, document).
  • You’re continuing a conversation (with_context_id) and need to stay on the same session.
  • You’re replying to a form Svantic emitted (with_action_data).
  • You need to route directly to a specific agent type (with_target_agent) or hand the platform a learner-grade execution context.
For plain text with no metadata, call remote.send(text) or remote.send_stream(text) instead — there’s nothing to build.

Functional usage

The builder adds the DataPart only if at least one metadata-carrying method was called — plain text passes through as a one-part message.

Constructor

text is the user-facing message string. It lands in the first TextPart of the resulting message.

Fluent methods

Every builder method returns this, so calls chain.

Terminal method

build(): Message

Returns an A2A Message. The message has:
  • One TextPart carrying the constructor text.
  • One DataPart (added only if any metadata-carrying builder method was called) with { context, target_agent, yolo_mode, action_data, files, execution_context } — each key present only when set.
  • A fresh messageId (UUID v4).
  • role: 'user'.
  • contextId when with_context_id was called.

Example