A Dart client library for the Mistral AI API, covering chat completions and audio transcription.
- Chat completions —
chat()with system/user/assistant messages, multimodal content parts, JSON mode (response_format), temperature, max tokens, top-p, and seed. - Audio transcription —
transcribeAudio()with multipart file upload, file URLs, or file IDs; language, temperature, and diarization options; timestamp granularity for per-segment and per-word timings; progress callbacks; confidence scores. - Structured errors —
MistralApiException(API errors with status code and message),MistralNetworkException(connection failures), andMistralException(validation errors). - Testable — accepts an injected
http.Clientand overridablebaseUrl.
Add the dependency to your pubspec.yaml:
dependencies:
mistral_client:
git:
url: https://github.com/HolotypePteLtd/mistral_voice_client.gitThe package is not yet published to pub.dev; once it is, you'll be able to use
mistral_client: ^0.1.0.
import 'package:mistral_client/mistral_client.dart';
final client = MistralClient(apiKey: 'YOUR_API_KEY');
final response = await client.chat(ChatRequest.simple(
model: 'mistral-small-latest',
systemPrompt: 'You are a helpful assistant.',
userMessage: 'What is the capital of France?',
));
print(response.choices.first.message.content);
client.dispose();For structured output, enable JSON mode:
final response = await client.chat(ChatRequest.simple(
model: 'mistral-small-latest',
systemPrompt: 'Return JSON.',
userMessage: 'List three colors as a JSON array.',
jsonMode: true,
));final client = MistralClient(apiKey: 'YOUR_API_KEY');
final file = File('recording.mp3');
final response = await client.transcribeAudio(
TranscriptionRequest(
model: 'voxtral-mini-latest',
fileBytes: await file.readAsBytes(),
fileName: file.path,
),
onProgress: (progress) => print('${(progress * 100).toStringAsFixed(0)}%'),
);
print(response.text);
// Timed segments:
for (final segment in response.segments) {
print('[${segment.start}s - ${segment.end}s] ${segment.text}');
}
// Per-word timings (request with timestampGranularities: ['word']):
for (final word in response.words) {
print('${word.text} ${word.start}-${word.end}s');
}
client.dispose();A small CLI is included for quick testing:
dart run bin/transcribe.dart <api-key> <audio-file-path>
dart run bin/transcribe.dart <api-key> --url <audio-url>dart pub get
dart test