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
TheMessage struct contains the following fields:
Fields
- id: Unique identifier (UUID) for the message
- role: Either
Type::UserorType::Assistant - content: The text content or image URL
- content_type: Either
ContentType::TextorContentType::Image - image_url: Optional URL for generated images
- base64_data: Optional base64-encoded image data
Type Enum
TheType enum represents the role of a message:
- User: Messages sent by the user to the API
- Assistant: Responses from the AI model
ContentType Enum
TheContentType 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:- A randomly generated UUID as the ID
ContentType::Text- No image data
Image Messages
Create a message containing an image:- A randomly generated UUID as the ID
ContentType::Image- The URL stored in both
contentandimage_url - Optional base64 data
Messages with Custom IDs
Create a message with a specific ID (useful for message reconstruction):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
Best Practices
Use appropriate message types
Use appropriate message types
Use
Type::User for all user inputs and Type::Assistant for AI responses. Don’t mix these up as it affects conversation context.Check content type before processing
Check content type before processing
Always match on
content_type when handling responses, especially with image generation models that may return either text or images.Preserve message IDs
Preserve message IDs
Message IDs are automatically generated and should be preserved. Use
with_id() only when reconstructing conversation history.Handle image data appropriately
Handle image data appropriately
For image messages, prefer using
send_with_image_download() to automatically populate base64_data for offline access.Related
- Client - Learn about the Client interface
- Conversations - Manage multi-turn conversations
- Models - Choose the right model for your messages