@@ -1339,12 +1339,20 @@ def _start_as_current_otel_span_with_processed_media(
13391339 cost_details : Optional [Dict [str , float ]] = None ,
13401340 prompt : Optional [PromptClient ] = None ,
13411341 ) -> Any :
1342- with self ._otel_tracer .start_as_current_span (
1343- name = name ,
1344- end_on_exit = end_on_exit if end_on_exit is not None else True ,
1345- ) as otel_span :
1346- baggage_token = None
1342+ # We manage span-context activation ourselves instead of using
1343+ # otel_tracer.start_as_current_span so that the context detach runs
1344+ # through _detach_context_token_safely. OTel's own detach is unguarded
1345+ # and logs "Failed to detach context" whenever __enter__ and __exit__
1346+ # straddle different async contexts (e.g. end_on_exit=False with a
1347+ # manual close), even though the observation is already complete.
1348+ otel_span = self ._otel_tracer .start_span (name = name )
1349+ end_span = end_on_exit if end_on_exit is not None else True
1350+ span_context_token = otel_context_api .attach (
1351+ otel_trace_api .set_span_in_context (otel_span )
1352+ )
1353+ baggage_token = None
13471354
1355+ try :
13481356 if otel_span .is_recording ():
13491357 context_with_app_root_claim = _set_langfuse_trace_id_in_baggage (
13501358 trace_id = self ._get_otel_trace_id (otel_span ),
@@ -1356,41 +1364,54 @@ def _start_as_current_otel_span_with_processed_media(
13561364 as_type or "generation"
13571365 ) # default was "generation"
13581366
1359- try :
1360- common_args = {
1361- "otel_span" : otel_span ,
1362- "langfuse_client" : self ,
1363- "environment" : self ._environment ,
1364- "release" : self ._release ,
1365- "input" : input ,
1366- "output" : output ,
1367- "metadata" : metadata ,
1368- "version" : version ,
1369- "level" : level ,
1370- "status_message" : status_message ,
1371- }
1367+ common_args = {
1368+ "otel_span" : otel_span ,
1369+ "langfuse_client" : self ,
1370+ "environment" : self ._environment ,
1371+ "release" : self ._release ,
1372+ "input" : input ,
1373+ "output" : output ,
1374+ "metadata" : metadata ,
1375+ "version" : version ,
1376+ "level" : level ,
1377+ "status_message" : status_message ,
1378+ }
13721379
1373- if span_class in [
1374- LangfuseGeneration ,
1375- LangfuseEmbedding ,
1376- ]:
1377- common_args .update (
1378- {
1379- "completion_start_time" : completion_start_time ,
1380- "model" : model ,
1381- "model_parameters" : model_parameters ,
1382- "usage_details" : usage_details ,
1383- "cost_details" : cost_details ,
1384- "prompt" : prompt ,
1385- }
1386- )
1387- # For span-like types (span, agent, tool, chain, retriever, evaluator, guardrail), no generation properties needed
1380+ if span_class in [
1381+ LangfuseGeneration ,
1382+ LangfuseEmbedding ,
1383+ ]:
1384+ common_args .update (
1385+ {
1386+ "completion_start_time" : completion_start_time ,
1387+ "model" : model ,
1388+ "model_parameters" : model_parameters ,
1389+ "usage_details" : usage_details ,
1390+ "cost_details" : cost_details ,
1391+ "prompt" : prompt ,
1392+ }
1393+ )
1394+ # For span-like types (span, agent, tool, chain, retriever, evaluator, guardrail), no generation properties needed
13881395
1389- yield span_class (** common_args ) # type: ignore[arg-type]
1396+ yield span_class (** common_args ) # type: ignore[arg-type]
13901397
1391- finally :
1392- if baggage_token is not None :
1393- _detach_context_token_safely (baggage_token )
1398+ except Exception as exc :
1399+ if otel_span .is_recording ():
1400+ otel_span .record_exception (exc )
1401+ otel_span .set_status (
1402+ otel_trace_api .Status (
1403+ status_code = otel_trace_api .StatusCode .ERROR ,
1404+ description = f"{ type (exc ).__name__ } : { exc } " ,
1405+ )
1406+ )
1407+ raise
1408+
1409+ finally :
1410+ if baggage_token is not None :
1411+ _detach_context_token_safely (baggage_token )
1412+ _detach_context_token_safely (span_context_token )
1413+ if end_span :
1414+ otel_span .end ()
13941415
13951416 def _get_current_otel_span (self ) -> Optional [otel_trace_api .Span ]:
13961417 current_span = otel_trace_api .get_current_span ()
0 commit comments