Overview
TheClient struct is the main entry point for all interactions with the t3.chat API. It manages authentication, session handling, conversation state, and API communication.
Structure
The Client maintains several key pieces of state:- Authentication: Cookies and Convex session ID for API authentication
- Thread Management: Optional thread ID for conversation continuity
- Message History: Vector of messages in the current conversation
- HTTP Client: Configured reqwest client with proper headers
Lifecycle
Creating a Client
Initialize a new client with your authentication credentials:Initializing the Session
Before sending messages, initialize the client to establish a session:Sending Messages
Send messages to the API and receive responses:Session Management
Refreshing Sessions
The client automatically refreshes sessions when sending messages, but you can manually refresh:wos-session cookie by calling the active sessions endpoint.
Thread Management
Threads represent conversation contexts in t3.chat:Image Operations
Generating and Downloading Images
For image generation models, usesend_with_image_download to automatically download generated images:
Manual Image Downloads
Download images from URLs:Response Parsing
The client automatically parses EventStream responses from the API: Method Signature:- Text content (
String) - Optional image URL (
Option<String>) - Optional base64 image data (
Option<String>)
Error Handling
Most Client methods returnResult types:
The
send method returns an error message as a valid Message object when no messages are queued, rather than returning an Err result.Best Practices
Always initialize before sending
Always initialize before sending
Call
client.init().await? after creating a new client to establish a proper session.Reuse client instances
Reuse client instances
Create one client per authentication context and reuse it for multiple conversations to maintain session state.
Handle conversation state
Handle conversation state
Use
new_conversation() when starting fresh topics to avoid context bleed between unrelated conversations.Check content type for images
Check content type for images
Always check
response.content_type when using image generation models to handle both text and image responses.Related
- Messages - Learn about Message types
- Conversations - Manage multi-turn conversations
- Models - Available model types