Skip to content

[Enhancement] Add FastAPI WebSocket middleware for deploying voice agents as API endpoints #754

Description

@deepgram-robot

Summary

Add a FastAPI-compatible WebSocket middleware that wraps Deepgram's Voice Agent API, enabling developers to deploy voice agents as standard API endpoints with minimal boilerplate. The middleware should handle WebSocket upgrade, audio proxying, session lifecycle, and error handling.

Problem it solves

Developers building voice agent backends with FastAPI currently write 50-100 lines of WebSocket handling code for each endpoint — managing the Deepgram connection lifecycle, audio proxying, and error recovery manually. A middleware abstraction reduces this to a few lines of configuration while following FastAPI's dependency injection patterns.

Proposed API

from deepgram.contrib.fastapi import DeepgramAgentMiddleware, AgentConfig

agent_config = AgentConfig(
    listen={"model": "nova-3"},
    think={"provider": {"type": "open_ai"}, "model": "gpt-4o-mini"},
    speak={"model": "aura-asteria-en"},
    tools=[lookup_order, create_ticket],
)

app = FastAPI()

@app.websocket("/agent")
async def agent_endpoint(websocket: WebSocket):
    async with DeepgramAgentMiddleware(websocket, config=agent_config) as session:
        await session.run()  # Handles audio proxy, events, tool execution

Acceptance criteria

  • Works with FastAPI's WebSocket support and dependency injection
  • Handles connection lifecycle (open, close, error, reconnect)
  • Proxies audio between browser WebSocket and Deepgram Agent API
  • Supports tool registration with typed callbacks
  • Includes health check endpoint helper
  • Documented with usage example
  • Compatible with existing API

Raised by the DX intelligence system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions