Skip to main content

Overview

The sendUserMessage() method sends text messages on behalf of the user programmatically. Use it when you want the persona to treat the text as a user turn and respond. Use addContext() when you want to add application state or user-action context without triggering an immediate persona response.
sendUserMessage() requires SDK version 3.3.0 or higher

Basic Usage

Send a message as if the user typed or spoke it:
The persona will receive this message and respond as if the user had actually said it.

Important Considerations

Messages sent via sendUserMessage() are not automatically added to the transcript. To maintain an accurate conversation history, you must manually add these messages to your transcript display.
The sendUserMessage() method differs from regular user messages in that:
  • It does not trigger message events that would normally update your UI
  • The message is sent directly to the persona without going through the normal message pipeline
  • You need to handle transcript updates separately in your application

Use Cases

Simulating User Input

Providing Context to the LLM

Use addContext() to provide contextual information that the LLM can use on the next user turn:
The persona will not respond immediately. The context is available the next time the user speaks or sends a message.
addContext() requires SDK version 4.11.0 or higher and an active streaming session. It throws an error if the client is not streaming or there is no active session.

Triggering Specific Flows

Custom Client-Side Transcription

Use sendUserMessage() to implement your own client-side speech-to-text transcription. Capture and transcribe audio using your preferred service, then send the transcribed messages directly:
With this approach you can use specialized transcription models, handle multiple languages, or implement custom preprocessing of the audio before sending it to the persona.

Complete Example

Here’s a complete example showing how to use sendUserMessage() with proper transcript management:

Error Handling

The sendUserMessage() method throws errors in two scenarios:
Always ensure the client is streaming before calling this method:

Next Steps

Events

Learn how to handle message events and build interactive experiences

Interrupt Command

Stop the persona from speaking mid-response
Last modified on July 8, 2026