Skip to main content

Voice2Voice Python SDK

🌐 Official Website: Voice2Voice Translator

Try the Voice2Voice Translator web application to convert speech between languages.

Python versions License: MIT

A Python SDK for the Voice2Voice Translator API. It provides synchronous and asynchronous clients for discovering supported languages, translating audio, downloading generated audio, checking visitor quotas, and managing administrator quota settings.

Release status: 0.1.1 — early release. The SDK interface and server API may evolve.

Features

  • Synchronous client: Voice2VoiceClient
  • Asynchronous client: AsyncVoice2VoiceClient
  • Audio upload and translation
  • Download translated audio
  • Retrieve supported languages and visitor quota
  • Optional administrator operations using a bearer token
  • Typed result models and specific API exceptions
  • Configurable request timeout
  • Reusable HTTPX clients and context-manager support
  • Persistent anonymous visitor ID when the server issues the vt_visitor_id cookie

Requirements

  • Python 3.9 or newer
  • Access to a running Voice2Voice API server
  • httpx (installed automatically)

Installation

python -m pip install voice2voice

Quick start

Synchronous client

from voice2voice import Voice2VoiceClient, APIError, QuotaExceededError

with Voice2VoiceClient() as client:
    languages = client.languages()
    print([(language.code, language.name) for language in languages])

    print("Quota:", client.quota())

    result = client.translate(
        file="sample.mp3",
        source_language="en",
        target_language="es",
        output_format="mp3",
    )

    print("Recognized:", result.recognized_text)
    print("Cleaned:", result.cleaned_text)
    print("Translated:", result.translated_text)

    audio = client.download_audio(result)
    with open(audio.filename, "wb") as output_file:
        output_file.write(audio.content)

Handle API-specific errors when needed:

from voice2voice import Voice2VoiceClient, APIError, QuotaExceededError

try:
    with Voice2VoiceClient() as client:
        result = client.translate(
            file="sample.mp3",
            source_language="en",
            target_language="es",
        )
except QuotaExceededError as exc:
    print("Quota exceeded:", exc.message)
    print("Remaining seconds:", exc.remaining_seconds)
except APIError as exc:
    print(f"API request failed ({exc.status_code}): {exc.message}")

Asynchronous client

Use async with to close the asynchronous HTTPX client reliably:

import asyncio
from voice2voice import AsyncVoice2VoiceClient

async def main():
    async with AsyncVoice2VoiceClient() as client:
        languages = await client.languages()
        print([(language.code, language.name) for language in languages])

        result = await client.translate(
            file="sample.mp3",
            source_language="en",
            target_language="es",
            output_format="mp3",
        )

        print("Translated:", result.translated_text)

        audio = await client.download_audio(result)
        with open(audio.filename, "wb") as output_file:
            output_file.write(audio.content)

asyncio.run(main())

Configuration

Both clients accept the following configuration options:

from voice2voice import Voice2VoiceClient

client = Voice2VoiceClient(
    base_url="https://your-api.example.com",
    timeout=120.0,
)
Option Description
base_url Base URL of the Voice2Voice API. Defaults to the hosted API configured by the SDK.
visitor_id Optional previously obtained anonymous visitor ID.
admin_token Bearer token used by administrator methods.
timeout HTTPX request timeout in seconds. Set to None to disable the timeout.
http_client Optional existing httpx.Client. The async client accepts an httpx.AsyncClient.

When an external HTTPX client is injected, the SDK does not close it. The caller remains responsible for managing its lifecycle. Requests use the SDK's configured base_url, independently of the injected HTTPX client's base_url.

Example with an externally managed HTTPX client:

import httpx
from voice2voice import Voice2VoiceClient

transport = httpx.Client(timeout=45.0)

try:
    with Voice2VoiceClient(
        base_url="https://your-api.example.com",
        http_client=transport,
    ) as client:
        print(client.languages())
finally:
    transport.close()

Anonymous visitor identity

The API server may issue a vt_visitor_id cookie to identify an anonymous visitor and track that visitor's quota. The SDK exposes the current value:

visitor_id = client.visitor_id

When the server issues this cookie, the SDK attempts to persist it at:

~/.voice2voice/visitor.json

The SDK continues working if the file cannot be read or written. You can also pass visitor_id=... explicitly to restore an identity.

Treat visitor IDs as private. Do not publish them, commit them to source control, or expose them in logs.

Administrator operations

Administrator methods require a valid server-issued admin token:

from voice2voice import Voice2VoiceClient

with Voice2VoiceClient(admin_token="YOUR_ADMIN_TOKEN") as client:
    current = client.admin_quota()
    print(current)

    updated = client.update_admin_quota(
        quota_seconds=180,
        period_type="daily",
    )
    print(updated)

Keep the token in an environment variable or secret manager rather than hard-coding it.

The async client provides the equivalent methods:

current = await client.admin_quota()

updated = await client.update_admin_quota(
    quota_seconds=180,
    period_type="daily",
)

The accepted period_type values and quota-update semantics are defined by the API server. Check the server's current documentation before changing administrator settings.

API methods

Method Description
languages() Return the languages advertised by the API.
translate(file, source_language, target_language, output_format="mp3") Upload audio and return translation data.
quota() Retrieve the current visitor's quota information.
download_audio(result_or_url) Download generated audio and return an AudioDownload.
admin_quota() Retrieve administrator quota settings.
update_admin_quota(quota_seconds, period_type, custom_period_seconds=86400) Update administrator quota settings.

For AsyncVoice2VoiceClient, use await with each method. It also provides aclose() and asynchronous context-manager support.

Language codes, supported audio formats, upload limits, and quota behavior are determined by the API server. Retrieve the current language list with client.languages() and consult the server's current API documentation for accepted formats and limits.

Return models

  • Language: name, code
  • TranslationResult: recognized_text, cleaned_text, translated_text, audio_url, audio_filename, and the original response dictionary in raw
  • AudioDownload: downloaded content (bytes), content_type, and filename

These models are immutable dataclasses.

Error handling

All SDK-specific exceptions inherit from Voice2VoiceError.

Exception Meaning
Voice2VoiceError Base SDK exception.
APIError Unsuccessful HTTP response; includes status_code, message, and detail.
AuthenticationError HTTP 401 or 403 response.
QuotaExceededError HTTP 429 response with the API's quota_exceeded error code.

QuotaExceededError also exposes quota fields when the server includes them: limit_seconds, used_seconds, remaining_seconds, requested_seconds, and reset_at.

Network and file-system errors are not converted into APIError. Handle exceptions such as httpx.TimeoutException, httpx.ConnectError, and OSError where appropriate.

Development

Clone the repository:

git clone https://github.com/ehsanulhaqislamyar-star/voice2voice-python.git
cd voice2voice-python

Install the development dependencies:

python -m pip install -e ".[dev]"

Run the test suite:

python -m pytest

Build the source distribution and wheel:

python -m build

Validate the distributions:

python -m twine check dist/*

License

This project is licensed under the MIT License. See LICENSE for the full license text.

Metadata

Release files for voice2voice 0.1.2

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

Source distribution (sdist)

Source distribution for voice2voice 0.1.2
File Size Uploaded Provenance Actions
voice2voice-0.1.2.tar.gz 8.7 kB Without provenance Details for voice2voice-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for voice2voice 0.1.2
File Interpreter ABI Platform Actions
voice2voice-0.1.2-py3-none-any.whl Python 3 none any Details for voice2voice-0.1.2-py3-none-any.whl

Total release size: 19.9 kB

Release files / voice2voice-0.1.2.tar.gz

Download URL voice2voice-0.1.2.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bb2837dd1681010e50a2be9572f998d378f928b52078c693b4395ab8e247dccd
BLAKE2b-256 checksum
How to use checksums
94aedc943a1b45404a2e6fc893cdb7ed96bfc16aeabd3759bbae6d2ab1891076
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / voice2voice-0.1.2-py3-none-any.whl

Download URL voice2voice-0.1.2-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cbb7ccbc1163c5708fe219368eb60a3df91f248797160da2486d389c19987348
BLAKE2b-256 checksum
How to use checksums
35f6d8f422ae5d9a6299bb982089e9502e20c3f7b25de283a4ab9d1d1287e994
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Security for voice2voice 0.1.2

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.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No 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