chatWithAudio method

Stream<String> chatWithAudio(
  1. String text,
  2. Uint8List audioBytes, {
  3. String? conversationId,
  4. bool enableThinking = false,
})

Send a multimodal chat message (text + audio) - Gemma 3n E4B only

Implementation

Stream<String> chatWithAudio(
  String text,
  Uint8List audioBytes, {
  String? conversationId,
  bool enableThinking = false,
}) async* {
  _assertInitialized();

  final convId = conversationId ?? _currentConversationId;
  if (convId == null) {
    throw StateError('No conversation. Call createConversation() first.');
  }

  final request = ChatWithAudioRequest()
    ..conversationId = convId
    ..text = text
    ..audio = audioBytes
    ..enableThinking = enableThinking;

  // Add timeout to prevent infinite hanging
  await for (final response in _client!.chatWithAudio(request).timeout(
    _streamTimeout,
    onTimeout: (sink) {
      sink.addError(TimeoutException(
        'Model response timed out after ${_streamTimeout.inMinutes} minutes',
      ));
      sink.close();
    },
  )) {
    if (response.hasError() && response.error.isNotEmpty) {
      throw Exception('Chat error: ${response.error}');
    }

    if (response.hasThinking() && response.thinking.isNotEmpty) {
      yield '<|channel>thought\n${response.thinking}<channel|>';
    }

    if (response.hasText() && response.text.isNotEmpty) {
      yield response.text;
    }
  }
}