Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions py/src/braintrust/integrations/pipecat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,16 @@ def setup_pipecat(
capture_agent_audio_attachments: bool | None = None,
audio_recording_options: AudioRecordingOptions | None = None,
) -> bool:
"""Set up Braintrust tracing for Pipecat ``PipelineWorker`` instances.
"""Enable Braintrust tracing for newly created Pipecat ``PipelineWorker`` instances.

Setup instruments ``PipelineWorker`` so every newly constructed worker is
traced, the same as ``braintrust.auto_instrument()`` or
``wrap_pipeline_worker()``. Pass ``capture_audio_attachments=True`` to attach
both user and agent audio, or configure them independently with
Set ``capture_audio_attachments=True`` to record both sides, or use
``capture_user_audio_attachments`` and ``capture_agent_audio_attachments``.
When arguments are omitted, the generic
environment variables ``BRAINTRUST_CAPTURE_USER_AUDIO_ATTACHMENTS`` and
``BRAINTRUST_CAPTURE_AGENT_AUDIO_ATTACHMENTS`` apply. Explicit arguments
take precedence.

Emits the audio v3 schema: a ``session`` with shared recordings, native
``user_turn`` / ``agent_turn`` groups, speech, STT, LLM, tools and TTS.
Turn containers follow Pipecat's native boundaries. Agent recordings
contain transport-confirmed playback.
``audio_recording_options`` configures segmentation, encoding and shutdown
using ``braintrust.audio.AudioRecordingOptions``. Recordings default
to Opus/Ogg when PyAV with libopus is available, otherwise raw PCM.
``braintrust[audio]`` installs a pinned PyAV on Python 3.12+.
Omitted capture options use ``BRAINTRUST_CAPTURE_USER_AUDIO_ATTACHMENTS``
and ``BRAINTRUST_CAPTURE_AGENT_AUDIO_ATTACHMENTS`` environment defaults.
Explicit arguments take precedence.

Use ``braintrust.audio.AudioRecordingOptions`` with ``audio_recording_options``
to configure recording format, segmentation, and shutdown.
"""
set_default_observer_options(
capture_audio_attachments=capture_audio_attachments,
Expand Down

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions py/src/braintrust/integrations/pipecat/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .patchers import (
LLMServicePushFramePatcher,
OpenAILivePatcher,
PipelineWorkerPatcher,
SegmentedSTTPushSegmentFramesPatcher,
STTServiceProcessGeneratorPatcher,
Expand All @@ -22,4 +23,5 @@ class PipecatIntegration(BaseIntegration):
LLMServicePushFramePatcher,
STTServiceProcessGeneratorPatcher,
SegmentedSTTPushSegmentFramesPatcher,
OpenAILivePatcher,
)
67 changes: 64 additions & 3 deletions py/src/braintrust/integrations/pipecat/patchers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Pipecat integration patchers."""

import inspect
from contextvars import ContextVar
from typing import Any

from braintrust._audio.config import AudioRecordingOptions
from braintrust.integrations.base import CompositeFunctionWrapperPatcher, FunctionWrapperPatcher

from .tracing import BraintrustPipecatObserver
from .tracing._live import is_live_service


_DEFAULT_OBSERVER_OPTIONS: dict[str, Any] = {
Expand Down Expand Up @@ -74,14 +76,18 @@ def _release_response_span(service: Any) -> None:
async def traced_llm_push_frame(wrapped: Any, instance: Any, args: tuple[Any, ...], kwargs: dict[str, Any]) -> Any:
"""Make an LLM service's llm_response span current while it makes the call.

Every LLM service pushes its response start frame, sends the provider
request and pushes its response end frame from the same task. Spans the
Cascade LLM services push their response start frame, send the provider
request and push their response end frame from the same task. Spans the
provider integration starts in between nest under llm_response.
"""
observer = _ACTIVE_OBSERVER.get()
frame = args[0] if args else kwargs.get("frame")
kind = type(frame).__name__
if observer is None or kind not in ("LLMFullResponseStartFrame", "LLMFullResponseEndFrame"):
if (
observer is None
or kind not in ("LLMFullResponseStartFrame", "LLMFullResponseEndFrame")
or is_live_service(instance)
):
return await wrapped(*args, **kwargs)
_release_response_span(instance)
if kind == "LLMFullResponseEndFrame":
Expand Down Expand Up @@ -166,6 +172,58 @@ class PipelineWorkerPatcher(CompositeFunctionWrapperPatcher):
sub_patchers = (PipelineWorkerInitPatcher, PipelineWorkerRunPatcher)


async def traced_live_event(wrapped: Any, instance: Any, args: tuple[Any, ...], kwargs: dict[str, Any]) -> Any:
observer = _ACTIVE_OBSERVER.get()
event = args[0] if args else kwargs.get("evt")
if observer is not None:
observer._live_event(instance, event)
result = await wrapped(*args, **kwargs)
if observer is not None:
observer._live_event_processed(event)
return result


async def traced_live_client_event(wrapped: Any, instance: Any, args: tuple[Any, ...], kwargs: dict[str, Any]) -> Any:
observer = _ACTIVE_OBSERVER.get()
result = await wrapped(*args, **kwargs)
if observer is not None:
observer._live_client_event(args[0] if args else kwargs.get("event"))
return result


class OpenAILiveEventPatcher(FunctionWrapperPatcher):
name = "openai_live_event"
target_module = "pipecat.services.openai.live.llm"
target_path = "OpenAILiveLLMService._handle_server_event"
wrapper = staticmethod(traced_live_event)


class OpenAILiveClientEventPatcher(FunctionWrapperPatcher):
name = "openai_live_client_event"
target_module = "pipecat.services.openai.live.llm"
target_path = "OpenAILiveLLMService.send_client_event"
wrapper = staticmethod(traced_live_client_event)


class OpenAILivePatcher(CompositeFunctionWrapperPatcher):
name = "openai_live"
sub_patchers = (OpenAILiveEventPatcher, OpenAILiveClientEventPatcher)

@classmethod
def applies(cls, module: Any | None, version: str | None, *, target: Any | None = None) -> bool:
# Both hooks are needed to track a response through tool submission.
# Let the composite own patch markers and idempotency after this gate.
for patcher, argument in ((OpenAILiveEventPatcher, "evt"), (OpenAILiveClientEventPatcher, "event")):
method = patcher.resolve_target(module, version, target=target)
if not inspect.iscoroutinefunction(method):
return False
try:
inspect.signature(method).bind(None, **{argument: None})
except (TypeError, ValueError):
return False
return super().applies(module, version, target=target)


def wrap_pipeline_worker(
PipelineWorker: Any,
*,
Expand Down Expand Up @@ -199,4 +257,7 @@ def wrap_pipeline_worker(
LLMServicePushFramePatcher.wrap_target(LLMService)
STTServiceProcessGeneratorPatcher.wrap_target(STTService)
SegmentedSTTPushSegmentFramesPatcher.wrap_target(SegmentedSTTService)
# Optional on older Pipecat versions; the patchers feature-detect the module.
if OpenAILivePatcher.applies(None, None):
OpenAILivePatcher.patch(None, None)
return PipelineWorkerPatcher.wrap_target(PipelineWorker)
Loading
Loading