Speechmatics Batch API Client
Project description
Speechmatics Batch API Client
Async Python client for Speechmatics Batch API.
Features
- Async API client with comprehensive error handling
- Type hints throughout for better IDE support
- Environment variable support for credentials
- Easy-to-use interface for submitting, monitoring, and retrieving transcription jobs
- Full job configuration support with all Speechmatics features
- Intelligent transcript formatting with speaker diarization
- Support for multiple output formats (JSON, TXT, SRT)
Installation
pip install speechmatics-batch
Usage
Quick Start
import asyncio
from speechmatics.batch import AsyncClient
async def main():
# Create a client using environment variable SPEECHMATICS_API_KEY
async with AsyncClient() as client:
# Simple transcription
result = await client.transcribe("audio.wav")
print(result.transcript_text)
asyncio.run(main())
JWT Authentication
For enhanced security, use temporary JWT tokens instead of static API keys. JWTs are short-lived (60 seconds default) and automatically refreshed:
from speechmatics.batch import AsyncClient, JWTAuth
auth = JWTAuth("your-api-key", ttl=60)
async with AsyncClient(auth=auth) as client:
# Tokens are cached and auto-refreshed automatically
result = await client.transcribe("audio.wav")
print(result.transcript_text)
Ideal for long-running applications or when minimizing API key exposure. See the authentication documentation for more details.
Basic Job Workflow
import asyncio
from speechmatics.batch import AsyncClient, JobConfig, JobType, TranscriptionConfig
async def main():
# Create client with explicit API key
async with AsyncClient(api_key="your-api-key") as client:
# Configure transcription
config = JobConfig(
type=JobType.TRANSCRIPTION,
transcription_config=TranscriptionConfig(
language="en",
enable_entities=True,
diarization="speaker"
)
)
# Submit job
job = await client.submit_job("audio.wav", config=config)
print(f"Job submitted: {job.id}")
# Wait for completion
result = await client.wait_for_completion(
job.id,
polling_interval=2.0,
timeout=300.0
)
# Access results
print(f"Transcript: {result.transcript_text}")
print(f"Confidence: {result.confidence}")
asyncio.run(main())
Advanced Configuration
import asyncio
from speechmatics.batch import (
AsyncClient,
JobConfig,
JobType,
OperatingPoint,
TranscriptionConfig,
TranslationConfig,
SummarizationConfig
)
async def main():
async with AsyncClient(api_key="your-api-key") as client:
# Advanced job configuration
config = JobConfig(
type=JobType.TRANSCRIPTION,
transcription_config=TranscriptionConfig(
language="en",
operating_point=OperatingPoint.ENHANCED,
enable_entities=True,
diarization="speaker",
),
translation_config=TranslationConfig(target_languages=["es", "fr"]),
summarization_config=SummarizationConfig(
content_type="conversational", summary_length="brief"
),
)
result = await client.transcribe("audio.wav", config=config)
# Access advanced features
if result.summary:
print(f"Summary: {result.summary}")
if result.translations:
print(f"Translations: {result.translations}")
asyncio.run(main())
Manual Job Management
import asyncio
from speechmatics.batch import AsyncClient, JobStatus
async def main():
async with AsyncClient() as client:
# Submit job
job = await client.submit_job("audio.wav")
# Check job status
job_details = await client.get_job_info(job.id)
print(f"Status: {job_details.status}")
# Wait for completion manually
while job_details.status == JobStatus.RUNNING:
await asyncio.sleep(5)
job_details = await client.get_job_info(job.id)
if job_details.status == JobStatus.DONE:
# Get transcript
transcript = await client.get_transcript(job.id)
print(transcript.transcript_text)
else:
print(f"Job failed with status: {job_details.status}")
asyncio.run(main())
Different Output Formats
import asyncio
from speechmatics.batch import AsyncClient, FormatType
async def main():
async with AsyncClient() as client:
job = await client.submit_job("audio.wav")
# Get JSON format (default)
json_result = await client.get_transcript(job.id, format_type=FormatType.JSON)
print(json_result.transcript_text)
# Get plain text
txt_result = await client.get_transcript(job.id, format_type=FormatType.TXT)
print(txt_result)
# Get SRT subtitles
srt_result = await client.get_transcript(job.id, format_type=FormatType.SRT)
print(srt_result)
asyncio.run(main())
Error Handling
import asyncio
from speechmatics.batch import (
AsyncClient,
BatchError,
AuthenticationError,
JobError,
TimeoutError
)
async def main():
try:
async with AsyncClient() as client:
result = await client.transcribe("audio.wav", timeout=120.0)
print(result.transcript_text)
except AuthenticationError:
print("Invalid API key")
except BatchError as e:
print(f"Job submission failed: {e}")
except JobError as e:
print(f"Job processing failed: {e}")
except TimeoutError as e:
print(f"Job timed out: {e}")
except FileNotFoundError:
print("Audio file not found")
asyncio.run(main())
Connection Configuration
import asyncio
from speechmatics.batch import AsyncClient, ConnectionConfig
async def main():
# Custom connection settings
config = ConnectionConfig(
url="https://asr.api.speechmatics.com/v2",
api_key="your-api-key",
connect_timeout=30.0,
operation_timeout=600.0
)
async with AsyncClient(conn_config=config) as client:
result = await client.transcribe("audio.wav")
print(result.transcript_text)
asyncio.run(main())
Logging
The client supports logging with job id tracing for debugging. To increase logging verbosity, set DEBUG level in your example code:
import logging
import sys
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
Environment Variables
The client supports the following environment variables:
SPEECHMATICS_API_KEY: Your Speechmatics API keySPEECHMATICS_BATCH_URL: Custom API endpoint URL (optional)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file speechmatics_batch-0.4.7.tar.gz.
File metadata
- Download URL: speechmatics_batch-0.4.7.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e3278166af4de0888b2ff83abb29a8a650766f20708c1792e283dc5cd2e2962
|
|
| MD5 |
7ae4c9ee8f9f5e54b8f0988292f7a221
|
|
| BLAKE2b-256 |
2e9a93c4747f467590cb70df6fd6984dcccc492c2156183f3ef5a201d89a3349
|
File details
Details for the file speechmatics_batch-0.4.7-py3-none-any.whl.
File metadata
- Download URL: speechmatics_batch-0.4.7-py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18757f29d6348f8dcc4fbc60784b316350c013fbdf607c251e9cfa4fc92c29fa
|
|
| MD5 |
e6e53478ac2fd108ef09c48eada30da1
|
|
| BLAKE2b-256 |
310a08d674ba5db0111543014c614e69b2af591f7b83c5026747979fedb90a6a
|