Repository navigation
Read Langfuse observations with Basic auth and the v2 endpoint - #787
Conversation
`tj backfill langfuse --source-url` sent the key as a Bearer token and read the v1 observations endpoint. - Langfuse gives a Bearer public key a scores-only scope, so reading observations needs Basic auth with the public and secret key. `--api-key` now takes PUBLIC_KEY:SECRET_KEY. A value without a colon is still sent as a Bearer token. - Langfuse Cloud removes the v1 observations endpoint on 16 November 2026 and keeps Observations API v2. The adapter reads v2 with cursor pagination and keeps following the cursor through empty pages, and falls back to v1 on a server that answers 404 for v2. Closes Metabuilder-Labs#785
[Medium impact] The PR appears safe to merge; no actionable issues were found.
|
anilmurty
left a comment
There was a problem hiding this comment.
Thanks @volodymyrshk, and good timing: Langfuse's v4 cutover is tomorrow and this keeps tj backfill langfuse working through it. I checked the auth and pagination against Langfuse's live API spec and they match, and it's nice that fields= leaves out io so no prompt content gets pulled. Merging.
One small thing for a follow-up if you want it: a 401/403 still surfaces as a raw httpx.HTTPStatusError traceback (that's true on main too). Catching it in cmd_backfill and printing a hint about the PUBLIC_KEY:SECRET_KEY format would save people some confusion.
|
Also: this is your first merged PR here, so welcome aboard, @volodymyrshk. 🎉 |
Summary
tj backfill langfuse --source-urlreads observations with a Bearer public key from the old/api/public/observationsendpoint. This PR makes the live mode use the project key pair with Basic auth and the Observations API v2.Two things change:
apiAuth.ts, a Bearer public key getsaccessLevel: "scores", a limited scope. Reading observations needs Basic auth with the public and secret key.--api-keynow takesPUBLIC_KEY:SECRET_KEY. A value without a colon is still sent as a Bearer token, so nothing that worked before stops working./api/public/v2/observations, followsmeta.cursoruntil it is absent (the spec says a page can be empty while a cursor is present, so the old "stop on an empty page" rule would drop data), and falls back to the v1 endpoint with page numbers when the server answers 404 for v2, for example an older self-hosted instance.The field mapping needed no change:
_observation_to_spanalready readsusageDetailsandtotalCost, which are the v2 names.Please read the testing note below. I could not run this against a live Langfuse server.
Related issue
Closes #785
Testing
tests/unit/test_ingest_langfuse.py, usinghttpx.MockTransport(no network): Basic auth for a key pair, Bearer kept for a plain key, v2 with cursors including an empty page that has a cursor, thesincebound, a source URL that already names the old endpoint, the v1 fallback on 404, an auth error is not hidden, and a v2 observation maps to a span. Before the change, four of them fail (it sendsBearer pk:skand calls the v1 path).ruff check tokenjam/andmypy tokenjam/are clean.pytest tests/unit/ tests/synthetic/ tests/agents/ tests/integration/: in a parallel run (-n 4) three tests failed (test_summarize_relocate.py::test_the_cli_says_why_when_nothing_is_offeredand two intest_xdist_isolation.py). The same parallel run on a cleanmainalso fails (five tests, including the same two xdist ones), and all three pass when run serially on this branch.web/public/generated/api/openapi.yml) and the auth behaviour fromapiAuth.ts, not from a run. If you have a Langfuse project key pair you can trytj backfill langfuse --source-url https://cloud.langfuse.com --api-key pk-lf-...:sk-lf-... --since 1donce, that would settle it. I am happy to adjust anything it shows.Checklist
pytest tests/unit/ tests/synthetic/ tests/agents/ tests/integration/), apart from the parallel-run failures described above that also fail onmainruff check tokenjam/)mypy tokenjam/)tests/factories.py(not applicable, the new tests use observation payloads)@anilmurtyas reviewer (@-mentioned here: @anilmurty, this is ready for a look)