Skip to main content

Overview

Messages are the fundamental unit of communication in T3Router. Each message has a role (User or Assistant), content, and metadata that determines how it’s processed by the API.

Message Structure

The Message struct contains the following fields:

Fields

  • id: Unique identifier (UUID) for the message
  • role: Either Type::User or Type::Assistant
  • content: The text content or image URL
  • content_type: Either ContentType::Text or ContentType::Image
  • image_url: Optional URL for generated images
  • base64_data: Optional base64-encoded image data

Type Enum

The Type enum represents the role of a message:
  • User: Messages sent by the user to the API
  • Assistant: Responses from the AI model

ContentType Enum

The ContentType enum indicates what kind of content the message contains:
  • Text: Standard text content
  • Image: Generated image (URL and/or base64 data)

Creating Messages

Text Messages

Create a standard text message:
Method Signature:
This creates a text message with:
  • A randomly generated UUID as the ID
  • ContentType::Text
  • No image data

Image Messages

Create a message containing an image:
Method Signature:
This creates an image message with:
  • A randomly generated UUID as the ID
  • ContentType::Image
  • The URL stored in both content and image_url
  • Optional base64 data

Messages with Custom IDs

Create a message with a specific ID (useful for message reconstruction):
Method Signature:

Working with Messages

Sending Messages

Messages can be sent immediately:

Building Conversation History

Messages can be appended to build context before sending:

Handling Different Content Types

Always check the content type when processing responses:

Message Examples

Simple Q&A

Image Generation

Pre-populated Context

Message Validation

Ensure messages contain actual content. The Client will return an error message if you attempt to send with an empty message queue.

Best Practices

Use Type::User for all user inputs and Type::Assistant for AI responses. Don’t mix these up as it affects conversation context.
Always match on content_type when handling responses, especially with image generation models that may return either text or images.
Message IDs are automatically generated and should be preserved. Use with_id() only when reconstructing conversation history.
For image messages, prefer using send_with_image_download() to automatically populate base64_data for offline access.
  • Client - Learn about the Client interface
  • Conversations - Manage multi-turn conversations
  • Models - Choose the right model for your messages