Skip to content

Voxtral chat with audio input

Exercises the audio-input branch of MistralClient.IChatClient end-to-end by: 1. Using Mistral TTS (/v1/audio/speech) to synthesize a short utterance. 2. Sending that audio to a Voxtral chat model (default voxtral-small-latest) as a Meai.DataContent with media type audio/wav, alongside a text prompt asking the model to repeat what it heard. 3. Asserting the model returns text that mentions the original utterance.

Skips when MISTRAL_API_KEY is unset, the account has no voices, the TTS endpoint refuses the request, or the Voxtral chat model isn't enabled for this account.

This example assumes using Mistral; is in scope and apiKey contains your Mistral 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using var client = new MistralClient(apiKey);

VoiceListResponse voices;
try
{
    voices = await client.AudioVoices.ListAllVoicesAsync();
}
catch (ApiException ex)
{
    throw new AssertInconclusiveException(
        $"Mistral voice listing unavailable (HTTP {(int?)ex.StatusCode}); skipping live chat-with-audio test.",
        ex);
}

if (voices.Items is not { Count: > 0 } items)
{
    throw new AssertInconclusiveException(
        "No voices available on this Mistral account; skipping live chat-with-audio test.");
}

const string utterance = "The quick brown fox jumps over the lazy dog.";

SpeechResponse speech;
try
{
    speech = await client.AudioSpeech.SpeechAsync(new SpeechRequest
    {
        Input = utterance,
        VoiceId = items[0].Id.ToString(),
        ResponseFormat = SpeechOutputFormat.Wav,
    });
}
catch (ApiException ex)
{
    throw new AssertInconclusiveException(
        $"Mistral TTS unavailable for this account (HTTP {(int?)ex.StatusCode}); skipping live chat-with-audio test.",
        ex);
}

var audioBytes = Convert.FromBase64String(speech.AudioData);

Meai.IChatClient chatClient = client;
Meai.ChatResponse response;
try
{
    response = await chatClient.GetResponseAsync(
        [
            new Meai.ChatMessage(Meai.ChatRole.User,
            [
                new Meai.TextContent("Repeat exactly what the speaker said in the audio, with no extra commentary."),
                new Meai.DataContent(audioBytes, mediaType: "audio/wav"),
            ]),
        ],
        new Meai.ChatOptions
        {
            ModelId = VoxtralChatModelId,
        });
}
catch (ApiException ex) when (ex.StatusCode is System.Net.HttpStatusCode.NotFound
                                  or System.Net.HttpStatusCode.Forbidden
                                  or System.Net.HttpStatusCode.BadRequest)
{
    throw new AssertInconclusiveException(
        $"Voxtral chat model '{VoxtralChatModelId}' not enabled for this account (HTTP {(int?)ex.StatusCode}); skipping live chat-with-audio test.",
        ex);
}