The Sarvam Conversational AI SDK for Flutter is a Dart library that helps developers build real-time voice-to-voice and text-based conversational AI applications. It provides a unified interface for managing conversation flow, handling audio streams, and processing real-time messages on both iOS and Android platforms.

Overview

The Sarvam Conv AI SDK enables developers to create applications that can:
  • Build real-time voice-to-voice conversational experiences on mobile
  • Create text-based chat applications using Sarvam agents
  • Handle audio capture from microphone and playback to speakers automatically
  • Manage conversation lifecycle with robust event handling
  • Support multiple Indian languages for conversational AI

Installation

Add the SDK to your pubspec.yaml:
Then run:

Platform Setup

iOS

Add microphone permission to ios/Runner/Info.plist:

Android

Add permission to android/app/src/main/AndroidManifest.xml:

Quick Start

Voice-to-Voice Conversation

Here’s a complete example for voice interaction:

Text-Based Conversation


SamvaadAgent

The main class for managing conversational AI sessions. It automatically handles voice or text modes based on the interactionType configuration.

Constructor Parameters

*Required when not using a proxy server

Methods

Future<void> start()

Start the conversation session and establish WebSocket connection.

Future<void> stop()

Stop the conversation session and cleanup resources.

Future<bool> waitForConnect({Duration? timeout})

Wait until the WebSocket connection is established. Returns true if connected, false if timeout.

Future<void> waitForDisconnect()

Wait until the WebSocket disconnects or the agent is stopped.

bool get isConnected

Check if the WebSocket is currently connected.

String? get interactionId

Get the current interaction identifier.

String? get referenceId

Get the reference ID for this session (useful for telephony integrations).

Future<void> sendAudio(Uint8List audioData)

Send raw audio data (only for voice interactions). Audio must be 16-bit PCM mono at the configured sample rate.

Future<void> sendText(String text)

Send a text message (available for both voice and text interactions).

Future<void> sendVoiceNote(Uint8List audioData, {bool transcribe = true})

Send a complete voice note for transcription.

Configuration

InteractionConfig

The configuration object that defines the conversation parameters.

Required Fields

Optional Fields

Important
If version is not provided, the SDK uses the latest committed version of the app.
The connection will fail if the provided appId has no committed version.

Example Configuration


Audio Interfaces

DefaultAudioInterface

Handles microphone capture and speaker playback with platform-specific implementations.
Features:
  • Automatic microphone access and audio capture
  • Real-time audio streaming at 8kHz, 16kHz, or 48kHz
  • Automatic speaker playback of agent responses
  • Handles user interruptions
  • Platform-specific optimizations (iOS: flutter_sound, Android: native AudioTrack)
Audio Format: LINEAR16 (16-bit PCM mono) Platform Requirements:
  • iOS: Microphone usage description in Info.plist
  • Android: RECORD_AUDIO permission in AndroidManifest.xml

Custom Audio Interface

Implement your own audio interface by implementing the AudioInterface abstract class:
Example custom implementation:

Event Handling

The SDK provides callbacks for different types of events during the conversation.

Text Callback

Receives streaming text chunks from the agent:

Audio Callback

Receives raw audio chunks (if not using DefaultAudioInterface):

Event Callback

Receives various conversation events:

Supported Languages

The SDK supports multilingual conversations using the SarvamToolLanguageName enum:
Available languages:
  • SarvamToolLanguageName.bengali - Bengali
  • SarvamToolLanguageName.gujarati - Gujarati
  • SarvamToolLanguageName.kannada - Kannada
  • SarvamToolLanguageName.malayalam - Malayalam
  • SarvamToolLanguageName.tamil - Tamil
  • SarvamToolLanguageName.telugu - Telugu
  • SarvamToolLanguageName.punjabi - Punjabi
  • SarvamToolLanguageName.odia - Odia
  • SarvamToolLanguageName.marathi - Marathi
  • SarvamToolLanguageName.hindi - Hindi
  • SarvamToolLanguageName.english - English
Note: The allowed languages are a subset that is preselected while defining the agent configuration on the platform. Example usage:

Proxy Server Setup

For production apps, you should hide your Sarvam API key from client devices by routing requests through a proxy server. See the Proxy Setup guide for server configuration, Flutter client examples, and security best practices.

Message Types

Server Message Types

The SDK handles various message types from the server:

Client Message Types

Messages sent from the SDK to the server:

Best Practices

1. Resource Cleanup

Always cleanup resources when done:

2. Connection Timeout

Always specify a timeout when waiting for connection:

3. Error Handling

Implement comprehensive error handling:

4. State Management

Use proper state management for UI updates:

5. Handle App Lifecycle

Stop the agent when the app goes to background:

Troubleshooting

Common Issues

1. Microphone Permission Denied

2. Connection Timeout

  • Check internet connectivity
  • Verify API key is valid
  • Ensure org_id, workspace_id, and app_id are correct
  • Make sure the app has a committed version

3. No Audio Output

  • Check device volume
  • Verify audio interface is properly initialized
  • Ensure sample rate matches server configuration

4. WebSocket Disconnects

Reconnection is not supported — each WebSocket URL is single-use. If the connection drops, stop the agent and create a new instance:
  • Handle app lifecycle (pause/resume) to stop the agent when backgrounded
  • On disconnect, call stop() then create a fresh SamvaadAgent and call start()