Commit aaa3cc3
authored
Migrate Langfuse to typed LLM event (#143)
* Migrate Langfuse to typed LLM event
Drive the openarmature.llm.complete Generation observation
lifecycle from LlmCompletionEvent on the success path, mirroring
PR 3b's OTel shape. Open and close the Generation in one shot at
typed-event arrival with start_time back-dated by latency_ms so
the Langfuse UI shows the adapter-boundary measurement rather
than dispatcher queue delay. Sentinel pair stays for the failure
path until the spec extends LlmCompletionEvent with error
semantics (coord thread filed).
Widen the LangfuseClient Protocol with optional start_time on
generation() and end_time on Span/Generation handle end(). The
SDK adapter forwards both to the v4 SDK; the InMemory client
stores them on LangfuseObservation for test assertions.
Drop the sentinel NodeEvent pair emission on the success path
from OpenAIProvider.complete(). The bundled OTel and Langfuse
observers consume the typed event directly; external custom
observers consuming LLM events MUST migrate to isinstance
discrimination. The sentinel completed event still fires on the
failure path; sentinel started is no longer emitted.
* Route Langfuse timestamps through OTel tracer
The v4.7 Langfuse SDK exposes timestamp control only on its
internal OTel tracer, not on the public start_observation() API
that the adapter was calling. Two quirks the original Protocol
widening got wrong, both surfaced by live-account verification:
start_observation() rejects a start_time kwarg with TypeError.
When start_time is supplied, the adapter now mirrors the SDK's
own create_event precedent: open the OTel span directly via
_otel_tracer.start_span(name=, start_time=int_ns) within a
trace context and wrap the result in LangfuseGeneration. The
existing no-start_time path still uses the public API.
LangfuseSpan.end(end_time) is typed Optional[int] (nanoseconds),
not datetime. The adapter now converts the Protocol's datetime
surface to nanoseconds before forwarding. Without the conversion
the OTel span_processor's formatter crashes with TypeError on
end_time / 1e9 deep in the SDK.
Strengthen the unit tests: spy on both _otel_tracer.start_span
and start_observation so the back-dated path asserts the OTel
route is taken and the public-API path asserts the OTel tracer
is NOT touched. The previous monkeypatch test accepted **kwargs
and would have passed even with the broken implementation.
Widen the integration test's REST poll budget to 180s and use
server-side name+type filters; add a diagnostic that lists
observation names actually projected under the trace_id when
the target Generation doesn't appear, so a future name-mismatch
SDK change surfaces explicitly.
* Address PR 143 review
Tighten three stale comments that still referred to a sentinel
"pair" on the failure path. The provider now emits only a single
sentinel completed event on failure (no started counterpart);
the comments in langfuse/observer.py (dispatch site + handler
docstring) and openai.py (failure-emission site) didn't catch up
with the v0.13.0 emission change in the same PR.
* Align CHANGELOG + integration docstring with SDK routing
Both descriptions were written before live-account verification
revealed that v4.7 Langfuse SDK's start_observation rejects
start_time with TypeError. The CHANGELOG entry claimed the adapter
forwards via start_observation(start_time=...); the integration
test docstring said unit tests validate that surface. Rewrote
both to describe the actual routing: the back-dated path bypasses
start_observation and goes through the private _otel_tracer.start_
span, wrapping the OTel span in LangfuseGeneration directly. The
guarded failure mode shifts accordingly: not "SDK silently drops
start_time" but "future SDK renames _otel_tracer or moves
LangfuseGeneration", breaking the private-API path silently.1 parent 371545a commit aaa3cc3
10 files changed
Lines changed: 926 additions & 228 deletions
File tree
- src/openarmature
- llm/providers
- observability
- langfuse
- otel
- tests
- integration
- unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
441 | 441 | | |
442 | 442 | | |
443 | 443 | | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | 444 | | |
460 | 445 | | |
461 | 446 | | |
| |||
473 | 458 | | |
474 | 459 | | |
475 | 460 | | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
483 | 470 | | |
484 | 471 | | |
485 | 472 | | |
| |||
498 | 485 | | |
499 | 486 | | |
500 | 487 | | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
528 | 496 | | |
529 | 497 | | |
530 | 498 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
129 | 130 | | |
130 | 131 | | |
131 | 132 | | |
132 | | - | |
| 133 | + | |
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
136 | 141 | | |
137 | 142 | | |
138 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
139 | 147 | | |
140 | 148 | | |
141 | 149 | | |
| |||
337 | 345 | | |
338 | 346 | | |
339 | 347 | | |
| 348 | + | |
340 | 349 | | |
341 | 350 | | |
342 | 351 | | |
| |||
356 | 365 | | |
357 | 366 | | |
358 | 367 | | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
369 | 399 | | |
370 | 400 | | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
371 | 460 | | |
372 | 461 | | |
373 | 462 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
68 | 76 | | |
69 | 77 | | |
70 | 78 | | |
| |||
125 | 133 | | |
126 | 134 | | |
127 | 135 | | |
128 | | - | |
| 136 | + | |
129 | 137 | | |
130 | 138 | | |
131 | 139 | | |
| |||
138 | 146 | | |
139 | 147 | | |
140 | 148 | | |
141 | | - | |
| 149 | + | |
142 | 150 | | |
143 | 151 | | |
144 | 152 | | |
| |||
224 | 232 | | |
225 | 233 | | |
226 | 234 | | |
| 235 | + | |
227 | 236 | | |
228 | 237 | | |
229 | 238 | | |
| |||
268 | 277 | | |
269 | 278 | | |
270 | 279 | | |
271 | | - | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
272 | 283 | | |
273 | 284 | | |
274 | 285 | | |
| |||
286 | 297 | | |
287 | 298 | | |
288 | 299 | | |
289 | | - | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
290 | 303 | | |
291 | 304 | | |
292 | 305 | | |
| |||
428 | 441 | | |
429 | 442 | | |
430 | 443 | | |
| 444 | + | |
431 | 445 | | |
432 | 446 | | |
433 | 447 | | |
| |||
444 | 458 | | |
445 | 459 | | |
446 | 460 | | |
| 461 | + | |
447 | 462 | | |
448 | 463 | | |
449 | 464 | | |
| |||
0 commit comments