Skip to content

add langfuse tracing#39

Merged
mjmmattoni98 merged 1 commit into
devfrom
feature/langfuse-tracking
May 12, 2026
Merged

add langfuse tracing#39
mjmmattoni98 merged 1 commit into
devfrom
feature/langfuse-tracking

Conversation

@alafca

@alafca alafca commented May 11, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

Release Notes

  • New Features

    • Added optional Langfuse observability integration to trace LLM calls and workflow execution
  • Documentation

    • Updated configuration guide with Langfuse setup instructions and environment variable requirements
  • Chores

    • Added langfuse dependency (v4.x)

Review Change Stack

@alafca alafca requested a review from mjmmattoni98 May 11, 2026 13:11
@dokploy-asm2-client

dokploy-asm2-client Bot commented May 11, 2026

Copy link
Copy Markdown

Dokploy Preview Deployment

Name Status Preview Updated (UTC)
dashboard ❌ Failed Preview URL 2026-05-11T13:11:47.417Z

@coderabbitai

coderabbitai Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The pull request adds optional Langfuse observability for LLM tracing by introducing a cached credential-driven factory function, exporting it at the tracing module level, integrating it into the chat server to conditionally attach callbacks and user/session metadata to graph invocations, and documenting configuration requirements.

Changes

Langfuse Observability Integration

Layer / File(s) Summary
Langfuse Handler Factory
backend/src/tracing/langfuse_setup.py
New module with cached get_langfuse_handler() function that reads LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, and LANGFUSE_BASE_URL from environment, returns None if any are missing, initializes Langfuse and returns a CallbackHandler on success, or returns None with warning logging if initialization fails. Result is memoized with @lru_cache(maxsize=1).
Module Export
backend/src/tracing/__init__.py
Re-exports get_langfuse_handler at the package level with __all__ definition.
Dependency Declaration
backend/pyproject.toml
Adds langfuse>=4.0.0,<5.0.0 to project dependencies.
Chat Server Integration
backend/server.py
Imports handler factory, explicitly types chat config as dict[str, Any], obtains Langfuse handler during each chat turn, and conditionally sets config["callbacks"], config["run_name"], and config["metadata"] (including user id, session id, tags) when handler is present.
Configuration & Documentation
.env.example, README.md
Documents three Langfuse environment variables with instructions that tracing is disabled when any are unset, and links to credential sources.

Sequence Diagram

sequenceDiagram
  participant User
  participant ChatEndpoint as Chat Endpoint
  participant GetHandler as get_langfuse_handler()
  participant GraphInvoke as LangGraph Invoke
  participant LLMCall as LLM Call
  
  User->>ChatEndpoint: Send chat message
  ChatEndpoint->>GetHandler: Obtain handler (cached)
  GetHandler-->>ChatEndpoint: CallbackHandler or None
  ChatEndpoint->>ChatEndpoint: Build invocation config<br/>with optional callbacks/metadata
  ChatEndpoint->>GraphInvoke: Invoke with config
  GraphInvoke->>LLMCall: Call LLM
  LLMCall-->>GraphInvoke: Response
  GraphInvoke-->>ChatEndpoint: Result
  ChatEndpoint-->>User: Chat response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A hop through Langfuse we trace,
Where handlers cache in their place,
Environment keys unlock the way,
To watch each LLM call and play,
Callbacks flow, metadata gleams bright,
Observability done just right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'add langfuse tracing' directly and concisely summarizes the main change: adding Langfuse observability/tracing integration across configuration, documentation, and backend code.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/langfuse-tracking

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
README.md (1)

94-102: ⚡ Quick win

Consider adding an example for LANGFUSE_BASE_URL.

The documentation provides clear examples for the Public and Secret keys, but the LANGFUSE_BASE_URL row has an empty example column. Consider adding an example like https://cloud.langfuse.com or https://us.cloud.langfuse.com (as mentioned in .env.example line 109) to help users configure this variable correctly.

📝 Suggested addition
 | Variable | Descripción | Ejemplo |
 | --- | --- | --- |
 | `LANGFUSE_PUBLIC_KEY` | Clave pública del proyecto (Langfuse UI → Settings → API Keys) | `pk-lf-...` |
 | `LANGFUSE_SECRET_KEY` | Clave secreta del proyecto | `sk-lf-...` |
-| `LANGFUSE_BASE_URL` | Host de Langfuse. |  |
+| `LANGFUSE_BASE_URL` | Host de Langfuse. | `https://cloud.langfuse.com` |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@README.md` around lines 94 - 102, Update the README table row for
LANGFUSE_BASE_URL to include a concrete example URL (e.g.,
"https://cloud.langfuse.com" or "https://us.cloud.langfuse.com") in the Example
column so users have a clear reference for valid values; locate the LANGFUSE
(Trazabilidad) section and the table containing `LANGFUSE_PUBLIC_KEY`,
`LANGFUSE_SECRET_KEY`, and `LANGFUSE_BASE_URL` and add the example text to the
`LANGFUSE_BASE_URL` example cell.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/pyproject.toml`:
- Line 66: The Langfuse client is being constructed using the deprecated host
parameter; update the Langfuse(...) instantiation (look for the call to Langfuse
in the codebase) to replace host=base_url with base_url=base_url so it matches
langfuse>=4.0.0's API; ensure any variable name (base_url) is unchanged and run
tests/imports to confirm the new parameter is accepted.

---

Nitpick comments:
In `@README.md`:
- Around line 94-102: Update the README table row for LANGFUSE_BASE_URL to
include a concrete example URL (e.g., "https://cloud.langfuse.com" or
"https://us.cloud.langfuse.com") in the Example column so users have a clear
reference for valid values; locate the LANGFUSE (Trazabilidad) section and the
table containing `LANGFUSE_PUBLIC_KEY`, `LANGFUSE_SECRET_KEY`, and
`LANGFUSE_BASE_URL` and add the example text to the `LANGFUSE_BASE_URL` example
cell.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1c7dc1ae-cd16-4616-aa2a-0903d08f30d4

📥 Commits

Reviewing files that changed from the base of the PR and between 1d58a9b and 0846f86.

⛔ Files ignored due to path filters (1)
  • backend/uv.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • .env.example
  • README.md
  • backend/pyproject.toml
  • backend/server.py
  • backend/src/tracing/__init__.py
  • backend/src/tracing/langfuse_setup.py

Comment thread backend/pyproject.toml
"langchain-core==1.2.18",
"langchain-openai==1.1.11",
"langchain-text-splitters==1.1.1",
"langfuse>=4.0.0,<5.0.0",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify langfuse package version availability on PyPI
echo "Checking langfuse versions on PyPI..."
curl -s https://pypi.org/pypi/langfuse/json | jq -r '.releases | keys[]' | grep '^4\.' | head -20

Repository: in2ai/ASM2-client

Length of output: 218


🌐 Web query:

Does langfuse 4.0.0 or higher exist? What is the correct parameter name for the Langfuse constructor to set the base URL/host in version 4.x?

💡 Result:

Yes, Langfuse Python SDK version 4.0.0 and higher exist. The latest version is 4.6.1 as of May 2026.[1][2] In version 4.x, the correct parameter name for the Langfuse constructor to set the base URL/host is base_url. The host parameter is deprecated; use base_url instead.[3][2][4][5][6]

Citations:


🏁 Script executed:

cat -n backend/src/tracing/langfuse_setup.py

Repository: in2ai/ASM2-client

Length of output: 1191


Replace host parameter with base_url in Langfuse constructor call.

Langfuse version 4.0.0 exists and is available on PyPI. However, the code at line 24 uses the deprecated host parameter when instantiating Langfuse(). In version 4.x, the correct parameter is base_url. Change host=base_url to base_url=base_url to ensure compatibility with the pinned version constraint.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/pyproject.toml` at line 66, The Langfuse client is being constructed
using the deprecated host parameter; update the Langfuse(...) instantiation
(look for the call to Langfuse in the codebase) to replace host=base_url with
base_url=base_url so it matches langfuse>=4.0.0's API; ensure any variable name
(base_url) is unchanged and run tests/imports to confirm the new parameter is
accepted.

@mjmmattoni98 mjmmattoni98 merged commit 0f3f9a6 into dev May 12, 2026
1 check passed
@mjmmattoni98 mjmmattoni98 deleted the feature/langfuse-tracking branch May 12, 2026 11:23
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.

2 participants