Skip to content

Commit c4809df

Browse files
committed
feat(context fix): fixed sessions and users for langchain callback
1 parent a7985a6 commit c4809df

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/openlayer/lib/integrations/langchain_callback.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Module with the Openlayer callback handler for LangChain."""
22

33
# pylint: disable=unused-argument
4+
import contextvars
45
import time
56
from typing import Any, Callable, Dict, List, Optional, Union
67
from uuid import UUID
@@ -190,8 +191,9 @@ def _end_step(
190191
):
191192
trace = self._traces_by_root.pop(run_id)
192193
if tracer._configured_background_publish_enabled:
194+
ctx = contextvars.copy_context()
193195
tracer._get_background_executor().submit(
194-
self._process_and_upload_trace, trace
196+
ctx.run, self._process_and_upload_trace, trace
195197
)
196198
else:
197199
self._process_and_upload_trace(trace)
@@ -1247,8 +1249,9 @@ def _end_step(
12471249
if is_root_step and has_standalone_trace and not self._has_external_trace:
12481250
trace = self._traces_by_root.pop(run_id)
12491251
if tracer._configured_background_publish_enabled:
1252+
ctx = contextvars.copy_context()
12501253
tracer._get_background_executor().submit(
1251-
self._process_and_upload_async_trace, trace
1254+
ctx.run, self._process_and_upload_async_trace, trace
12521255
)
12531256
else:
12541257
self._process_and_upload_async_trace(trace)

src/openlayer/lib/tracing/tracer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1631,9 +1631,12 @@ def _handle_trace_completion(
16311631

16321632
if _publish:
16331633
if _configured_background_publish_enabled:
1634-
# Submit to background thread pool
1634+
# Submit to background thread pool, copying context so that
1635+
# contextvars (user_id, session_id, etc.) are preserved.
1636+
ctx = contextvars.copy_context()
16351637
executor = _get_background_executor()
16361638
executor.submit(
1639+
ctx.run,
16371640
_upload_and_publish_trace,
16381641
current_trace,
16391642
resolved_pipeline_id,

0 commit comments

Comments
 (0)