Skip to content

SDKs

The official Gladia SDKs work with the Gladia Connect API: set the API address of your region.

Terminal window
pip install "gladiaio-sdk>=2.2" # Python 3.10 or later
npm install @gladiaio/sdk # Node.js 20 or later, and browsers

Language, endpointing, custom vocabulary and spelling, translation, sentiment, callbacks, custom_metadata: pass them to start_session / startSession as usual.

sdk_session.py
# pip install "gladiaio-sdk>=2.2"
import os
import sys
import threading
import wave
from gladiaio_sdk import GladiaClient, LiveV2InitRequest
API_URL = os.environ.get("GLADIA_API_URL", "https://eu1.api.gladia.io")
API_KEY = os.environ["GLADIA_API_KEY"]
with wave.open(sys.argv[1]) as wav: # a 16-bit PCM WAV file
rate, channels = wav.getframerate(), wav.getnchannels()
audio = wav.readframes(wav.getnframes())
config = {
"encoding": "wav/pcm",
"sample_rate": rate,
"bit_depth": 16,
"channels": channels,
"model": "solaria-3",
"language_config": {
"languages": [
"en"
]
},
"realtime_processing": {
"custom_vocabulary": True,
"custom_vocabulary_config": {
"vocabulary": [
"Gladia"
]
}
}
}
client = GladiaClient(api_key=API_KEY, api_url=API_URL)
session = client.live_v2().start_session(LiveV2InitRequest.from_dict(config))
ended, errors = threading.Event(), []
@session.on("message")
def on_message(message):
if message.type == "transcript" and message.data.is_final:
print(message.data.utterance.text)
session.on("error", errors.append)
session.once("ended", lambda _: ended.set())
# Stream once the socket is open: a stop sent while it opens can pass the audio queued until then.
connected = threading.Event()
session.once("connected", lambda _: connected.set())
session.once("ended", lambda _: connected.set()) # a session that never connects ends
session.on("error", lambda _: connected.set())
if session.status == "connected": # it opened before the listener above
connected.set()
if not connected.wait(timeout=30) or errors or ended.is_set():
sys.exit(1) # no open session to stream to
step = rate * channels * 2 // 10 # 100 ms of audio per chunk
for start in range(0, len(audio), step):
session.send_audio(audio[start : start + step]) # live audio: send each chunk as it is captured
session.stop_recording()
ended.wait(timeout=60)
sys.exit(1 if errors or not ended.is_set() else 0)

GLADIA_API_KEY and GLADIA_API_URL in the environment work too.

profile, punctuation, numerals, end_of_turn, diarization, named_entity_recognition with options, pii and turn_router are not in the SDKs’ types: pass the configuration as a plain dict (Python) or object (JavaScript) to start_session / startSession. The SDK sends it as is, then streams, buffers and reconnects as usual.

  • Python: pass the dict itself, not a LiveV2InitRequest: it only keeps the options it types, and turns an object into True where it types a boolean (named_entity_recognition with options). To keep type checking on the common options, build them with LiveV2InitRequest(...).to_dict() and add the others: start_session(LiveV2InitRequest(encoding="wav/pcm").to_dict() | {"numerals": True}).
  • TypeScript: the configuration needs a cast (as LiveV2InitRequest).
sdk_enterprise_options.py
# pip install -U "gladiaio-sdk>=2.2"
import os
import sys
import threading
import wave
from gladiaio_sdk import GladiaClient
API_URL = os.environ.get("GLADIA_API_URL", "https://eu1.api.gladia.io")
API_KEY = os.environ["GLADIA_API_KEY"]
with wave.open(sys.argv[1]) as wav: # a 16-bit PCM WAV file
rate, channels = wav.getframerate(), wav.getnchannels()
audio = wav.readframes(wav.getnframes())
config = {
"encoding": "wav/pcm",
"sample_rate": rate,
"bit_depth": 16,
"channels": channels,
"model": "solaria-3",
"language_config": {
"languages": [
"en"
]
},
"numerals": True,
"end_of_turn": True
}
client = GladiaClient(api_key=API_KEY, api_url=API_URL)
# `numerals` and `end_of_turn`: Gladia Connect, not in the SDK's types: a dict is sent as is.
session = client.live_v2().start_session(config)
ended, errors = threading.Event(), []
@session.on("message")
def on_message(message):
if message.type == "transcript" and message.data.is_final:
print(message.data.utterance.text)
session.on("error", errors.append)
session.once("ended", lambda _: ended.set())
# Stream once the socket is open: a stop sent while it opens can pass the audio queued until then.
connected = threading.Event()
session.once("connected", lambda _: connected.set())
session.once("ended", lambda _: connected.set()) # a session that never connects ends
session.on("error", lambda _: connected.set())
if session.status == "connected": # it opened before the listener above
connected.set()
if not connected.wait(timeout=30) or errors or ended.is_set():
sys.exit(1) # no open session to stream to
step = rate * channels * 2 // 10 # 100 ms of audio per chunk
for start in range(0, len(audio), step):
session.send_audio(audio[start : start + step]) # live audio: send each chunk as it is captured
session.stop_recording()
ended.wait(timeout=60)
sys.exit(1 if errors or not ended.is_set() else 0)
  • Messages the SDKs do not type (pii, end_of_turn, end_of_turn_score) arrive on the message event with their type and raw data. In Python they are LiveV2UnknownWebSocketMessage objects: if isinstance(message, LiveV2UnknownWebSocketMessage) and message.type == "end_of_turn" (import it from gladiaio_sdk.v2.live.generated_types).
  • Fields the SDKs do not type on known messages (text_raw, entities and forced of an utterance, the speaker of a word) are there: in Python, read them with getattr(utterance, "entities", []); their values are plain JSON (dicts, lists).
  • model: any model name works (solaria-3, or with a latency profile: solaria-3-fast).
  • In a browser, never put the API key in the page: create the session on your server and give the page its url, then use connectSession (Authentication).
  • region: leave the SDKs’ region option unset; the API address you set is the region.