Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Run llama Llama index BaseVoiceAgentWebsocket

From Leeroopedia

Overview

The BaseVoiceAgentWebsocket class is an abstract base class that defines the contract for websocket connections used by voice agents. It provides the basic structure for connecting to a voice service via websockets, sending data, and closing the connection. The class uses the websockets library's ClientConnection type for the underlying connection.

Source File: llama-index-core/llama_index/core/voice_agents/websocket.py

Module: llama_index.core.voice_agents.websocket

Lines of Code: 78

Dependencies

Dependency Type Purpose
abc.ABC Standard Library Abstract base class support
abc.abstractmethod Standard Library Decorator for abstract methods
typing Standard Library Type annotations (Optional, Any, TYPE_CHECKING)
websockets.asyncio.client.ClientConnection External (conditional) Websocket client connection type (imported only during type checking)

The websockets library import is guarded by TYPE_CHECKING, meaning it is only resolved during static analysis and not at runtime. This avoids a hard dependency on the websockets package at import time.

Class: BaseVoiceAgentWebsocket

class BaseVoiceAgentWebsocket(ABC)

Constructor

def __init__(self, uri: str)
Parameter Type Description
uri str URL of the websocket endpoint

Instance Attributes

Attribute Type Initial Value Description
uri str (from parameter) The websocket URL
ws Optional[ClientConnection] None The websocket client connection, initially unconnected

Methods

connect (Synchronous)

def connect(self) -> None

A stub method for synchronous websocket connection. The method body is empty (no implementation), serving as a placeholder. Voice agents typically use the asynchronous aconnect method instead.

aconnect (Asynchronous)

async def aconnect(self) -> None

Asynchronous connection method. The docstring specifies the expected implementation pattern:

self.ws = await websockets.connect(uri=self.uri)

This method is not marked as @abstractmethod, meaning subclasses may override it but are not required to. The base implementation is empty (no-op).

send (Abstract)

@abstractmethod
async def send(self, data: Any) -> None

Sends data to the websocket. This is an abstract method that must be implemented by all subclasses.

Parameter Type Description
data Any Data to send to the websocket (typically bytes for audio or JSON strings for control messages)

close (Abstract)

@abstractmethod
async def close(self) -> None

Closes the websocket connection. This is an abstract method that must be implemented by all subclasses. Implementations should handle graceful disconnection and resource cleanup.

Method Summary

Method Async Abstract Description
__init__ No No Initialize with URI, set ws to None
connect No No Synchronous connection stub (empty body)
aconnect Yes No Asynchronous connection (empty body, to be overridden)
send Yes Yes Send data to the websocket
close Yes Yes Close the websocket connection

Design Patterns

Lazy Connection

The websocket connection (self.ws) is initialized as None and is only populated when aconnect (or connect) is called. This allows the websocket object to be instantiated and configured before the actual connection is established.

Conditional Import

The websockets library is imported under TYPE_CHECKING only:

if TYPE_CHECKING:
    from websockets.asyncio.client import ClientConnection

This pattern ensures that:

  • Type checkers and IDEs can resolve the ClientConnection type.
  • The module does not fail at import time if websockets is not installed.
  • The runtime dependency is deferred to actual usage.

Minimal Abstract Surface

Only send and close are abstract methods. The connection methods (connect and aconnect) are provided as non-abstract stubs, giving subclasses the flexibility to override them without being forced to. This design recognizes that connection logic may vary significantly between voice service providers.

Async-First Design

The primary methods (aconnect, send, close) are all asynchronous, reflecting the inherently async nature of websocket communication. The synchronous connect method exists as a convenience stub but is not the primary path.

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment