@@ -149,6 +149,30 @@ def on_retriever_error(
149149 except Exception as e :
150150 langfuse_logger .exception (e )
151151
152+ def _parse_langfuse_trace_attributes_from_metadata (
153+ self ,
154+ metadata : Optional [Dict [str , Any ]],
155+ ) -> Dict [str , Any ]:
156+ attributes : Dict [str , Any ] = {}
157+
158+ if metadata is None :
159+ return attributes
160+
161+ if "langfuse_session_id" in metadata and isinstance (
162+ metadata ["langfuse_session_id" ], str
163+ ):
164+ attributes ["session_id" ] = metadata ["langfuse_session_id" ]
165+
166+ if "langfuse_user_id" in metadata and isinstance (
167+ metadata ["langfuse_user_id" ], str
168+ ):
169+ attributes ["user_id" ] = metadata ["langfuse_user_id" ]
170+
171+ if "langfuse_tags" in metadata and isinstance (metadata ["langfuse_tags" ], list ):
172+ attributes ["tags" ] = [str (tag ) for tag in metadata ["langfuse_tags" ]]
173+
174+ return attributes
175+
152176 def on_chain_start (
153177 self ,
154178 serialized : Optional [Dict [str , Any ]],
@@ -173,7 +197,7 @@ def on_chain_start(
173197 span_level = "DEBUG" if tags and LANGSMITH_TAG_HIDDEN in tags else None
174198
175199 if parent_run_id is None :
176- self . runs [ run_id ] = self .client .start_span (
200+ span = self .client .start_span (
177201 name = span_name ,
178202 metadata = span_metadata ,
179203 input = inputs ,
@@ -182,6 +206,10 @@ def on_chain_start(
182206 span_level ,
183207 ),
184208 )
209+ span .update_trace (
210+ ** self ._parse_langfuse_trace_attributes_from_metadata (metadata )
211+ )
212+ self .runs [run_id ] = span
185213 else :
186214 self .runs [run_id ] = cast (
187215 LangfuseSpan , self .runs [parent_run_id ]
@@ -1003,7 +1031,12 @@ def _strip_langfuse_keys_from_dict(metadata: Optional[Dict[str, Any]]) -> Any:
10031031 if metadata is None or not isinstance (metadata , dict ):
10041032 return metadata
10051033
1006- langfuse_metadata_keys = ["langfuse_prompt" ]
1034+ langfuse_metadata_keys = [
1035+ "langfuse_prompt" ,
1036+ "langfuse_session_id" ,
1037+ "langfuse_user_id" ,
1038+ "langfuse_tags" ,
1039+ ]
10071040
10081041 metadata_copy = metadata .copy ()
10091042
0 commit comments