deepgram-sdk-patterns
Apply production-ready Deepgram SDK patterns for TypeScript and Python. Use when implementing Deepgram integrations, refactoring SDK usage, or establishing team coding standards for Deepgram. Trigger with phrases like "deepgram SDK patterns", "deepgram best practices", "deepgram code patterns", "idiomatic deepgram", "deepgram typescript".
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 2.0k
- Forks
- 233
01 前言 Front Matter
- name
- deepgram-sdk-patterns
- author
- Jeremy Longshore <jeremy@intentsolutions.io>
- license
- MIT
- version
- 1.0.0
- description
- Apply production-ready Deepgram SDK patterns for TypeScript and Python. Use when implementing Deepgram integrations, refactoring SDK usage, or establishing team coding standards for Deepgram. Trigger with phrases like "deepgram SDK patterns", "deepgram best practices", "deepgram code patterns", "idiomatic deepgram", "deepgram typescript".
- allowed-tools
- Read, Write, Edit
03 SKILL.md 正文
Deepgram SDK Patterns
Overview
Production-ready patterns for Deepgram SDK integration with proper error handling, typing, and structure.
Prerequisites
- Completed
deepgram-install-authsetup - Familiarity with async/await patterns
- Understanding of error handling best practices
Instructions
Step 1: Create Type-Safe Client Singleton
Implement a singleton pattern for the Deepgram client.
Step 2: Add Robust Error Handling
Wrap all API calls with proper error handling and logging.
Step 3: Implement Response Validation
Validate API responses before processing.
Step 4: Add Retry Logic
Implement exponential backoff for transient failures.
Output
- Type-safe client singleton
- Robust error handling with structured logging
- Automatic retry with exponential backoff
- Runtime validation for API responses
Error Handling
| Error | Cause | Solution |
|---|---|---|
| Type Mismatch | Incorrect response shape | Add runtime validation |
| Client Undefined | Singleton not initialized | Call init() before use |
| Memory Leak | Multiple client instances | Use singleton pattern |
| Timeout | Large audio file | Increase timeout settings |
Examples
TypeScript Client Singleton
// lib/deepgram.ts
import { createClient, DeepgramClient } from '@deepgram/sdk';
let client: DeepgramClient | null = null;
export function getDeepgramClient(): DeepgramClient {
if (!client) {
const apiKey = process.env.DEEPGRAM_API_KEY;
if (!apiKey) {
throw new Error('DEEPGRAM_API_KEY environment variable not set');
}
client = createClient(apiKey);
}
return client;
}
export function resetClient(): void {
client = null;
}
Typed Transcription Response
// types/deepgram.ts
export interface TranscriptWord {
word: string;
start: number;
end: number;
confidence: number;
punctuated_word?: string;
}
export interface TranscriptAlternative {
transcript: string;
confidence: number;
words: TranscriptWord[];
}
export interface TranscriptChannel {
alternatives: TranscriptAlternative[];
}
export interface TranscriptResult {
results: {
channels: TranscriptChannel[];
utterances?: Array<{
start: number;
end: number;
transcript: string;
speaker: number;
}>;
};
metadata: {
request_id: string;
model_uuid: string;
model_info: Record<string, unknown>;
};
}
Error Handling Wrapper
// lib/transcribe.ts
import { getDeepgramClient } from './deepgram';
import { TranscriptResult } from '../types/deepgram';
export class TranscriptionError extends Error {
constructor(
message: string,
public readonly code: string,
public readonly requestId?: string
) {
super(message);
this.name = 'TranscriptionError';
}
}
export async function transcribeUrl(
url: string,
options: { model?: string; language?: string } = {}
): Promise<TranscriptResult> {
const client = getDeepgramClient();
try {
const { result, error } = await client.listen.prerecorded.transcribeUrl(
{ url },
{
model: options.model || 'nova-2',
language: options.language || 'en',
smart_format: true,
punctuate: true,
}
);
if (error) {
throw new TranscriptionError(
error.message || 'Transcription failed',
error.code || 'UNKNOWN_ERROR'
);
}
return result as TranscriptResult;
} catch (err) {
if (err instanceof TranscriptionError) throw err;
throw new TranscriptionError(
err instanceof Error ? err.message : 'Unknown error',
'NETWORK_ERROR'
);
}
}
Retry with Exponential Backoff
// lib/retry.ts
interface RetryOptions {
maxRetries?: number;
baseDelay?: number;
maxDelay?: number;
}
export async function withRetry<T>(
fn: () => Promise<T>,
options: RetryOptions = {}
): Promise<T> {
const { maxRetries = 3, baseDelay = 1000, maxDelay = 10000 } = options;
let lastError: Error | undefined;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
// Don't retry on auth errors
if (lastError.message.includes('401') ||
lastError.message.includes('403')) {
throw lastError;
}
if (attempt < maxRetries) {
const delay = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
throw lastError;
}
// Usage
const result = await withRetry(() => transcribeUrl(audioUrl));
Python Patterns
# lib/deepgram_client.py
from deepgram import DeepgramClient, PrerecordedOptions
from functools import lru_cache
import os
@lru_cache(maxsize=1)
def get_deepgram_client() -> DeepgramClient:
"""Get or create Deepgram client singleton."""
api_key = os.environ.get('DEEPGRAM_API_KEY')
if not api_key:
raise ValueError('DEEPGRAM_API_KEY environment variable not set')
return DeepgramClient(api_key)
def transcribe_url(url: str, model: str = 'nova-2') -> dict:
"""Transcribe audio from URL with error handling."""
client = get_deepgram_client()
options = PrerecordedOptions(
model=model,
smart_format=True,
punctuate=True,
)
try:
response = client.listen.rest.v("1").transcribe_url(
{"url": url},
options
)
return response.to_dict()
except Exception as e:
raise TranscriptionError(str(e)) from e
Resources
Next Steps
Proceed to deepgram-core-workflow-a for speech-to-text workflow implementation.
04 同仓库更多 Skills
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.
web-artifacts-builder
Suite of tools for creating elaborate, multi-component claude.ai HTML artifacts using modern frontend web technologies (React, Tailwind CSS, shadcn/ui). Use for complex artifacts requiring state management, routing, or shadcn/ui components - not for simple single-file HTML/JSX artifacts.
webapp-testing
Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.
neuropixels-analysis
Neuropixels neural recording analysis. Load SpikeGLX/OpenEphys data, preprocess, motion correction, Kilosort4 spike sorting, quality metrics, Allen/IBL curation, AI-assisted visual analysis, for Neuropixels 1.0/2.0 extracellular electrophysiology. Use when working with neural recordings, spike sorting, extracellular electrophysiology, or when the user mentions Neuropixels, SpikeGLX, Open Ephys, Kilosort, quality metrics, or unit curation.
skill-creator
Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.
notebooklm
Use this skill to query your Google NotebookLM notebooks directly from Claude Code for source-grounded, citation-backed answers from Gemini. Browser automation, library management, persistent auth. Drastically reduced hallucinations through document-only responses.