hannguyen0712 opened a new pull request, #844:
URL: https://github.com/apache/burr/pull/844
Adds a built-in Langfuse integration (#206): LangfuseBridge, a lifecycle
hook that logs Burr application execution to Langfuse, built on
OpenTelemetryBridge and the OTel-native Langfuse Python SDK.
Closes #206
## Changes
1. New `burr/integrations/langfuse.py` with `LangfuseBridge`, attached via
`.with_hooks(LangfuseBridge())`:
- One trace per application execution call, one span per step, one span
per `__tracer` span
- Action inputs/read state and results/written state captured as
observation input/output (`capture_state=False` to disable)
- `__tracer.log_attributes()` captured as observation metadata (complex
values JSON-serialized, since OTel attributes only accept primitives)
- `app_id` maps to the Langfuse session, `partition_key` to the user
(both overridable); the client is exposed as `bridge.langfuse_client`
- Langfuse SDK v4 only exports LLM-relevant spans by default, so the
bridge injects a `should_export_span` filter (exported as
`burr_span_export_filter` for users constructing their own client), with a
fallback for v3
2. Docs page (`docs/reference/integrations/langfuse.rst`), runnable example
(`examples/integrations/langfuse/`), and a `langfuse` pyproject extra
**Note on the divergence from the tracking-client pattern in the issue:**
the original issue proposed mirroring `burr/tracking/client.py` and depended on
#205 (closed as not planned). Since then, the Langfuse SDK was rebuilt on
OpenTelemetry and registers its span processor on the global tracer provider,
so building on the existing `OpenTelemetryBridge` avoids duplicating
span-lifecycle machinery and nests third-party OTel LLM instrumentation (e.g.
`opentelemetry-instrumentation-openai`) inside step spans for free. This
direction was discussed and approved in the issue thread (May 31). Client
access, which #205 would have provided via runtime inputs, is handled through
constructor injection and the public `bridge.langfuse_client` attribute instead.
Each execution call (`run`, `step`, `iterate`, ...) becomes its own trace;
repeated `.step()` calls therefore produce multiple traces, grouped under one
Langfuse session via `app_id` (`session_id` is overridable if that's not
desired). Async and streaming execution get the same coverage as the existing
OTel bridge, since all execute methods flow through the same hooks.
## How I tested this
- 6 integration tests (`tests/integrations/test_burr_langfuse.py`) run a
real two-action app with a tracer span and logged attributes against an
in-memory OTel exporter and a mocked Langfuse client, so CI needs no
credentials. They assert the full span tree, parenting, session/user
attributes, observation input/output payloads, metadata serialization,
`capture_state=False`, constructor validation, the export filter, and error
status.
- Verified end to end against Langfuse Cloud with
`opentelemetry-instrumentation-openai` installed: screenshots below show the
trace tree with the OpenAI generation (tokens/cost) nested inside the step
spans, observation input/output, the session/user mapping, and error status on
a failed run.
- Full `tests/core`, `tests/visibility`, and existing OTel integration tests
pass; pre-commit clean.
<img width="1512" height="869" alt="langfuse-chat-response-input-output"
src="https://github.com/user-attachments/assets/3799f4de-ae6d-47a7-902d-752a88a00394"
/>
<img width="1512" height="867" alt="langfuse-error-check-invalid-api-key"
src="https://github.com/user-attachments/assets/cb43689f-a27a-412d-bf15-f82d5f24c5e6"
/>
<img width="1512" height="869" alt="langfuse-openai-query-token-view"
src="https://github.com/user-attachments/assets/b1b29770-c77f-492d-9165-e233b9f5dc96"
/>
<img width="1512" height="911"
alt="langfuse-prepare-history-length-metadata"
src="https://github.com/user-attachments/assets/6540ca76-8c22-4a5f-8c07-2d8dbd2ba517"
/>
<img width="1512" height="870" alt="langfuse-session-user-mapping"
src="https://github.com/user-attachments/assets/b78cc186-bd40-4e67-980c-d84341596d3f"
/>
<img width="1512" height="868" alt="langfuse-trace-list"
src="https://github.com/user-attachments/assets/ea34eb80-6e9c-4d92-9bbb-a558c488ca29"
/>
## Notes
- **TODO: Pre-existing core gap found while testing:**
`post_run_execute_call` hooks always receive `exception=None`; the wrappers in
`_call_execute_method_pre_post` (`burr/core/application.py`) initialize `exc =
None` inside a `try/finally` with no `except`, so the exception is never
captured. This affects the existing `OpenTelemetryBridge` too: root spans of
failed runs are never marked errored (Langfuse still flags the trace via
child-observation errors). This PR deliberately does not touch core; the error
test asserts on the step span only, with a comment marking the assertion to
strengthen once fixed. I have an 8-line fix with sync/async regression tests
ready and am happy to file an issue and/or separate PR, whichever is preferred.
- The original issue said to open a follow-up for approach (2); I can file a
tracking issue for the deferred items (deeper model reconciliation per #46,
Langfuse-native generation types, scoring helpers) if useful.
## Checklist
- [X] PR has an informative and human-readable title (this will be pulled
into the release notes)
- [X] Changes are limited to a single goal (no scope creep)
- [X] Code passed the pre-commit check & code is left cleaner/nicer than
when first encountered.
- [X] Any _change_ in functionality is tested
- [X] New functions are documented (with a description, list of inputs, and
expected output)
- [X] Placeholder code is flagged / future TODOs are captured in comments
- [X] Project documentation has been updated if adding/changing
functionality.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]