From 8d682074b300c85b9a778f0160c202e7ee1115bf Mon Sep 17 00:00:00 2001 From: philipshurpik Date: Thu, 14 May 2026 21:19:43 +0100 Subject: [PATCH] option to force keep_trace_attributes in langfuse --- langfuse/langchain/CallbackHandler.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/langfuse/langchain/CallbackHandler.py b/langfuse/langchain/CallbackHandler.py index 1349f6ae0..602348799 100644 --- a/langfuse/langchain/CallbackHandler.py +++ b/langfuse/langchain/CallbackHandler.py @@ -1191,6 +1191,10 @@ def __on_llm_action( current_parent_run_id ) + # Keep trace attributes if llm runs isolated outside chain, or caller forces it via metadata + keep_langfuse_trace_attributes = bool( + (metadata or {}).get("keep_trace_attributes") or parent_run_id is None + ) content = { "name": self.get_langchain_run_name(serialized, **kwargs), "input": prompts, @@ -1198,10 +1202,7 @@ def __on_llm_action( parent_run_id=parent_run_id, tags=tags, metadata=metadata, - # If llm is run isolated and outside chain, keep trace attributes - keep_langfuse_trace_attributes=True - if parent_run_id is None - else False, + keep_langfuse_trace_attributes=keep_langfuse_trace_attributes, ), "model": model_name, "model_parameters": self._parse_model_parameters(kwargs),