Metadata-Version: 2.5
Name: langfuse-voice
Version: 0.1.0
Summary: Voice-call observability for Langfuse: map realtime voice agent sessions to structured traces
Author: zndqs
License: MIT
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: langfuse>=3.0.0
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Description-Content-Type: text/markdown

# langfuse-voice

Voice-call observability for [Langfuse](https://langfuse.com). Maps realtime
voice agent sessions (OpenAI Realtime API first) onto structured Langfuse
traces — per-turn observations with real timestamps, barge-in accounting,
async-transcript reordering, and a conversation view that Langfuse's chat
renderer actually renders.

> **Status: early.** Extracted from a production Twilio + OpenAI Realtime
> deployment; being generalized in the open.

```bash
pip install langfuse-voice
```

## Why

Langfuse has no native concept of a voice call
([#4757](https://github.com/orgs/langfuse/discussions/4757),
[#3765](https://github.com/orgs/langfuse/discussions/3765)). Teams wire it up
by hand and hit the same walls: the whole call crammed into one observation
written at hangup (trace latency reads 0.00s, a mid-call crash loses
everything), interruptions with nowhere to live, user transcripts arriving
out of order, no audio/text cost split.

This library is that hand-wiring done once, properly:

- **trace = call, child observation = turn**, opened/closed live as events
  arrive — real latencies, crash-resilient
- **Barge-in, twice**: strikethrough in the rendered conversation for humans,
  exact `heard_ms / total_ms` in metadata for queries
- **Placeholder/backfill** for asynchronous user transcripts, so the timeline
  reads as the actual back-and-forth
- **Transport-agnostic**: how much audio the caller actually heard is a
  transport-layer fact; the core just accepts `report_playback()` (Twilio
  mark-based tracking lives in `examples/`, not in core)
- **Per-turn audio**: `attach_audio(item_id, wav)` puts a playable clip on the
  turn's observation (stdlib PCM16/mu-law→WAV helpers included); audio arriving
  after the turn closed lands under trace `metadata.late_audio`. Requires the
  server's S3 media upload to be configured with an endpoint reachable from
  both the uploader and the browser, and `client.shutdown()` before exit
  (media uploads run on a background thread that `flush()` does not await)

## Usage sketch

```python
from langfuse import get_client
from langfuse_voice import LangfuseVoiceTracer, CallStarted, CallEnded, ToolCallCompleted
from langfuse_voice.sources import OpenAIRealtimeSource

tracer = LangfuseVoiceTracer(get_client())
source = OpenAIRealtimeSource()

tracer.ingest(CallStarted(call_id=call_sid, model=model, instructions=prompt, tools=tools))

async for raw in realtime_ws:          # your existing WebSocket loop
    for event in source.translate(raw):
        tracer.ingest(event)
    ...                                 # your audio/tool handling, unchanged

tracer.ingest(ToolCallCompleted(call_id=cid, result=result))  # app-side facts
tracer.ingest(CallEnded(reason="caller_hangup"))
```

## Field notes (things no docs will tell you)

Empirically verified against Langfuse v3.225:

1. Chat-view message objects may only carry `role` / `content` /
   `tool_calls` / `tool_call_id`. One extra key and the whole conversation
   falls back to raw JSON.
2. Tool definitions placed in `input` also kill the chat view — put them in
   `metadata`.
3. On OpenAI Realtime, the assistant transcript is a free by-product of audio
   generation (whole sentences); the *user* transcript runs on a separate
   per-minute-billed model and arrives asynchronously — treat them as
   different things or your timeline lies.

## Roadmap

See [doc/design.md](doc/design.md): usage/cost split (audio vs text tokens,
transcription minutes) → per-turn audio attachments → Gemini Live source.

## License

MIT
