Skip to main content

Documentation Index

Fetch the complete documentation index at: https://assemblyai.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Streaming profanity filtering lets you automatically mask profane words in your streaming transcripts in real time. When enabled, the API replaces profane words with asterisks in both partial and final turns before sending them to the client. The mask uses the first letter of the word followed by n - 1 asterisks (for example, shit becomes s***). Apostrophes, capitalization, and surrounding punctuation are preserved (for example, shit's becomes s***'s). Profanity filtering supports all streaming models: u3-rt-pro, universal-streaming-english, and universal-streaming-multilingual. It also works alongside other features such as format_turns and PII redaction.
Pre-recorded profanity filteringFor profanity filtering on pre-recorded audio, see Filter profanity from transcripts.

Connection parameters

ParameterTypeRequiredDefaultDescription
filter_profanitybooleanNofalseEnable real-time profanity filtering. When true, profane words in both partial and final turns are masked with asterisks (first letter preserved). The server accepts the truthy strings true, 1, and yes. Invalid values cause the WebSocket to close with code 3006.
include_partial_turnsbooleanNotrueWhen false, the API only sends final turns. Useful with filter_profanity: true if you display partials directly to end-users and want to avoid any unmasked profanity flashing during word completion.

Quickstart

Get started with streaming profanity filtering using the code below. This example streams 16 kHz mono PCM audio from your microphone and prints each turn with profanity masked.
1
Install the required libraries
pip install websocket-client pyaudio
2
Create a new file main.py and paste the code below. Replace <YOUR_API_KEY> with your API key.
3
Run with python main.py and speak into your microphone.
import pyaudio
import websocket
import json
import threading
import time
from urllib.parse import urlencode

YOUR_API_KEY = "<YOUR_API_KEY>"
CONNECTION_PARAMS = {
    "sample_rate": 16000,
    "speech_model": "u3-rt-pro",
    "format_turns": "true",
    "filter_profanity": "true",
}
API_ENDPOINT_BASE_URL = "wss://streaming.assemblyai.com/v3/ws"
API_ENDPOINT = f"{API_ENDPOINT_BASE_URL}?{urlencode(CONNECTION_PARAMS)}"

FRAMES_PER_BUFFER = 800
SAMPLE_RATE = CONNECTION_PARAMS["sample_rate"]
CHANNELS = 1
FORMAT = pyaudio.paInt16

audio = None
stream = None
ws_app = None
audio_thread = None
stop_event = threading.Event()

def on_open(ws):
    print("WebSocket connection opened.")

    def stream_audio():
        global stream
        while not stop_event.is_set():
            try:
                audio_data = stream.read(FRAMES_PER_BUFFER, exception_on_overflow=False)
                ws.send(audio_data, websocket.ABNF.OPCODE_BINARY)
            except Exception as e:
                print(f"Error streaming audio: {e}")
                break

    global audio_thread
    audio_thread = threading.Thread(target=stream_audio)
    audio_thread.daemon = True
    audio_thread.start()

def on_message(ws, message):
    try:
        data = json.loads(message)
        msg_type = data.get("type")
        if msg_type == "Begin":
            print(f"Session began: ID={data.get('id')}")
        elif msg_type == "Turn":
            transcript = data.get("transcript", "")
            end_of_turn = data.get("end_of_turn", False)
            if end_of_turn:
                print(f"\r{' ' * 80}\r{transcript}")
        elif msg_type == "Termination":
            print(f"\nSession terminated: {data.get('audio_duration_seconds', 0)}s of audio")
    except Exception as e:
        print(f"Error handling message: {e}")

def on_error(ws, error):
    print(f"\nWebSocket Error: {error}")
    stop_event.set()

def on_close(ws, close_status_code, close_msg):
    print(f"\nWebSocket Disconnected: Status={close_status_code}")
    global stream, audio
    stop_event.set()
    if stream:
        if stream.is_active():
            stream.stop_stream()
        stream.close()
    if audio:
        audio.terminate()

def run():
    global audio, stream, ws_app
    audio = pyaudio.PyAudio()
    stream = audio.open(
        input=True,
        frames_per_buffer=FRAMES_PER_BUFFER,
        channels=CHANNELS,
        format=FORMAT,
        rate=SAMPLE_RATE,
    )
    print("Speak into your microphone. Press Ctrl+C to stop.")
    ws_app = websocket.WebSocketApp(
        API_ENDPOINT,
        header={"Authorization": YOUR_API_KEY},
        on_open=on_open,
        on_message=on_message,
        on_error=on_error,
        on_close=on_close,
    )
    ws_thread = threading.Thread(target=ws_app.run_forever)
    ws_thread.daemon = True
    ws_thread.start()
    try:
        while ws_thread.is_alive():
            time.sleep(0.1)
    except KeyboardInterrupt:
        print("\nStopping...")
        stop_event.set()
        if ws_app and ws_app.sock and ws_app.sock.connected:
            ws_app.send(json.dumps({"type": "Terminate"}))
            time.sleep(2)
        if ws_app:
            ws_app.close()
        ws_thread.join(timeout=2.0)

if __name__ == "__main__":
    run()
Suppress unmasked partials with include_partial_turns=falseProfanity filtering applies to both partial and final turns, but during word-completion an unmasked partial can briefly appear before the model resolves the word and applies the mask. If your application surfaces partials directly to end-users (for example a live caption stream or voice-agent UI), set include_partial_turns: false on the connection to suppress all partial turns and only receive masked finals. The default is true (partials enabled), so this requires an explicit opt-out.

Example output

With filter_profanity=true, a final turn might look like:
s*** is what you say when you stub your toe.
The mask preserves word length, apostrophes, and surrounding punctuation, so a word like shit's is returned as s***'s and motherfucker becomes m***********.

Supported models

Streaming profanity filtering works with all streaming models on both the US and EU endpoints:
  • u3-rt-pro
  • universal-streaming-english
  • universal-streaming-multilingual

Troubleshooting

The streaming filter targets the same word list as pre-recorded profanity filtering and only masks words on that list. Some words you might consider profane, such as crap and damn, are intentionally not masked and pass through unchanged. If you need stricter filtering, apply your own post-processing on top of the masked transcript.
Profanity masking applies during word classification, so an unmasked partial can briefly appear before the word is fully recognized and masked. If your UI surfaces partials directly to users, set include_partial_turns: false on the connection. Final turns are always masked.