Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/openlayer/lib/integrations/langchain_callback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module with the Openlayer callback handler for LangChain."""

# pylint: disable=unused-argument
import contextvars
import time
from typing import Any, Callable, Dict, List, Optional, Union
from uuid import UUID
Expand Down Expand Up @@ -190,8 +191,9 @@ def _end_step(
):
trace = self._traces_by_root.pop(run_id)
if tracer._configured_background_publish_enabled:
ctx = contextvars.copy_context()
tracer._get_background_executor().submit(
self._process_and_upload_trace, trace
ctx.run, self._process_and_upload_trace, trace
)
else:
self._process_and_upload_trace(trace)
Expand Down Expand Up @@ -1247,8 +1249,9 @@ def _end_step(
if is_root_step and has_standalone_trace and not self._has_external_trace:
trace = self._traces_by_root.pop(run_id)
if tracer._configured_background_publish_enabled:
ctx = contextvars.copy_context()
tracer._get_background_executor().submit(
self._process_and_upload_async_trace, trace
ctx.run, self._process_and_upload_async_trace, trace
)
else:
self._process_and_upload_async_trace(trace)
Expand Down
5 changes: 4 additions & 1 deletion src/openlayer/lib/tracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,9 +1631,12 @@ def _handle_trace_completion(

if _publish:
if _configured_background_publish_enabled:
# Submit to background thread pool
# Submit to background thread pool, copying context so that
# contextvars (user_id, session_id, etc.) are preserved.
ctx = contextvars.copy_context()
executor = _get_background_executor()
executor.submit(
ctx.run,
_upload_and_publish_trace,
current_trace,
resolved_pipeline_id,
Expand Down