Skip to main content
Loss of provenance

A previous release had provenance metadata, but this release does not. View details.

Deepgram Python SDK

Built with Fern PyPI version Python 3.8+ MIT License

The official Python SDK for Deepgram's automated speech recognition, text-to-speech, and language understanding APIs. Power your applications with world-class speech and Language AI models.

Documentation

Comprehensive API documentation and guides are available at developers.deepgram.com.

Migrating From Earlier Versions

Installation

Install the Deepgram Python SDK using pip:

pip install deepgram-sdk

Reference

Usage

Quick Start

The Deepgram SDK provides both synchronous and asynchronous clients for all major use cases:

Real-time Speech Recognition (Listen v2)

Our newest and most advanced speech recognition model with contextual turn detection (WebSocket Reference):

from deepgram import DeepgramClient
from deepgram.core.events import EventType

client = DeepgramClient()

with client.listen.v2.connect(
    model="flux-general-en",
    encoding="linear16",
    sample_rate="16000"
) as connection:
    def on_message(message):
        print(f"Received {message.type} event")

    connection.on(EventType.OPEN, lambda _: print("Connection opened"))
    connection.on(EventType.MESSAGE, on_message)
    connection.on(EventType.CLOSE, lambda _: print("Connection closed"))
    connection.on(EventType.ERROR, lambda error: print(f"Error: {error}"))

    # Start listening and send audio data
    connection.start_listening()

File Transcription

Transcribe pre-recorded audio files (API Reference):

from deepgram import DeepgramClient

client = DeepgramClient()

with open("audio.wav", "rb") as audio_file:
    response = client.listen.v1.media.transcribe_file(
        request=audio_file.read(),
        model="nova-3"
    )
    print(response.results.channels[0].alternatives[0].transcript)

Text-to-Speech

Generate natural-sounding speech from text (API Reference):

from deepgram import DeepgramClient

client = DeepgramClient()

response = client.speak.v1.audio.generate(
    text="Hello, this is a sample text to speech conversion."
)

# Save the audio file
with open("output.mp3", "wb") as audio_file:
    audio_file.write(response.stream.getvalue())

Text Analysis

Analyze text for sentiment, topics, and intents (API Reference):

from deepgram import DeepgramClient

client = DeepgramClient()

response = client.read.v1.text.analyze(
    request={"text": "Hello, world!"},
    language="en",
    sentiment=True,
    summarize=True,
    topics=True,
    intents=True
)

Voice Agent (Conversational AI)

Build interactive voice agents (WebSocket Reference):

from deepgram import DeepgramClient
from deepgram.extensions.types.sockets import (
    AgentV1SettingsMessage, AgentV1Agent, AgentV1AudioConfig,
    AgentV1AudioInput, AgentV1Listen, AgentV1ListenProvider,
    AgentV1Think, AgentV1OpenAiThinkProvider, AgentV1SpeakProviderConfig,
    AgentV1DeepgramSpeakProvider
)

client = DeepgramClient()

with client.agent.v1.connect() as agent:
    settings = AgentV1SettingsMessage(
        audio=AgentV1AudioConfig(
            input=AgentV1AudioInput(encoding="linear16", sample_rate=44100)
        ),
        agent=AgentV1Agent(
            listen=AgentV1Listen(
                provider=AgentV1ListenProvider(type="deepgram", model="nova-3")
            ),
            think=AgentV1Think(
                provider=AgentV1OpenAiThinkProvider(
                    type="open_ai", model="gpt-4o-mini"
                )
            ),
            speak=AgentV1SpeakProviderConfig(
                provider=AgentV1DeepgramSpeakProvider(
                    type="deepgram", model="aura-2-asteria-en"
                )
            )
        )
    )

    agent.send_settings(settings)
    agent.start_listening()

Complete SDK Reference

For comprehensive documentation of all available methods, parameters, and options:

  • API Reference - Complete reference for REST API methods including:

    • Listen (Speech-to-Text): File transcription, URL transcription, and media processing
    • Speak (Text-to-Speech): Audio generation and voice synthesis
    • Read (Text Intelligence): Text analysis, sentiment, summarization, and topic detection
    • Manage: Project management, API keys, and usage analytics
    • Auth: Token generation and authentication management
  • WebSocket Reference - Detailed documentation for real-time connections:

    • Listen v1/v2: Real-time speech recognition with different model capabilities
    • Speak v1: Real-time text-to-speech streaming
    • Agent v1: Conversational voice agents with integrated STT, LLM, and TTS

Authentication

The Deepgram SDK supports two authentication methods:

Access Token Authentication

Use access tokens for temporary or scoped access (recommended for client-side applications):

from deepgram import DeepgramClient

# Explicit access token
client = DeepgramClient(access_token="YOUR_ACCESS_TOKEN")

# Or via environment variable DEEPGRAM_TOKEN
client = DeepgramClient()

# Generate access tokens using your API key
auth_client = DeepgramClient(api_key="YOUR_API_KEY")
token_response = auth_client.auth.v1.tokens.grant()
token_client = DeepgramClient(access_token=token_response.access_token)

API Key Authentication

Use your Deepgram API key for server-side applications:

from deepgram import DeepgramClient

# Explicit API key
client = DeepgramClient(api_key="YOUR_API_KEY")

# Or via environment variable DEEPGRAM_API_KEY
client = DeepgramClient()

Environment Variables

The SDK automatically discovers credentials from these environment variables:

  • DEEPGRAM_TOKEN - Your access token (takes precedence)
  • DEEPGRAM_API_KEY - Your Deepgram API key

Precedence: Explicit parameters > Environment variables

Async Client

The SDK provides full async/await support for non-blocking operations:

import asyncio
from deepgram import AsyncDeepgramClient

async def main():
    client = AsyncDeepgramClient()

    # Async file transcription
    with open("audio.wav", "rb") as audio_file:
        response = await client.listen.v1.media.transcribe_file(
            request=audio_file.read(),
            model="nova-3"
        )

    # Async WebSocket connection
    async with client.listen.v2.connect(
        model="flux-general-en",
        encoding="linear16",
        sample_rate="16000"
    ) as connection:
        async def on_message(message):
            print(f"Received {message.type} event")

        connection.on(EventType.MESSAGE, on_message)
        await connection.start_listening()

asyncio.run(main())

Exception Handling

The SDK provides detailed error information for debugging and error handling:

from deepgram import DeepgramClient
from deepgram.core.api_error import ApiError

client = DeepgramClient()

try:
    response = client.listen.v1.media.transcribe_file(
        request=audio_data,
        model="nova-3"
    )
except ApiError as e:
    print(f"Status Code: {e.status_code}")
    print(f"Error Details: {e.body}")
    print(f"Request ID: {e.headers.get('x-dg-request-id', 'N/A')}")
except Exception as e:
    print(f"Unexpected error: {e}")

Advanced Features

Raw Response Access

Access raw HTTP response data including headers:

from deepgram import DeepgramClient

client = DeepgramClient()

response = client.listen.v1.media.with_raw_response.transcribe_file(
    request=audio_data,
    model="nova-3"
)

print(response.headers)  # Access response headers
print(response.data)     # Access the response object

Request Configuration

Configure timeouts, retries, and other request options:

from deepgram import DeepgramClient

# Global client configuration
client = DeepgramClient(timeout=30.0)

# Per-request configuration
response = client.listen.v1.media.transcribe_file(
    request=audio_data,
    model="nova-3",
    request_options={
        "timeout_in_seconds": 60,
        "max_retries": 3
    }
)

Custom HTTP Client

Use a custom httpx client for advanced networking features:

import httpx
from deepgram import DeepgramClient

client = DeepgramClient(
    httpx_client=httpx.Client(
        proxies="http://proxy.example.com",
        timeout=httpx.Timeout(30.0)
    )
)

Retry Configuration

The SDK automatically retries failed requests with exponential backoff:

# Automatic retries for 408, 429, and 5xx status codes
response = client.listen.v1.media.transcribe_file(
    request=audio_data,
    model="nova-3",
    request_options={"max_retries": 3}
)

Contributing

We welcome contributions to improve this SDK! However, please note that this library is primarily generated from our API specifications.

Development Setup

  1. Install Poetry (if not already installed):

    curl -sSL https://install.python-poetry.org | python - -y --version 1.5.1
    
  2. Install dependencies:

    poetry install
    
  3. Install example dependencies:

    poetry run pip install -r examples/requirements.txt
    
  4. Run tests:

    poetry run pytest -rP .
    
  5. Run examples:

    python -u examples/listen/v2/connect/main.py
    

Contribution Guidelines

See our CONTRIBUTING guide.

Requirements

  • Python 3.8+
  • See pyproject.toml for full dependency list

Community Code of Conduct

Please see our community code of conduct before contributing to this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for deepgram-sdk 5.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Loss of provenance

These files do not have provenance metadata, despite files in a prior release including this data.

We recommend further investigation before installing these files. View details.

Source distribution (sdist)

Source distribution for deepgram-sdk 5.3.3
File Size Uploaded Provenance Actions
deepgram_sdk-5.3.3.tar.gz 151.5 kB Without provenance Details for deepgram_sdk-5.3.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for deepgram-sdk 5.3.3
File Interpreter ABI Platform Actions
deepgram_sdk-5.3.3-py3-none-any.whl Python 3 none any Details for deepgram_sdk-5.3.3-py3-none-any.whl

Total release size: 546.7 kB

Release files / deepgram_sdk-5.3.3.tar.gz

Loss of provenance

This file belongs to a release without provenance metadata, despite a prior release including this data.

We recommend further investigation before installing this file. View details.

Download URL deepgram_sdk-5.3.3.tar.gz
Size 151.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4387f8a0a3bcc12080d4b4e7be6f24c49c54585c89b6cd30d578bd185144fe3d
BLAKE2b-256 checksum
How to use checksums
a9f49501bdd7110bf4a6d5b79216d174609391a38a2aecfdb9bdbdf1e9704d4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.8.18 Linux/6.11.0-1018-azure

Release files / deepgram_sdk-5.3.3-py3-none-any.whl

Loss of provenance

This file belongs to a release without provenance metadata, despite a prior release including this data.

We recommend further investigation before installing this file. View details.

Download URL deepgram_sdk-5.3.3-py3-none-any.whl
Size 395.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc1d6237016625afc16f8b0ceddd331ed3fa5447745a61cfcf8fff982c847eb9
BLAKE2b-256 checksum
How to use checksums
dd2954f986f64063ad9b8f6fbf592de43a2728273de6ca4e4ecb8a6564c75450
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.8.18 Linux/6.11.0-1018-azure

Security for deepgram-sdk 5.3.3

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.

Loss of provenance

A previous release of these files had provenance metadata, but this release does not.

While this could reflect a routine change by the project maintainers, it may also indicate that the release files have been compromised.

Release comparison

  • Removed 6.0.0rc2 2 files With provenance
  • Added 5.3.3 2 files Without provenance

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.

Release history Release notifications | RSS feed

This release

5.3.3 This release

2 release files Loss of provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page