@@ -149,6 +149,30 @@ def on_retriever_error(
149
149
except Exception as e :
150
150
langfuse_logger .exception (e )
151
151
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
+
152
176
def on_chain_start (
153
177
self ,
154
178
serialized : Optional [Dict [str , Any ]],
@@ -173,7 +197,7 @@ def on_chain_start(
173
197
span_level = "DEBUG" if tags and LANGSMITH_TAG_HIDDEN in tags else None
174
198
175
199
if parent_run_id is None :
176
- self . runs [ run_id ] = self .client .start_span (
200
+ span = self .client .start_span (
177
201
name = span_name ,
178
202
metadata = span_metadata ,
179
203
input = inputs ,
@@ -182,6 +206,10 @@ def on_chain_start(
182
206
span_level ,
183
207
),
184
208
)
209
+ span .update_trace (
210
+ ** self ._parse_langfuse_trace_attributes_from_metadata (metadata )
211
+ )
212
+ self .runs [run_id ] = span
185
213
else :
186
214
self .runs [run_id ] = cast (
187
215
LangfuseSpan , self .runs [parent_run_id ]
@@ -1003,7 +1031,12 @@ def _strip_langfuse_keys_from_dict(metadata: Optional[Dict[str, Any]]) -> Any:
1003
1031
if metadata is None or not isinstance (metadata , dict ):
1004
1032
return metadata
1005
1033
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
+ ]
1007
1040
1008
1041
metadata_copy = metadata .copy ()
1009
1042
0 commit comments