Skip to main content

voicerun_completions

A comprehensive, unified SDK for interacting with multiple LLM providers (OpenAI, Anthropic, Google, and Vertex Anthropic) through a single, consistent API. This library simplifies working with different providers by normalizing their APIs and providing powerful features like automatic fallbacks, retry logic, and advanced streaming options.

Quick Start

from primfunctions.events import Event, StartEvent, TextEvent, TextToSpeechEvent
from primfunctions.context import Context
from voicerun_completions import generate_chat_completion

async def handler(event: Event, context: Context):
    if isinstance(event, StartEvent):
        yield TextToSpeechEvent(
            text="Hello! Ask me anything and I'll answer your question.",
            voice="nova"
        )

    if isinstance(event, TextEvent):
        user_message = event.data.get("text", "")
        
        response = await generate_chat_completion({
            "provider": "openai",
            "api_key": context.variables.get("OPENAI_API_KEY"),
            "model": "gpt-5-mini",
            "messages": [
                {"role": "user", "content": user_message}
            ]
        })
        
        if response.message.content:
            yield TextToSpeechEvent(
                text=response.message.content,
                voice="nova"
            )

Connection Reuse with CompletionsClient

Use CompletionsClient to reuse HTTP connections across requests. This avoids the TLS handshake overhead of creating a new SDK client on every call — important for voice agents where latency matters.

In a VoiceRun Agent

Create the client at module level so it persists across handler invocations. The agent handler is called per-event, but the module-level client lives for the lifetime of the process, keeping connections warm.

from primfunctions.events import Event, StartEvent, TextEvent, TextToSpeechEvent
from primfunctions.context import Context
from voicerun_completions import CompletionsClient, deserialize_conversation, UserMessage

# Module-level client — connections are reused across all handler calls
completions = CompletionsClient()

async def handler(event: Event, context: Context):
    if isinstance(event, StartEvent):
        yield TextToSpeechEvent(text="Hello! Ask me anything.", voice="nova")

    if isinstance(event, TextEvent):
        user_message = event.data.get("text", "N/A")

        messages = deserialize_conversation(context.get_completion_messages())
        messages.append(UserMessage(content=user_message))

        stream = await completions.generate_chat_completion_stream(
            request={
                "provider": "anthropic",
                "api_key": context.variables.get("ANTHROPIC_API_KEY"),
                "model": "claude-haiku-4-5",
                "messages": messages,
            },
            stream_options={"stream_sentences": True, "clean_sentences": True},
        )

        async for chunk in stream:
            if chunk.type == "content_sentence":
                yield TextToSpeechEvent(text=chunk.sentence, voice="nova")
            elif chunk.type == "response":
                messages.append(chunk.response.message)
                context.set_completion_messages(messages)

In Other Applications

For servers or scripts where you control the lifecycle, you can also use the context manager:

from voicerun_completions import CompletionsClient

async with CompletionsClient() as client:
    response = await client.generate_chat_completion({...})
    stream = await client.generate_chat_completion_stream({...})
# All SDK connections closed automatically

The module-level generate_chat_completion() and generate_chat_completion_stream() functions still work as before without connection reuse.

Documentation

Testing

Install dev dependencies:

uv pip install -e ".[dev]"

Run unit tests (no API keys required):

uv run pytest -m "not integration" -v

Run all tests including integration (requires API keys in environment):

uv run pytest -v

Run a specific test file:

uv run pytest tests/test_google_schema_sanitization.py -v

Integration tests require one or more of these environment variables:

  • OPENAI_API_KEY
  • ANTHROPIC_API_KEY
  • GEMINI_API_KEY
  • GCP_SERVICE_ACCOUNT_JSON / GCP_PROJECT_ID (for Vertex tests)

Tests missing the required key will be skipped automatically.

Features

  • Unified API - Single interface for OpenAI, Anthropic, Google, and Vertex Anthropic
  • Automatic Fallbacks - Seamlessly fallback to alternative providers on failure
  • Retry Logic - Built-in exponential backoff retry mechanism
  • Advanced Streaming - Token-based and sentence-based streaming for real-time applications
  • Tool Calling - Unified tool/function calling across all providers
  • Type Safety - Full type hints and support for both dict and object formats

Git Worktrees (Parallel Agent Development)

This repo supports running multiple coding agents in parallel using git worktrees. Each agent gets an isolated working directory with its own branch while sharing the same git object store.

Worktrees are stored in .worktrees/ (gitignored).

Create a worktree

git worktree add .worktrees/feature-xyz -b feature-xyz

List active worktrees

git worktree list

Remove a worktree

git worktree remove .worktrees/feature-xyz

Clean up stale worktrees

git worktree prune

Metadata

Release files for voicerun_completions 0.4.0

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

Source distribution (sdist)

Source distribution for voicerun_completions 0.4.0
File Size Uploaded Provenance Actions
voicerun_completions-0.4.0.tar.gz 74.6 kB Without provenance Details for voicerun_completions-0.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 128.3 kB

Release files / voicerun_completions-0.4.0.tar.gz

Download URL voicerun_completions-0.4.0.tar.gz
Size 74.6 kB
Tags Source
SHA-256 checksum
How to use checksums
de9b3120608b71f25944f890a72faa2fe6104a7a1c0199e41bccf84af6aec7b6
BLAKE2b-256 checksum
How to use checksums
d7bdb4ee4f3dfedbaf81a7ef16d44050885ca6583080a0e5b1cef1103b63bed8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.12

Release files / voicerun_completions-0.4.0-py3-none-any.whl

Download URL voicerun_completions-0.4.0-py3-none-any.whl
Size 53.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3db382e5fa3661b907edd601419ca26e336bd4c0a7cdf390465ebf82d559ca2
BLAKE2b-256 checksum
How to use checksums
91186ebc79300bc95cefdf8a997aeb31c5371154b3a64847538adec9dd2bfe7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.12

Security for voicerun_completions 0.4.0

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.

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