Skip to Content
ReferenceVoicevoice.answer()

voice.answer()

The answer() method is used in real-time voice providers to trigger the AI to generate a response. This method is particularly useful in speech-to-speech conversations where you need to explicitly signal the AI to respond after receiving user input.

Usage Example

import { OpenAIRealtimeVoice } from "@mastra/voice-openai-realtime"; import Speaker from "@mastra/node-speaker"; const speaker = new Speaker({ sampleRate: 24100, // Audio sample rate in Hz - standard for high-quality audio on MacBook Pro channels: 1, // Mono audio output (as opposed to stereo which would be 2) bitDepth: 16, // Bit depth for audio quality - CD quality standard (16-bit resolution) }); // Initialize a real-time voice provider const voice = new OpenAIRealtimeVoice({ realtimeConfig: { model: "gpt-4o", apiKey: process.env.OPENAI_API_KEY, }, speaker: "alloy", // Default voice }); // Connect to the real-time service await voice.connect(); // Register event listener for responses voice.on("speaker", (stream) => { // Handle audio response stream.pipe(speaker); }); // Send user audio input const microphoneStream = getMicrophoneStream(); await voice.send(microphoneStream); // Trigger the AI to respond await voice.answer(); // With custom options (provider-specific) await voice.answer({ options: { content: "Hello, how can I help you today?", voice: "nova", }, });

Parameters

options?:

Record<string, unknown>
Provider-specific options for the response

Return Value

Returns a Promise<void> that resolves when the response has been triggered.

Provider-Specific Options

Each real-time voice provider may support different options for the answer() method:

OpenAI Realtime

options.content?:

string
Text content to use for the response instead of generating one

options.voice?:

string
Voice ID to use for this specific response

Using with CompositeVoice

When using CompositeVoice, the answer() method delegates to the configured real-time provider:

import { CompositeVoice } from "@mastra/core/voice"; import { OpenAIRealtimeVoice } from "@mastra/voice-openai-realtime"; const realtimeVoice = new OpenAIRealtimeVoice(); const voice = new CompositeVoice({ realtimeProvider: realtimeVoice, }); // Connect to the real-time service await voice.connect(); // This will use the OpenAIRealtimeVoice provider await voice.answer();

Notes

  • This method is only implemented by real-time voice providers that support speech-to-speech capabilities
  • If called on a voice provider that doesn’t support this functionality, it will log a warning and resolve immediately
  • The response audio will typically be emitted through the ‘speaking’ event rather than returned directly
  • For providers that support it, you can use this method to send a specific response instead of having the AI generate one
  • This method is commonly used in conjunction with send() to create a conversational flow