From a43ebe741840142424d11adefc5b40357ec75342 Mon Sep 17 00:00:00 2001 From: Max Friedrich Date: Tue, 6 May 2025 13:50:49 +0200 Subject: [PATCH] feat: ignore control flow exceptions in LangChain callback (#1169) Co-authored-by: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com> --- langfuse/callback/langchain.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/langfuse/callback/langchain.py b/langfuse/callback/langchain.py index 674ab0b21..3f9ec39b3 100644 --- a/langfuse/callback/langchain.py +++ b/langfuse/callback/langchain.py @@ -13,7 +13,7 @@ log.error( f"Could not import langchain. The langchain integration will not work. {e}" ) -from typing import Any, Dict, List, Optional, Sequence, Union, cast +from typing import Any, Dict, List, Optional, Sequence, Set, Type, Union, cast from uuid import UUID, uuid4 from langfuse.api.resources.ingestion.types.sdk_log_body import SdkLogBody @@ -52,7 +52,13 @@ ) LANGSMITH_TAG_HIDDEN: str = "langsmith:hidden" +CONTROL_FLOW_EXCEPTION_TYPES: Set[Type[BaseException]] = set() +try: + from langgraph.errors import GraphBubbleUp + CONTROL_FLOW_EXCEPTION_TYPES.add(GraphBubbleUp) +except ImportError: + pass class LangchainCallbackHandler( LangchainBaseCallbackHandler, LangfuseBaseCallbackHandler @@ -484,8 +490,13 @@ def on_chain_error( try: self._log_debug_event("on_chain_error", run_id, parent_run_id, error=error) if run_id in self.runs: + if any(isinstance(error, t) for t in CONTROL_FLOW_EXCEPTION_TYPES): + level = None + else: + level = "ERROR" + self.runs[run_id] = self.runs[run_id].end( - level="ERROR", + level=level, status_message=str(error), version=self.version, input=kwargs.get("inputs"),