Skip to content

Commit e475334

Browse files
committed
Fix: Enter Langfuse span context to properly attach child spans
The Langfuse session span was created with start_as_current_span() but never entered, causing child generations and tool spans to be orphaned. Now properly using __enter__()/__exit__() to manage span lifecycle. This fixes the missing Langfuse SDK traces in the UI.
1 parent 76966c8 commit e475334

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

components/runners/claude-code-runner/observability.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ async def _init_langfuse(self, prompt: str, namespace: str) -> bool:
121121
"user_name": self.user_name if self.user_name else None,
122122
},
123123
)
124+
# Enter the span context to make it current
125+
self.langfuse_span.__enter__()
124126

125127
if self.user_id:
126128
logging.info(f"Langfuse: Tracking session for user {self.user_name} ({self.user_id})")
@@ -464,6 +466,8 @@ async def finalize(self, result_payload: dict | None) -> None:
464466
# Complete Langfuse session span with final results
465467
if self.langfuse_span and self.langfuse_client and result_payload:
466468
try:
469+
# Exit span context before ending
470+
self.langfuse_span.__exit__(None, None, None)
467471
self.langfuse_span.end(
468472
output=result_payload,
469473
metadata={
@@ -527,6 +531,8 @@ async def cleanup_on_error(self, error: Exception) -> None:
527531
# 1. End Langfuse session span with error if available
528532
if self.langfuse_span and self.langfuse_client:
529533
try:
534+
# Exit span context before ending
535+
self.langfuse_span.__exit__(None, None, None)
530536
self.langfuse_span.end(
531537
level="ERROR",
532538
status_message=str(error)

0 commit comments

Comments
 (0)