# FastRTC

> Turns any Python function into a real-time audio and video stream over WebRTC or WebSockets

- Category: Multimodal Generation / Audio, Music & Voice
- Language: Python
- Licence: MIT
- Repository: https://github.com/gradio-app/fastrtc
- Website: https://fastrtc.org
- Canonical page: https://ai-tldr.dev/tools/fastrtc/

## In short

FastRTC is the real-time communication library for Python, from the team behind Gradio. It sits in the Audio, Music & Voice category of the Multimodal Generation tools and is open-source, written in Python and licensed MIT. To get started: install the library.

## Overview

FastRTC is the real-time communication library for Python, from the team behind Gradio. It solves the plumbing problem that sits under every voice AI demo: getting microphone audio (or webcam video) from a browser to your model and the response back again, with low latency. You write an ordinary Python function; FastRTC wraps it in a `Stream` and handles WebRTC or WebSocket transport.

The parts that are tedious to build yourself come included. Automatic voice activity detection and turn taking mean you only write the logic for responding to the user, rather than deciding when they stopped talking. `.ui.launch()` gives you a WebRTC-enabled Gradio interface for testing, `.mount(app)` attaches the stream to a FastAPI app so your own front end can use the WebRTC or WebSocket endpoint, and `fastphone()` launches the app behind a free temporary phone number.

Because the backend is just a function mounted on FastAPI, it extends to production rather than stopping at the demo — the project's own cookbook covers wiring it to the Gemini and OpenAI real-time voice APIs, streaming webcam video and audio together to a model, and serving a custom JavaScript front end.

## What it does

- Wraps any Python function as a real-time audio/video stream over WebRTC or WebSockets
- Built-in voice activity detection and turn taking via `ReplyOnPause`, so you only write the response logic
- `.ui.launch()` for an instant WebRTC-enabled Gradio UI to test against
- `.mount(app)` to attach the stream to a FastAPI app and expose WebRTC or WebSocket endpoints to your own front end
- `fastphone()` launches the app and issues a free temporary phone number to call it on
- Optional `vad` and `tts` extras for pause detection and text to speech
- Fully customizable backend — a `Stream` is ordinary FastAPI, so it extends to production deployments

## Getting started

FastRTC installs from PyPI. Pause detection and text to speech live behind optional extras, so install those if you want the batteries-included voice loop.

1. Install the library

   The base install covers streaming. Add the extras to get built-in pause detection (`ReplyOnPause`) and text to speech.

```bash
pip install fastrtc

# with built-in pause detection and text to speech
pip install "fastrtc[vad, tts]"
```

2. Launch the built-in UI to test

   Calling `.ui.launch()` on a Stream starts the WebRTC-enabled Gradio interface, which is the fastest way to talk to your handler and confirm the audio loop works before you build a front end.

```python
stream.ui.launch()
```

3. Mount it on your own FastAPI app

   `.mount(app)` attaches the stream to a FastAPI application and exposes a WebRTC endpoint (and a WebSocket endpoint) for a custom front end, which is the path to production.

```python
stream.mount(app)
```

4. Call it on a real phone number

   `fastphone()` launches the application and returns a free temporary phone number, which is useful for testing a voice agent over real telephony without setting up a provider.

```python
stream.fastphone()
```

5. Work from the cookbook

   The documentation at fastrtc.org includes a cookbook of runnable examples — the Gemini and OpenAI real-time voice APIs, a webcam plus audio chat that streams both feeds to Gemini, and a 'Talk To Claude' demo that shows how to serve a custom JS front end.

## When to use it

- Building a voice agent where the model should reply when the user stops speaking, without hand-rolling turn detection
- Streaming webcam video and microphone audio together to a multimodal model
- Putting a WebRTC front end on an existing Python speech-to-text or text-to-speech pipeline
- Testing a voice application over real telephony using a temporary phone number before committing to a provider
