Skip to content

[Enhancement] Add voice agent middleware pipeline for custom interceptors and hooks #796

Description

@deepgram-robot

Summary

Add a middleware/interceptor pipeline for the Voice Agent client that lets developers inject custom logic at key points in the conversation lifecycle — before/after STT results, before/after LLM processing, before/after TTS output, and around function calls — without modifying the SDK internals.

Problem it solves

Developers building production voice agents need to add cross-cutting concerns — logging, PII detection, compliance checks, metric collection, response filtering, A/B testing — but the current event-based API requires writing this logic inline in event handlers. A middleware pipeline provides a composable, reusable pattern for layering concerns, similar to ASGI/WSGI middleware in web frameworks that Python developers already understand.

Proposed API

from deepgram import DeepgramClient, AgentMiddleware

class PIIRedactionMiddleware(AgentMiddleware):
    async def on_transcript(self, transcript, next):
        redacted = self.redact_pii(transcript)
        return await next(redacted)

    async def on_agent_response(self, response, next):
        if self.contains_pii(response):
            response = self.redact_pii(response)
        return await next(response)

class MetricsMiddleware(AgentMiddleware):
    async def on_transcript(self, transcript, next):
        start = time.monotonic()
        result = await next(transcript)
        self.record_latency("stt", time.monotonic() - start)
        return result

client = DeepgramClient()
agent = client.agent.v1.websocket(
    middleware=[PIIRedactionMiddleware(), MetricsMiddleware()]
)

Acceptance criteria

  • Middleware base class with hooks for each conversation lifecycle stage
  • Middleware chain executes in order, each calling next to proceed
  • Middleware can modify, filter, or reject messages
  • Zero overhead when no middleware is configured
  • Documented with usage example
  • Compatible with existing event-based API (middleware runs before event handlers)

Raised by the DX intelligence system.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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