Skip to content

fix: valid LangFuse trace ids, trace metadata, feedback scores - #518

Merged
neuromechanist merged 3 commits into
OpenScience-Collective:developfrom
bendichter:fix/issue-515-langfuse-tracing
Sep 25, 2026
Merged

neuromechanist merged 3 commits into
OpenScience-Collective:developfrom
bendichter:fix/issue-515-langfuse-tracing

Conversation

@bendichter

Copy link
Copy Markdown
Contributor

Fixes #515.

Summary

1. Valid trace ids (bug fix). create_community_assistant built trace ids as f"{community_id}-{uuid.uuid4().hex[:12]}". LangFuse requires 32 lowercase hex characters, so it rejected those ids (Passed trace ID 'nwb-…' is not a valid 32 lowercase hex char Langfuse trace id, then a ValueError in on_chain_start). As a result, every LLM call and tool call became its own trace, and request_log.langfuse_trace_id matched nothing. Trace ids are now uuid.uuid4().hex.

2. Trace metadata. The LangChain run config now carries langfuse_tags: [community_id], langfuse_user_id (from X-User-ID), and langfuse_session_id for /chat and streaming chat. This keeps the community filterable now that it's no longer in the id, and groups a conversation's turns. create_community_assistant gains an optional session_id parameter for this.

3. Feedback as scores. POST /feedback now looks up the rated request's langfuse_trace_id and attaches a user_feedback score (categorical up/down, with the comment) to that trace. The LangFuse client is built from Settings, so this also works before the first traced request. It's best-effort, like write_feedback: it's skipped when LangFuse isn't configured or the request wasn't traced, and it logs errors without failing the request.

Tests

  • New in tests/test_api/test_community_router.py: TestLangfuseTracing checks the trace metadata variants, and that create_community_assistant requests a 32-hex trace id and attaches the metadata. The tracing service is faked, so langfuse isn't needed.
  • New in tests/test_api/test_feedback_endpoint.py: the score is sent to the answer's trace, nothing is sent when the request wasn't traced or LangFuse isn't configured, and a LangFuse error still returns 200. These use a fake langfuse module.
  • pytest -m "not network": 2978 passed. Excluded are tests that also fail on develop in my environment (they need bun, or hit an existing AttributeError in tests/test_integration/test_llm.py); see feat: add NWB community #517 for details.
  • ruff check and ruff format --check are clean.
  • End to end against self-hosted LangFuse 4.46 with the pinned langfuse 3.11.2 SDK: one /hed/ask now produces a single trace whose id matches request_log, with tag ['hed'] and the user id. POST /feedback for that request adds a user_feedback score to the same trace. Before this change, the same request produced one trace per LLM or tool call.

Benjamin Dichter added 2 commits September 25, 2026 15:43
LangFuse requires 32-char lowercase hex trace ids. The previous
'<community>-<12 hex>' ids were rejected, so the root span was never
created: each LLM and tool call became its own trace and
request_log.langfuse_trace_id matched nothing.

Traces now also carry the community as a tag, the X-User-ID as the
user, and the chat session id for /chat and streaming chat.

Refs OpenScience-Collective#515
POST /feedback now attaches thumbs up/down (and the comment) to the
rated answer's LangFuse trace as a user_feedback score, using
request_log.langfuse_trace_id. Best-effort: skipped when LangFuse is
not configured or the request was not traced, and errors are logged
without failing the request.

Refs OpenScience-Collective#515
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@neuromechanist neuromechanist left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants