rag_project_backend / observability.py
anhkhoiphan's picture
deploy: 2026-06-05 11:40:44
4d85858
Raw
History Blame Contribute Delete
977 Bytes
from __future__ import annotations
from typing import Any
from src.config import LANGFUSE_ENABLED
def langfuse_config(user_id: str, session_id: str | None = None) -> dict[str, Any]:
"""Build LangChain/LangGraph runtime config with Langfuse tracing."""
if not LANGFUSE_ENABLED:
return {}
from langfuse.langchain import CallbackHandler
return {
"callbacks": [CallbackHandler()],
"metadata": {
"langfuse_user_id": user_id,
"langfuse_session_id": session_id or user_id,
"langfuse_tags": ["agentic-rag", "langgraph", "gemini"],
},
"run_name": "agentic-rag-chat",
}
def flush_langfuse() -> None:
"""Force pending Langfuse spans to be exported after a request."""
if not LANGFUSE_ENABLED:
return
try:
from langfuse import get_client
get_client().flush()
except Exception as exc:
print(f"[LANGFUSE] Failed to flush traces: {exc}")