Skip to content

Speech To Text Client Get Streaming Text Async

Real-time streaming speech-to-text via the MEAI ISpeechToTextClient interface. Uses typed ConnectAsync internally for model and language selection.

This example assumes using Deepgram; is in scope and apiKey contains your Deepgram API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using var client = new DeepgramClient(apiKey);
ISpeechToTextClient speechClient = client;

// Download a short audio sample to use as streaming input.
using var httpClient = new HttpClient();
var audioBytes = await httpClient.GetByteArrayAsync(
    "https://dpgr.am/spacewalk.wav");
using var audioStream = new MemoryStream(audioBytes);

// Stream audio through the MEAI ISpeechToTextClient interface.
// This uses typed ConnectAsync internally with model and language params.
var updates = new List<SpeechToTextResponseUpdate>();
await foreach (var update in speechClient.GetStreamingTextAsync(
    audioStream,
    new SpeechToTextOptions
    {
        ModelId = "nova-3",
        SpeechLanguage = "en",
    }))
{
    updates.Add(update);

    if (update.Kind == SpeechToTextResponseUpdateKind.TextUpdated)
    {
        Console.WriteLine($"Final: {update.Text}");
    }
}

// Verify we received session events and transcription results.

var finalTranscripts = updates
    .Where(u => u.Kind == SpeechToTextResponseUpdateKind.TextUpdated && u.Text is { Length: > 0 })
    .Select(u => u.Text!)
    .ToList();
Console.WriteLine($"Total final transcripts: {finalTranscripts.Count}");