From dfc40872e6340dba6b356cd880e2f13e95870710 Mon Sep 17 00:00:00 2001 From: Mashel Odera Date: Fri, 17 Apr 2026 15:53:06 +0300 Subject: [PATCH 1/7] fix: working Langfuse tracing and markdown UI rendering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace broken LangChain callback (incompatible with langchain 1.x) with Langfuse SDK direct trace API — no LangChain dependency, works reliably with Langfuse server v2. Add marked@15 to web app and render agent messages as proper HTML so bold (**text**), lists, code blocks, and headers display correctly instead of showing raw *** symbols. Co-Authored-By: Claude Sonnet 4.6 --- apps/api/agent.py | 41 +++++++++++++++++++++--------------- apps/web/app/pages/index.vue | 29 ++++++++++++++++++++++++- apps/web/package-lock.json | 13 ++++++++++++ apps/web/package.json | 1 + 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/apps/api/agent.py b/apps/api/agent.py index 65a518a..8ef4977 100644 --- a/apps/api/agent.py +++ b/apps/api/agent.py @@ -66,24 +66,26 @@ async def run_agent( invoke_config: RunnableConfig = {"configurable": {"thread_id": thread_id}} - langfuse_handler = None + lf_client = None + lf_trace = None if settings.langfuse_public_key and settings.langfuse_secret_key: try: - from langfuse.callback import CallbackHandler # type: ignore[import-untyped] - - lf_kwargs: dict[str, object] = { - "public_key": settings.langfuse_public_key, - "secret_key": settings.langfuse_secret_key, - "host": settings.langfuse_host, - "user_id": body.user_id, - "session_id": thread_id, - "trace_name": _LANGFUSE_TRACE_NAME, - "tags": [settings.environment, settings.llm_provider], - } - langfuse_handler = CallbackHandler(**lf_kwargs) # type: ignore[arg-type] - invoke_config["callbacks"] = [langfuse_handler] + from langfuse import Langfuse # type: ignore[import-untyped] + + lf_client = Langfuse( + public_key=settings.langfuse_public_key, + secret_key=settings.langfuse_secret_key, + host=settings.langfuse_host, + ) + lf_trace = lf_client.trace( + name=_LANGFUSE_TRACE_NAME, + user_id=body.user_id, + session_id=thread_id, + tags=[settings.environment, settings.llm_provider], + input=body.input, + ) except Exception as exc: - log.warning("langfuse.callback.unavailable", error=str(exc)) + log.warning("langfuse.trace.init_failed", error=str(exc)) start = time.perf_counter() result = await request.app.state.agent.ainvoke(cast(AgentState, state), config=invoke_config) @@ -101,8 +103,13 @@ async def run_agent( thread_id=thread_id, ) - if langfuse_handler is not None: - background_tasks.add_task(langfuse_handler.langfuse.flush) + if lf_trace is not None: + import contextlib + + with contextlib.suppress(Exception): + lf_trace.update(output=result.get("final_output")) + if lf_client is not None: + background_tasks.add_task(lf_client.flush) background_tasks.add_task( _persist_run, diff --git a/apps/web/app/pages/index.vue b/apps/web/app/pages/index.vue index 375bcf4..1e908cd 100644 --- a/apps/web/app/pages/index.vue +++ b/apps/web/app/pages/index.vue @@ -41,7 +41,7 @@
{{ msg.role === 'user' ? 'You' : 'Agent' }}
-
{{ msg.content }}
+
@@ -116,6 +116,13 @@