Make a sequence_ai.voice.Session, give it the microphone’s audio, and play what it gives back. The session hears where a sentence ends, has it written as text, calls a language model with your tools, speaks the answer sentence by sentence while the rest is still being written, and lets a person talk over it. It is made for a robot you talk to: two hooks tell your code that someone began to speak, and what they said, before any reply does.
- A voice activity model, which runs on your computer, finds each thing a person says in the microphone’s audio.
transcribe()writes it as text: the first speech-to-text model you named that answers.chat()is called with the conversation and your tools, streamed.- Each sentence of the answer is one
speech(), streamed, and its audio is played as it is made. - The tools the model called are run, and it is called again with what they returned.
pip install "sequence-ai[voice]" export SEQUENCES_API_KEY=... # your key
The voice extra brings three packages: pysilero-vad, the voice activity model; pywebrtc-audio, the echo canceller, on Python 3.10 or later; and sounddevice, the microphone and loudspeaker of this computer. A session whose audio comes from somewhere else needs only the first: pip install sequence-ai pysilero-vad.
An agent in one file
# agent_user.py from sequence_ai import voice from sequence_ai.voice import device, echo, vad def open_gripper_user(width_mm_user: float) -> str: """Open the gripper. Args: width_mm_user: How wide to open it, in millimetres. """ ... # your robot's own call return f"The gripper is open {width_mm_user:g} mm." session_user = voice.Session( vad=vad.SileroModel(), # where speech starts and ends echo=echo.WebRtcEchoCanceller(), # the loudspeaker's own sound out of the microphone's stt=["universal-3-6-pro", "nova-3"], language="en", # hear llm="claude-haiku-5-5", # decides what to say and do tts="aura-2", voice="aura-2-thalia-en", # speaks instructions="You run a robot's gripper. You are heard, not read: short plain sentences.", tools=[open_gripper_user], ) device.run(session_user) # this computer's microphone and loudspeaker, until Ctrl-C
python agent_user.py
session_user.reply(instructions="Say that you are ready, and ask what to do."), before device.run(), has the agent speak first: a turn the session begins by itself, the instructions told to the model for that turn alone.
What a session takes
| Argument | What it is |
|---|---|
vad | The voice activity model: vad.SileroModel(). |
stt | A speech-to-text model’s id, or several. |
llm | The language model’s id. server= sends its calls to a server of your own, as chat(server=) does. |
tts, voice, format | The text-to-speech model, its voice and the format it is asked for, as speech() takes them. |
tts_options, llm_options, stt_options | More arguments for each speech(), chat() and transcribe() the session makes, as those functions take them. |
instructions | What the language model is told it is. |
tools | Python functions the model may call. |
language, keyterms | As transcribe() takes them. |
diarize | True asks the speech-to-text model who spoke, and takes the loudest voice for the person talking to it. |
echo | An echo canceller: echo.WebRtcEchoCanceller(). |
endpointing_mode | "dynamic" learns the wait before a turn ends from the person’s own pauses. Left out, the same wait every time. |
on_speech_started, on_heard | The two hooks: a person began to speak, and what they said. |
before_llm | Called with each call’s messages; what it returns is sent in their place. |
text_transforms | What is taken out of the model’s text on its way to the voice. Left out, markdown and emoji. |
text_pacing | True: after a reply’s first sentence, the next ones are sent to be spoken only when five seconds or less of what was made are left to play, several in one speech(). Left out, each sentence is sent when the one before it has been made. |
on_error | Called with the error when a call fails for good. |
api_key and base_url are as the three functions take them; left out, the environment’s.
Two models that hear
stt may name several models. Each thing said goes to the first; when it fails, or gives no answer in ten seconds (stt_timeout_s), the same audio goes to the next. keyterms=["SO-101", "left gripper"] are words to listen for: the session sends them only to the models that take them, which sequence_ai.models(endpoint="transcription") says in each one’s raw["keyterms"].
More than one voice in the room
diarize=True asks the speech-to-text model who spoke each word, and the session takes one voice for the person talking to it, by how loud it is alone: the loudest in each thing said, and from one thing said to the next a voice at least half as loud as that one has been. By itself that changes nothing of what is heard. To leave out what the other voices say:
from sequence_ai import voice from sequence_ai.voice import speakers, vad session_user = voice.Session( vad=vad.SileroModel(), stt=["mai-transcribe-2", "nemotron-3-5-transcribe"], diarize=speakers.Speakers(suppress_background_speaker=True), llm="claude-haiku-5-5", tts="aura-2", voice="aura-2-thalia-en", )
A thing said by background voices alone then counts as nothing heard: on_heard(""), and the model is not called. Loudness is all it goes by: anyone less than half as loud as the person has been is taken for background - the person themself too, when they speak that much more quietly - and that thing said does not count. A stretch too short to measure, under a third of a second, is never left out: it may be “stop”. Left out is only what you ask for: by default nothing said is lost. A robot that runs with it on must not rely on a voice to stop it.
Only the models of stt that tell speakers apart are asked - sequence_ai.models(endpoint="transcription") says which, in each one’s raw["diarize"] - in the order you gave them; when none of them does, the session is refused when it is made, with the models that do. Which voices are told apart is the model’s own doing: two very short things said one after the other may be written as one speaker’s, and then nothing of them is left out.
Give it a tool
A tool is a Python function. The model reads its name, the first paragraph of its docstring, each argument’s type and the line the docstring’s Args: gives it; what the function returns is what the model reads next. A tool runs on a thread of the session’s and may take as long as it needs: the session goes on listening meanwhile. After at most three rounds of tools (max_tool_steps) the model is asked for its answer alone.
Show it a picture
before_llm is called with the messages of each call of the language model, just before it. Add what the model should see now, as chat() takes an image:
import sequence_ai def with_picture_user(messages_user: list) -> list: picture_user = sequence_ai.image(camera_user.read()) # a file's path, bytes, a PIL image or a numpy array return [*messages_user, {"role": "user", "content": [picture_user, {"type": "text", "text": "The camera, now."}]}]
Pass it as before_llm=with_picture_user. The picture goes with that call alone and is not kept in the conversation. The model must be one that reads images.
Say a line of your own
session_user.say("The gripper is open.") # spoken as given: no call of the language model
say() is the session’s turn as a reply is: it is kept in the conversation, spoken after a reply that is going on, and dropped when a person talks over it. A line that never changes can be made once and played from then on:
import sequence_ai ready_user = sequence_ai.speech("aura-2", "Ready when you are.", voice="aura-2-thalia-en", format="mp3") session_user.say("Ready when you are.", audio=ready_user) # played as it is: no speech() either
audio is what speech() returned, the bytes of a file in the session’s format, or a numpy array of 16-bit mono samples at 24 kHz. ready_user.save("ready_user.mp3") keeps it for the next run.
session_user.interrupt() drops what the session is saying and what waits to be said: a stop button for your own code. What was heard stays in the conversation, a tool that is running finishes, and neither hook is called.
What is spoken, and what is kept
A language model writes **bold**, lists, links and emoji; a voice should not read the marks aloud. They are taken out of the text on its way to the voice, and the conversation keeps what the model wrote. text_transforms=() turns that off. A function of your own goes in the same list: it takes the text as it is written, piece by piece, and gives it back changed.
def no_brackets_user(pieces_user): for piece_user in pieces_user: yield piece_user.replace("[", "").replace("]", "")
Pass it as text_transforms=["filter_markdown", "filter_emoji", no_brackets_user].
A voice from a recording
tts_options goes with every speech() the session makes. With a model that clones a voice, {"reference": ...} is a recording of the voice to speak in, and "reference_text" what the recording says:
from sequence_ai import voice from sequence_ai.voice import vad session_user = voice.Session( vad=vad.SileroModel(), stt="universal-3-6-pro", llm="claude-haiku-5-5", tts="s2-1-pro", tts_options={"reference": "my_voice_user.wav", "reference_text": "This is how I sound."}, )
sequence_ai.models(endpoint="speech") says which models clone, in each one’s raw["references"]: 1 or more. llm_options={"max_tokens": 300} and stt_options={"prompt": "A robot arm, an SO-101."} go with each chat() and each transcribe() in the same way. An argument the session sets itself, such as voice or tools, is given to the session and refused there.
A robot that stops when you speak
A reply reaches your tools only after the person has finished, their words are text, and the model has answered. Something that must stop the moment a person starts to speak listens to the two hooks instead:
import threading hold_user = threading.Event() # your robot's loop stands still while this is set stop_user = threading.Event() # and ends what it is doing when this is def speech_started_user() -> None: hold_user.set() # someone began to speak: hold, before a word is known def heard_user(text_user: str) -> None: if text_user: stop_user.set() # it was words: end what the robot is doing; the model answers them hold_user.clear() # known now: nothing more to wait for
Pass them as on_speech_started=speech_started_user, on_heard=heard_user. on_heard is called once for each thing said, when its text is known: the words, or "" when it was none or no model could say. Each hook is called on a thread of the session’s and should return at once. A spoken “stop” is no emergency stop: keep a physical one on anything that moves.
Audio of your own
device.run() is one way to give a session audio. A robot with a microphone array, or a sound card its own loop drives, calls the session itself:
session_user.hear(pcm_user) # the microphone: 16-bit mono PCM at 16 kHz, as it comes out_user = session_user.read(480) # the loudspeaker's next 480 samples, 16-bit mono PCM at 24 kHz
Call hear() steadily, with silence too: time passes for the session as audio does. read() answers silence when there is nothing to say. With an echo canceller, hear(pcm_user, far_user) takes what the loudspeaker played over the same time, at the microphone’s rate and length.
What happens when the person talks over it
- Half a second of speech holds the reply. Its audio stops where it is. If no words come back, it plays on two seconds after the person falls silent; if words do, the rest of it is dropped and they are answered.
- The conversation keeps what was heard. A reply that was dropped stays in the conversation up to where its sound stopped: the sentences heard to their end, and the words heard of the one it stopped in.
- A tool that is running is not stopped. It finishes, and its result is kept; the two hooks are how your code learns to stop it.
- A sentence in two halves is one turn. When a person pauses and goes on, the session waits for the text of all they said before the model answers.
- The wait before a turn ends can follow the person. A turn ends half a second after the person was last heard (
min_delay), and not before their words are back as text. Withendpointing_mode="dynamic", each time they stop and go on with no reply between, the wait moves a tenth of the way to the length of that pause, never undermin_delaynor overmax_delay, three seconds: someone who pauses long in the middle of a sentence is given longer before they are answered. - For the first three seconds after the session first speaks, while its voice plays, a person’s voice is told to
on_speech_startedas any other, so a robot stops on it, but it is taken for no words:on_heard(""), the reply plays on, and nothing is written as text (aec_warmup_duration). An echo canceller needs that long to learn the room, and until then what would be written might be the session’s own words. Say it again once it is quiet.
What it costs
Each call is billed as its function’s call is, at the model’s own rate: each thing said is one transcribe(), each call of the language model one chat(), each sentence spoken one speech().
- After every round of tools the model is called again, with what they returned.
- The model may be called before a turn is over. When text comes back while the person is speaking again, the model is called with what they have said so far. If they add words, that call is cut off and made again with all of it. With models that hear whole sentences this is rare: the text usually comes back after the person has stopped.
early=Falseturns it off. - A call of the language model that is cut off is billed for what it had written.
- Sentences are made ahead of what is heard. A sentence is billed whole once its sound has begun to come back, heard or not. Without
text_pacing, each sentence is its ownspeech(), sent when the one before it has been made - while earlier ones are still playing, and while a reply is held as well. A voice is made faster than it plays, so when a person talks over a long answer, what was made by then and not yet heard is billed too. Withtext_pacing=True, the first sentence is sent at once and the next ones only when five seconds or less of what was made are left to play - several sentences in onespeech(), added until they come to three hundred characters - and none while a reply is held: a long answer talked over is billed for at most those seconds and one batch more than was heard. That is less than without it where the voice is made much faster than it plays, and can be more where it is made only a little faster, a batch being several sentences.text_pacing=pacing.Pacing(min_remaining_audio=3.0, max_text_length=200), fromsequence_ai.voice, sets the two numbers. - A line said with its
audiomakes no call.say(text, audio=...)plays what it was given. - A call the platform refuses is not billed: a wrong key, no credit left, an argument a model does not take.
When a call fails
- A speech-to-text model fails: the same audio goes to the next model of
stt. It is not reported toon_error; a warning is logged. - No model can hear: the error is reported, and what was said counts as nothing heard:
on_heard(""). The session goes on listening. - The language model fails: the error is reported and that reply is over. The session goes on listening.
- A sentence cannot be spoken: the error is reported, and the next sentence is tried.
An error is reported to on_error; without one it is logged under sequence_ai.voice.