Skip to navigation

Audio Decisions in Pipecat

Run a local Pipecat voice agent and follow how Audio Decisions answers reach its LLM.

Run a Pipecat bot that classifies the user’s speech with HumeAudioClassifier and attaches the Audio Decisions answers to each transcription before it reaches the LLM. Clone this example to follow along, or fork it as a starting point.

By the end you will have the bot running locally, and you will know how the perception source is configured, where PerceptionReader sits in the pipeline, how expressive conversation passes the answers to the LLM, and how to log the user message the LLM receives.

Repositoryhume-audio-decisions-pipecat-example on GitHub
Packagepipecat-ai[hume,local,openai,silero] on PyPI, version 1.17.0 or later
Python3.11 to 3.14

Prerequisites

  1. macOS 14 or later on Apple silicon, or Linux on x86-64 or ARM64 with glibc 2.28 or later.
  2. Python 3.11 to 3.14, and Git.
  3. PortAudio, which PyAudio needs to reach your microphone and speakers. PyAudio builds from source, so install PortAudio before the dependencies: brew install portaudio on macOS. Debian and Ubuntu also need a C compiler, the Python headers, and the venv module: sudo apt-get install build-essential portaudio19-dev python3-dev python3-venv.
  4. A microphone, and headphones. LocalAudioTransport has no echo cancellation, so without headphones the microphone picks up the bot’s speech as user speech.
  5. An API key from the API keys page of the Hume Platform. Your organization needs access to Audio Decisions, which you can request through the contact form.
  6. An OpenAI API key. The bot uses OpenAI for speech-to-text, the LLM, and text-to-speech.

Run the example

Clone the repository and install its dependencies. If you use uv, it needs version 0.13.6 or later and installs the Python version in .python-version for you. If you use pip, first check that python3 --version reports 3.11 to 3.14.

Shell
git clone https://github.com/HumeAI/hume-audio-decisions-pipecat-example.git
cd hume-audio-decisions-pipecat-example
uv sync

Copy the example environment file.

Shell
cp .env.example .env

Set both API keys in .env. A key exported in your shell takes precedence over the file. The bot stops at startup when HUME_API_KEY is empty or unset, and stops on its first OpenAI request when OPENAI_API_KEY is empty or wrong.

.env
HUME_API_KEY=your_hume_api_key
OPENAI_API_KEY=your_openai_api_key

Start the bot, and on macOS allow your terminal app to use the microphone when asked. The bot introduces itself, then listens. Press Ctrl+C to stop it.

Shell
uv run bot.py

Speak, then pause. When the user turn ends, the bot logs the user message as the LLM receives it: the reading, which holds the answers Pipecat attached to the turn, then the transcription. The reading has one signal per question, named by the question’s key and followed by its answer: a yes or no question’s probability, a choice question’s two likeliest options with their probabilities, and a score question’s position on its scale. With the example’s three questions, frustration, speakers, and intelligibility, the lines look like these, shown without loguru’s prefix.

Terminal
User: ([frustration value=0.12],[speakers one=0.98, two=0.01],[intelligibility value=2.8]) Hi, I'd like to change the shipping address on my order.
User: ([frustration value=0.43],[speakers one=0.91, two=0.09],[intelligibility value=2.85]) The order number is 4521, and it should go to my office instead.

When a reading is missing or unexpected, check these causes.

  1. (no reading) on some turns means those turns have no signals. Either the answers missed the 2-second timeout, which logs a warning, or the request failed, which logs an error. HTTP 401 in that error means the Hume API key is wrong, and HTTP 403 means your organization lacks access to Audio Decisions.
  2. (no reading) on every turn, with no warning or error, means expressive conversation is off or the user aggregator has no vad_analyzer.
  3. Most questions need at least 5 seconds of audio, and a short user turn right after the bot’s turn is sent with less. See How it sends audio.

How it works

bot.py starts from Pipecat’s local voice agent example, swaps in OpenAI speech-to-text and text-to-speech, and adds the Hume pieces. Each step below links the section of the Pipecat integration guide that explains its pieces.

  1. Configure the perception source

    1. PerceptionSource pairs HumeAudioClassifier with the questions, keyed by name. Each key becomes a signal name in the reading, and each description tells the LLM how to read that signal. See Add it to a pipeline.

    2. Each question type reads differently. A YesNoQuestion reads as the chance a rater would say yes, a ChoiceQuestion as its likeliest options with their probabilities, and a ScoreQuestion as a position on its levels, counted from 0. Each description tells the LLM which kind of reading to expect. See Map Pipecat questions to primitives.

    3. timeout is the wait limit: the reader holds each transcription for up to 2 seconds after the end of speech. Answers that arrive later miss that user turn, and the reader logs a warning.

    4. PerceptionReader is the processor that sends the user’s audio to its sources and attaches their answers to the transcription.

    5. ProcessorUnusablePolicy.END ends the pipeline when a processor becomes unusable, such as when OpenAI rejects its key. A failed Hume request doesn’t make PerceptionReader unusable, so a rejected Hume key logs a warning at startup and an error per clip, and the bot keeps running without readings. See Handle setup and request failures.

  2. Place the reader in the pipeline

    1. transport.input() pushes the microphone audio downstream as InputAudioRawFrame, and the reader keeps that audio to cut each clip, the user’s audio it sends in one request.

    2. The user aggregator runs SileroVADAnalyzer and broadcasts its voice activity detection (VAD) frames, so the reader, upstream of it, receives each VADUserStoppedSpeakingFrame. See How it sends audio.

    3. The reader sits between the speech-to-text service and the user aggregator, so it can hold each TranscriptionFrame until its answers arrive. The request goes out when VAD reports the end of speech, before turn detection ends the user’s turn, so it runs while speech-to-text finishes the transcription. Audio Decisions answers whatever clip the reader sends.

  3. Turn on expressive conversation

    1. enable_expressive_conversation prefixes each user message with its reading and adds a section to the system prompt that explains the reading and defines each signal by its description. When a user turn has several clips, the reading keeps each signal’s highest value across them. See Turn on expressive conversation and How the answers reach the LLM.

    2. SYSTEM_INSTRUCTION doesn’t mention the questions, because that section covers them.

    3. The guidance lines of the ExpressionPolicy go in that system prompt section and tell the LLM what to do with each signal, such as the threshold at which to respond to it. A policy can also steer speech with expression tags, which OpenAITTSService doesn’t perform.

  4. Log the user message

    1. add_event_handler registers log_turn for the user aggregator’s on_user_turn_stopped event. message.perception holds the turn’s reading, and format_perception renders it as the LLM sees it. A turn without a transcription is skipped. See Check that answers reach the LLM.

  5. Queue the greeting

    1. The developer message asks the LLM to introduce the bot, and LLMRunFrame runs the LLM before the user speaks. Once the bot has spoken, the reader sends its last reply as the context of each request, so the user’s first clip goes out with the greeting. See How it sends audio.

import asyncio
import os
import sys
from dotenv import load_dotenv
from loguru import logger
from pipecat.audio.vad.silero import SileroVADAnalyzer
from pipecat.classifiers.base_classifier import (
ChoiceQuestion,
ScoreQuestion,
YesNoQuestion,
)
from pipecat.classifiers.hume.audio_decisions.classifier import HumeAudioClassifier
from pipecat.expressive.perception import format_perception
from pipecat.expressive.policy import ExpressionPolicy
from pipecat.expressive.processors.perception_reader import (
PerceptionQuestion,
PerceptionReader,
PerceptionSource,
)
from pipecat.frames.frames import LLMRunFrame
from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.worker import (
PipelineParams,
PipelineWorker,
ProcessorUnusablePolicy,
)
from pipecat.processors.aggregators.llm_context import LLMContext
from pipecat.processors.aggregators.llm_response_universal import (
LLMContextAggregatorPair,
LLMUserAggregator,
LLMUserAggregatorParams,
UserTurnStoppedMessage,
)
from pipecat.services.openai.llm import OpenAILLMService
from pipecat.services.openai.stt import OpenAISTTService
from pipecat.services.openai.tts import OpenAITTSService
from pipecat.transports.local.audio import (
LocalAudioTransport,
LocalAudioTransportParams,
)
from pipecat.turns.user_stop import BaseUserTurnStopStrategy
from pipecat.workers.runner import WorkerRunner
SYSTEM_INSTRUCTION = (
"You are a helpful assistant in a voice conversation. Your responses will be "
"spoken aloud, so avoid emojis, bullet points, or other formatting that can't be "
"spoken. Keep your responses brief."
)
async def log_turn(
aggregator: LLMUserAggregator,
strategy: BaseUserTurnStopStrategy | None,
message: UserTurnStoppedMessage,
) -> None:
if not message.content:
return
reading = (
format_perception(message.perception) if message.perception else "(no reading)"
)
logger.info(f"User: {reading} {message.content}")
async def main() -> None:
load_dotenv()
# Pipecat's DEBUG logs would bury the readings.
logger.remove()
logger.add(sys.stderr, level="INFO")
transport = LocalAudioTransport(
LocalAudioTransportParams(audio_in_enabled=True, audio_out_enabled=True)
)
stt = OpenAISTTService(api_key=os.environ["OPENAI_API_KEY"])
hume = PerceptionSource(
classifier=HumeAudioClassifier(api_key=os.environ["HUME_API_KEY"]),
questions={
"frustration": PerceptionQuestion(
question=YesNoQuestion(
instructions="Does the speaker sound frustrated?"
),
description=(
"Estimated share of raters who would say the user sounded "
"frustrated, from 0 to 1."
),
),
"speakers": PerceptionQuestion(
question=ChoiceQuestion(
instructions="How many distinct speakers are there?",
options={
"one": None,
"two": None,
"three_or_more": None,
},
),
description=(
"How many distinct voices were in the user's audio, with how "
"likely each count is."
),
),
"intelligibility": PerceptionQuestion(
question=ScoreQuestion(
instructions=(
"How much of the speaker's speech can a listener understand?"
),
levels=["Almost none", "Some", "Most", "All"],
),
description=(
"How much of the user's speech a listener could understand, as a "
"position on a scale from 0 (almost none) to 3 (all). It is a "
"measure, not a confidence, so a low value is a real reading."
),
),
},
timeout=2.0,
)
reader = PerceptionReader(sources=[hume])
llm = OpenAILLMService(
api_key=os.environ["OPENAI_API_KEY"],
settings=OpenAILLMService.Settings(system_instruction=SYSTEM_INSTRUCTION),
)
tts = OpenAITTSService(api_key=os.environ["OPENAI_API_KEY"])
context = LLMContext()
# The reader cuts a clip for Hume each time this VAD reports the end of speech.
user_aggregator, assistant_aggregator = LLMContextAggregatorPair(
context,
user_params=LLMUserAggregatorParams(vad_analyzer=SileroVADAnalyzer()),
)
user_aggregator.add_event_handler("on_user_turn_stopped", log_turn)
pipeline = Pipeline(
[
transport.input(),
stt,
reader,
user_aggregator,
llm,
tts,
transport.output(),
assistant_aggregator,
]
)
worker = PipelineWorker(
pipeline,
params=PipelineParams(
# Without expressive conversation, the readings never reach the LLM.
enable_expressive_conversation=True,
# OpenAI TTS performs no expression tags, so Pipecat warns at startup
# that the policy's tags aren't taught. The guidance still reaches the LLM.
expression_policy=ExpressionPolicy(
guidance=[
"If frustration is 0.6 or above, acknowledge once that the user "
"sounds frustrated, then focus on helping.",
"If speakers is two or three_or_more at 0.5 or above, someone "
"near the user may be talking too: answer only what the user "
"asked you, and if the words seem meant for someone else, ask "
"whether they're talking to you.",
"If intelligibility is below 1.5, the transcription may be wrong: "
"say back what you heard and ask the user to confirm or repeat it "
"before acting on it.",
],
),
),
processor_unusable_policy=ProcessorUnusablePolicy.END,
)
runner = WorkerRunner()
await runner.add_workers(worker)
context.add_message(
{"role": "developer", "content": "Please introduce yourself to the user."}
)
await worker.queue_frames([LLMRunFrame()])
await runner.run()
if __name__ == "__main__":
asyncio.run(main())

Adapt it to your agent

  1. Add it to an existing bot. Install pipecat-ai with the hume extra and set HUME_API_KEY. Copy the PerceptionSource and PerceptionReader, and place reader between your speech-to-text service and your user aggregator. Give the user aggregator a vad_analyzer if it has none, and set enable_expressive_conversation=True in PipelineParams, with an ExpressionPolicy for any guidance.
  2. Change the questions. Edit or add questions in questions, each with a description for the LLM. Each key must be one word with none of =,[](), or the bot stops at startup. When the LLM should act on a signal, add a line for it to the guidance of the ExpressionPolicy. See Map Pipecat questions to primitives and Writing questions.
  3. Set your own thresholds. The thresholds in the guidance lines are the example’s. Set yours from your own calls, as in Setting thresholds, once your questions and their order are final, since adding, removing, or reordering questions can change the other answers. Pipecat’s instructions tell the LLM that “a reading below 0.5 is a guess: don’t act on it”, so a threshold below 0.5 conflicts with them.
  4. Use other services. Any Pipecat speech-to-text, LLM, or text-to-speech service can replace the OpenAI ones. With a text-to-speech service that performs expression tags, the ExpressionPolicy can also choose how replies are spoken; see Turn on expressive conversation.
  5. Act on answers outside the LLM. Every answer is also broadcast in a PerceptionFrame as soon as it arrives, including answers that miss the wait limit. A processor placed after the reader can act on them in your backend, such as alerting a supervisor; see Use answers without waiting for the turn.
  6. Use another transport. LocalAudioTransport serves only your own machine. To reach users over WebRTC or a phone line, replace it with another Pipecat transport. The reader takes InputAudioRawFrame from any input transport, and HumeAudioClassifier resamples that audio to 16 kHz, so the rest of the pipeline stays as it is.

Next steps