Deepgram Python SDK
Official Python SDK for Deepgram. Power your apps with world-class speech and Language AI models.
- Documentation
- Migrating from earlier versions
- Requirements
- Installation
- Initialization
- Pre-Recorded (Synchronous)
- Pre-Recorded (Asynchronous / Callbacks)
- Streaming Audio
- Transcribing to Captions
- Voice Agent
- Text to Speech REST
- Text to Speech Streaming
- Text Intelligence
- Authentication
- Projects
- Keys
- Members
- Scopes
- Invitations
- Usage
- Billing
- Models
- On-Prem APIs
- Logging
- Backwards Compatibility
- Development and Contributing
- Getting Help
Documentation
You can learn more about the Deepgram API at developers.deepgram.com.
Migrating from earlier versions
V2 to V3
We have published a migration guide on our docs, showing how to move from v2 to v3.
V3.* to V4
The Voice Agent interfaces have been updated to use the new Voice Agent V1 API. Please refer to our Documentation on Migration to new V1 Agent API.
Requirements
Python (version ^3.10)
Installation
To install the latest version available:
pip install deepgram-sdk
Initialization
All of the examples below will require DeepgramClient.
from deepgram import DeepgramClient
# Initialize the client
deepgram = DeepgramClient("YOUR_API_KEY") # Replace with your API key
Getting an API Key
🔑 To access the Deepgram API you will need a free Deepgram API Key.
Pre-Recorded (Synchronous)
Remote Files (Synchronous)
Transcribe audio from a URL.
from deepgram import PrerecordedOptions
response = deepgram.listen.rest.v("1").transcribe_url(
source={"url": "https://dpgr.am/spacewalk.wav"},
options=PrerecordedOptions(model="nova-3") # Apply other options
)
See our API reference for more info.
Local Files (Synchronous)
Transcribe audio from a file.
from deepgram import PrerecordedOptions
response = deepgram.listen.rest.v("1").transcribe_file(
source=open("path/to/your/audio.wav", "rb"),
options=PrerecordedOptions(model="nova-3") # Apply other options
)
See our API reference for more info.
Pre-Recorded (Asynchronous / Callbacks)
Remote Files (Asynchronous)
Transcribe audio from a URL.
from deepgram import PrerecordedOptions
response = deepgram.listen.rest.v("1").transcribe_url_async(
source={"url": "https://dpgr.am/spacewalk.wav"},
callback_url="https://your-callback-url.com/webhook",
options=PrerecordedOptions(model="nova-3") # Apply other options
)
See our API reference for more info.
Local Files (Asynchronous)
Transcribe audio from a file.
from deepgram import PrerecordedOptions
response = deepgram.listen.rest.v("1").transcribe_file_async(
source=open("path/to/your/audio.wav", "rb"),
callback_url="https://your-callback-url.com/webhook",
options=PrerecordedOptions(model="nova-3") # Apply other options
)
See our API reference for more info.
Streaming Audio
Transcribe streaming audio.
from deepgram import LiveOptions, LiveTranscriptionEvents
# Create a websocket connection
connection = deepgram.listen.websocket.v("1")
# Handle transcription events
@connection.on(LiveTranscriptionEvents.Transcript)
def handle_transcript(result):
print(result.channel.alternatives[0].transcript)
# Start connection with streaming options
connection.start(LiveOptions(model="nova-3", language="en-US"))
# Send audio data
connection.send(open("path/to/your/audio.wav", "rb").read())
# Close when done
connection.finish()
See our API reference for more info.
Transcribing to Captions
Transcribe audio to captions.
WebVTT
from deepgram_captions import DeepgramConverter, webvtt
transcription = DeepgramConverter(dg_response)
captions = webvtt(transcription)
SRT
from deepgram_captions import DeepgramConverter, srt
transcription = DeepgramConverter(dg_response)
captions = srt(transcription)
See our stand alone captions library for more information..
Voice Agent
Configure a Voice Agent.
from deepgram import (
SettingsOptions,
Speak
)
# Create websocket connection
connection = deepgram.agent.websocket.v("1")
# Configure agent settings
options = SettingsOptions()
options.language = "en"
options.agent.think.provider.type = "open_ai"
options.agent.think.provider.model = "gpt-4o-mini"
options.agent.think.prompt = "You are a helpful AI assistant."
options.agent.listen.provider.type = "deepgram"
options.agent.listen.provider.model = "nova-3"
# Configure multiple TTS providers for automatic fallback.
primary = Speak()
primary.provider.type = "deepgram"
primary.provider.model = "aura-2-zeus-en"
fallback = Speak()
fallback.provider.type = "cartesia"
fallback.provider.model = "sonic-english"
options.agent.speak = [primary, fallback]
# Set Agent greeting
options.greeting = "Hello, I'm your AI assistant."
# Start the connection
connection.start(options)
# Close the connection
connection.finish()
This example demonstrates:
- Setting up a WebSocket connection
- Configuring the agent with speech, language, and audio settings
- Handling various agent events (speech, transcripts, audio)
- Sending audio data and keeping the connection alive
For a complete implementation, you would need to:
- Add your audio input source (e.g., microphone)
- Implement audio playback for the agent's responses
- Handle any function calls if your agent uses them
- Add proper error handling and connection management
See our API reference for more info.
Text to Speech REST
Convert text into speech using the REST API.
from deepgram import SpeakOptions
# Configure speech options
options = SpeakOptions(model="aura-2-thalia-en")
# Convert text to speech and save to file
response = deepgram.speak.rest.v("1").save(
"output.mp3",
{"text": "Hello world!"},
options
)
See our API reference for more info.
Text to Speech Streaming
Convert streaming text into speech using a Websocket.
from deepgram import (
SpeakWSOptions,
SpeakWebSocketEvents
)
# Create websocket connection
connection = deepgram.speak.websocket.v("1")
# Handle audio data
@connection.on(SpeakWebSocketEvents.AudioData)
# Configure streaming options
options = SpeakWSOptions(
model="aura-2-thalia-en",
encoding="linear16",
sample_rate=16000
)
# Start connection and send text
connection.start(options)
connection.send_text("Hello, this is a text to speech example.")
connection.flush()
connection.wait_for_complete()
# Close when done
connection.finish()
See our API reference for more info.
Text Intelligence
Analyze text.
from deepgram import ReadOptions
# Configure read options
options = ReadOptions(
model="nova-3",
language="en"
)
# Process text for intelligence
response = deepgram.read.rest.v("1").process(
text="The quick brown fox jumps over the lazy dog.",
options=options
)
See our API reference for more info.
Authentication
The Deepgram Python SDK supports multiple authentication methods to provide flexibility and enhanced security for your applications.
Authentication Methods
API Key Authentication (Traditional)
The traditional method using your Deepgram API key:
from deepgram import DeepgramClient
# Direct API key
client = DeepgramClient(api_key="YOUR_API_KEY")
# Or using environment variable DEEPGRAM_API_KEY
client = DeepgramClient() # Auto-detects from environment
Bearer Token Authentication (OAuth 2.0)
Use short-lived access tokens for enhanced security:
from deepgram import DeepgramClient
# Direct access token
client = DeepgramClient(access_token="YOUR_ACCESS_TOKEN")
# Or using environment variable DEEPGRAM_ACCESS_TOKEN
client = DeepgramClient() # Auto-detects from environment
Authentication Priority
When multiple credentials are provided, the SDK follows this priority order:
- Explicit
access_tokenparameter (highest priority) - Explicit
api_keyparameter DEEPGRAM_ACCESS_TOKENenvironment variableDEEPGRAM_API_KEYenvironment variable (lowest priority)
Environment Variables
Set your credentials using environment variables:
# For API key authentication
export DEEPGRAM_API_KEY="your-deepgram-api-key"
# For bearer token authentication
export DEEPGRAM_ACCESS_TOKEN="your-access-token"
Dynamic Authentication Switching
Switch between authentication methods at runtime:
from deepgram import DeepgramClient, DeepgramClientOptions
# Start with API key
config = DeepgramClientOptions(api_key="your-api-key")
client = DeepgramClient(config=config)
# Switch to access token
client._config.set_access_token("your-access-token")
# Switch back to API key
client._config.set_apikey("your-api-key")
Complete Bearer Token Workflow
Here's a practical example of using API keys to obtain access tokens:
from deepgram import DeepgramClient
# Step 1: Create client with API key
api_client = DeepgramClient(api_key="your-api-key")
# Step 2: Get a short-lived access token (30-second TTL)
response = api_client.auth.v("1").grant_token()
access_token = response.access_token
# Step 3: Create new client with Bearer token
bearer_client = DeepgramClient(access_token=access_token)
# Step 4: Use the Bearer client for API calls
transcription = bearer_client.listen.rest.v("1").transcribe_url(
{"url": "https://dpgr.am/spacewalk.wav"}
)
Benefits of Bearer Token Authentication
- Enhanced Security: Short-lived tokens (30-second expiration) minimize risk
- OAuth 2.0 Compliance: Standard bearer token format
- Scope Limitation: Tokens can be scoped to specific permissions
- Audit Trail: Better tracking of token usage vs API keys
Authentication Management
Get Token Details
Retrieves the details of the current authentication token:
response = deepgram.manage.rest.v("1").get_token_details()
Grant Token
Creates a temporary token with a 30-second TTL:
response = deepgram.auth.v("1").grant_token()
See our API reference for more info.
Projects
Get Projects
Returns all projects accessible by the API key.
response = deepgram.manage.v("1").get_projects()
See our API reference for more info.
Get Project
Retrieves a specific project based on the provided project_id.
response = deepgram.manage.v("1").get_project(myProjectId)
See our API reference for more info.
Update Project
Update a project.
response = deepgram.manage.v("1").update_project(myProjectId, options)
See our API reference for more info.
Delete Project
Delete a project.
response = deepgram.manage.v("1").delete_project(myProjectId)
See our API reference for more info.
Keys
List Keys
Retrieves all keys associated with the provided project_id.
response = deepgram.manage.v("1").get_keys(myProjectId)
See our API reference for more info
Get Key
Retrieves a specific key associated with the provided project_id.
response = deepgram.manage.v("1").get_key(myProjectId, myKeyId)
See our API reference for more info
Create Key
Creates an API key with the provided scopes.
response = deepgram.manage.v("1").create_key(myProjectId, options)
See our API reference for more info
Delete Key
Deletes a specific key associated with the provided project_id.
response = deepgram.manage.v("1").delete_key(myProjectId, myKeyId)
See our API reference for more info
Members
Get Members
Retrieves account objects for all of the accounts in the specified project_id.
response = deepgram.manage.v("1").get_members(myProjectId)
See our API reference for more info.
Remove Member
Removes member account for specified member_id.
response = deepgram.manage.v("1").remove_member(myProjectId, MemberId)
See our API reference for more info.
Scopes
Get Member Scopes
Retrieves scopes of the specified member in the specified project.
response = deepgram.manage.v("1").get_member_scopes(myProjectId, memberId)
See our API reference for more info.
Update Scope
Updates the scope for the specified member in the specified project.
response = deepgram.manage.v("1").update_member_scope(myProjectId, memberId, options)
See our API reference for more info.
Invitations
List Invites
Retrieves all invitations associated with the provided project_id.
response = deepgram.manage.v("1").get_invites(myProjectId)
See our API reference for more info.
Send Invite
Sends an invitation to the provided email address.
response = deepgram.manage.v("1").send_invite(myProjectId, options)
See our API reference for more info.
Delete Invite
Removes the specified invitation from the project.
response = deepgram.manage.v("1").delete_invite(myProjectId, email)
See our API reference for more info.
Leave Project
response = deepgram.manage.v("1").leave_project(myProjectId)
See our API reference for more info.
Usage
Get All Requests
Retrieves all requests associated with the provided project_id based on the provided options.
response = deepgram.manage.v("1").get_usage_requests(myProjectId)
See our API reference for more info.
Get Request
Retrieves a specific request associated with the provided project_id
response = deepgram.manage.v("1").get_usage_request(myProjectId, RequestId)
See our API reference for more info.
Get Fields
Lists the features, models, tags, languages, and processing method used for requests in the specified project.
response = deepgram.manage.v("1").get_usage_fields(myProjectId)
See our API reference for more info.
Summarize Usage
Deprecated Retrieves the usage for a specific project. Use Get Project Usage Breakdown for a more comprehensive usage summary.
response = deepgram.manage.v("1").get_usage_summary(myProjectId)
See our API reference for more info.
Billing
Get All Balances
Retrieves the list of balance info for the specified project.
response = deepgram.manage.v("1").get_balances(myProjectId)
See our API reference for more info.
Get Balance
Retrieves the balance info for the specified project and balance_id.
response = deepgram.manage.v("1").get_balance(myProjectId)
See our API reference for more info.
Models
Get All Project Models
Retrieves all models available for a given project.
response = deepgram.manage.v("1").get_project_models(myProjectId)
See our API reference for more info.
Get Model
Retrieves details of a specific model.
response = deepgram.manage.v("1").get_project_model(myProjectId, ModelId)
See our API reference for more info.
On-Prem APIs
List On-Prem credentials
Lists sets of distribution credentials for the specified project.
response = deepgram.selfhosted.v("1").list_selfhosted_credentials(projectId)
See our API reference for more info.
Get On-Prem credentials
Returns a set of distribution credentials for the specified project.
response = deepgram.selfhosted.v("1").get_selfhosted_credentials(projectId, distributionCredentialsId)
See our API reference for more info.
Create On-Prem credentials
Creates a set of distribution credentials for the specified project.
response = deepgram.selfhosted.v("1").create_selfhosted_credentials(project_id, options)
See our API reference for more info.
Delete On-Prem credentials
Deletes a set of distribution credentials for the specified project.
response = deepgram.selfhosted.v("1").delete_selfhosted_credentials(projectId, distributionCredentialId)
See our API reference for more info.
Pinning Versions
To ensure your application remains stable and reliable, we recommend using version pinning in your project. This is a best practice in Python development that helps prevent unexpected changes. You can pin to a major version (like ==4.*) for a good balance of stability and updates, or to a specific version (like ==4.1.0) for maximum stability. We've included some helpful resources about version pinning and dependency management if you'd like to learn more. For a deeper understanding of how version numbers work, check outsemantic versioning.
In a requirements.txt file, you can pin to a specific version like this:
deepgram-sdk==4.1.0
Or using pip:
pip install deepgram-sdk==4.1.0
Logging
This SDK provides logging as a means to troubleshoot and debug issues encountered. By default, this SDK will enable Information level messages and higher (ie Warning, Error, etc) when you initialize the library as follows:
deepgram: DeepgramClient = DeepgramClient()
To increase the logging output/verbosity for debug or troubleshooting purposes, you can set the DEBUG level but using this code:
config: DeepgramClientOptions = DeepgramClientOptions(
verbose=logging.DEBUG,
)
deepgram: DeepgramClient = DeepgramClient("", config)
Testing
Daily and Unit Tests
If you are looking to use, run, contribute or modify to the daily/unit tests, then you need to install the following dependencies:
pip install -r requirements-dev.txt
Daily Tests
The daily tests invoke a series of checks against the actual/real API endpoint and save the results in the tests/response_data folder. This response data is updated nightly to reflect the latest response from the server. Running the daily tests does require a DEEPGRAM_API_KEY set in your environment variables.
To run the Daily Tests:
make daily-test
Unit Tests
The unit tests invoke a series of checks against mock endpoints using the responses saved in tests/response_data from the daily tests. These tests are meant to simulate running against the endpoint without actually reaching out to the endpoint; running the unit tests does require a DEEPGRAM_API_KEY set in your environment variables, but you will not actually reach out to the server.
make unit-test
Backwards Compatibility
We follow semantic versioning (semver) to ensure a smooth upgrade experience. Within a major version (like 4.*), we will maintain backward compatibility so your code will continue to work without breaking changes. When we release a new major version (like moving from 3.* to 4.*), we may introduce breaking changes to improve the SDK. We'll always document these changes clearly in our release notes to help you upgrade smoothly.
Older SDK versions will receive Priority 1 (P1) bug support only. Security issues, both in our code and dependencies, are promptly addressed. Significant bugs without clear workarounds are also given priority attention.
Development and Contributing
Interested in contributing? We ❤️ pull requests!
To make sure our community is safe for all, be sure to review and agree to our Code of Conduct. Then see the Contribution guidelines for more information.
In order to develop new features for the SDK itself, you first need to uninstall any previous installation of the deepgram-sdk and then install/pip the dependencies contained in the requirements.txt then instruct python (via pip) to use the SDK by installing it locally.
From the root of the repo, that would entail:
pip uninstall deepgram-sdk
pip install -r requirements.txt
pip install -e .
Getting Help
We love to hear from you so if you have questions, comments or find a bug in the project, let us know! You can either:
Metadata
Release files for deepgram-sdk 4.8.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| deepgram_sdk-4.8.1.tar.gz | 100.1 kB | Without provenance | Details for deepgram_sdk-4.8.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| deepgram_sdk-4.8.1-py3-none-any.whl | Python 3 | none | any | Details for deepgram_sdk-4.8.1-py3-none-any.whl |
Total release size: 258.0 kB
Release files / deepgram_sdk-4.8.1.tar.gz
| Download URL | deepgram_sdk-4.8.1.tar.gz |
|---|---|
| Size | 100.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aada4c34532096bb8c438ed23c6d182920e8cba9609bf1b1e40a7f30820cfa32
|
|
BLAKE2b-256 checksum How to use checksums |
e17a420a885ddfc16b6142353414e8e529882a1e261fd4cfbae449ce84d59590
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / deepgram_sdk-4.8.1-py3-none-any.whl
| Download URL | deepgram_sdk-4.8.1-py3-none-any.whl |
|---|---|
| Size | 157.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1af2a9fab57c858f2201bebd8f553fdfe9ef3d1b036e0ac588104b11ae3bc727
|
|
BLAKE2b-256 checksum How to use checksums |
7d8570151387088f110623ff066d6e27d1169f5dd130eaf8b2044f4de9d41034
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for deepgram-sdk 4.8.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.