@@ -324,17 +324,22 @@ export class LangfuseClient {
324324
325325 this . traceState . tracedGenerationIds . add ( input . messageID ) ;
326326
327- const output = this . getAssistantText ( input . messageID ) ;
327+ const output = {
328+ role : "assistant" as const ,
329+ content : this . getAssistantText ( input . messageID ) ,
330+ } ;
331+ const turn = this . getTurnObservation ( input . sessionID , input . parentID ) ;
332+ turn ?. span . setAttribute (
333+ "langfuse.observation.output" ,
334+ JSON . stringify ( output ) ,
335+ ) ;
328336 const step = this . traceState . activeGenerationSteps . get ( input . sessionID ) ;
329337
330338 if ( step ) {
331339 step . span . setAttribute ( "langfuse.observation.model.name" , input . modelID ) ;
332340 step . span . setAttribute (
333341 "langfuse.observation.output" ,
334- JSON . stringify ( {
335- role : "assistant" ,
336- content : output ,
337- } ) ,
342+ JSON . stringify ( output ) ,
338343 ) ;
339344 step . span . setAttribute (
340345 "langfuse.observation.usage_details" ,
@@ -379,10 +384,7 @@ export class LangfuseClient {
379384 "langfuse.observation.type" : "generation" ,
380385 "session.id" : input . sessionID ,
381386 "langfuse.observation.model.name" : input . modelID ,
382- "langfuse.observation.output" : JSON . stringify ( {
383- role : "assistant" ,
384- content : output ,
385- } ) ,
387+ "langfuse.observation.output" : JSON . stringify ( output ) ,
386388 "langfuse.observation.usage_details" : JSON . stringify ( {
387389 input : input . tokens . input ,
388390 output : input . tokens . output ,
@@ -572,17 +574,22 @@ export class LangfuseClient {
572574 messageID : string | undefined ,
573575 fn : ( ) => T ,
574576 ) {
575- const parentSpan =
576- ( messageID
577- ? this . traceState . turnObservationsByMessageId . get ( messageID ) ?. span
578- : undefined ) ??
579- this . traceState . latestTurnObservationsBySession . get ( sessionID ) ?. span ;
577+ const parentSpan = this . getTurnObservation ( sessionID , messageID ) ?. span ;
580578
581579 return parentSpan
582580 ? context . with ( trace . setSpan ( context . active ( ) , parentSpan ) , fn )
583581 : fn ( ) ;
584582 }
585583
584+ private getTurnObservation ( sessionID : string , messageID : string | undefined ) {
585+ return (
586+ ( messageID
587+ ? this . traceState . turnObservationsByMessageId . get ( messageID )
588+ : undefined ) ??
589+ this . traceState . latestTurnObservationsBySession . get ( sessionID )
590+ ) ;
591+ }
592+
586593 private withObservationParent < T > ( sessionID : string , fn : ( ) => T ) {
587594 const parentSpan =
588595 this . traceState . activeGenerationSteps . get ( sessionID ) ?. span ??
0 commit comments