Skip to content

Commit abdde71

Browse files
fix(closes OPEN-8437): undefined 'root_step' in LangChain callback handler
1 parent 04c5df6 commit abdde71

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/openlayer/lib/integrations/langchain_callback.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22

33
# pylint: disable=unused-argument
44
import time
5-
from typing import Any, Dict, List, Optional, Union, Callable
5+
from typing import Any, Callable, Dict, List, Optional, Union
66
from uuid import UUID
77

88
try:
99
try:
1010
from langchain_core import messages as langchain_schema
11-
from langchain_core.callbacks.base import BaseCallbackHandler, AsyncCallbackHandler
11+
from langchain_core.callbacks.base import (
12+
AsyncCallbackHandler,
13+
BaseCallbackHandler,
14+
)
1215
except ImportError:
1316
from langchain import schema as langchain_schema
14-
from langchain.callbacks.base import BaseCallbackHandler, AsyncCallbackHandler
17+
from langchain.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler
1518

1619
HAVE_LANGCHAIN = True
1720
except ImportError:
1821
HAVE_LANGCHAIN = False
1922

2023

21-
from ..tracing import tracer, steps, traces, enums
2224
from .. import utils
25+
from ..tracing import enums, steps, tracer, traces
2326

2427
LANGCHAIN_TO_OPENLAYER_PROVIDER_MAP = {
2528
"azure-openai-chat": "Azure",
@@ -156,7 +159,11 @@ def _end_step(
156159

157160
# Only upload if this is a standalone trace (not integrated with external trace)
158161
# If current_step is set, we're part of a larger trace and shouldn't upload
159-
if is_root_step and run_id in self._traces_by_root and tracer.get_current_step() is None:
162+
if (
163+
is_root_step
164+
and run_id in self._traces_by_root
165+
and tracer.get_current_step() is None
166+
):
160167
trace = self._traces_by_root.pop(run_id)
161168
self._process_and_upload_trace(trace)
162169

@@ -187,8 +194,11 @@ def _process_and_upload_trace(self, trace: traces.Trace) -> None:
187194
config.update({"ground_truth_column_name": "groundTruth"})
188195
if "context" in trace_data:
189196
config.update({"context_column_name": "context"})
197+
198+
root_step = trace.steps[0] if trace.steps else None
190199
if (
191-
isinstance(root_step, steps.ChatCompletionStep)
200+
root_step
201+
and isinstance(root_step, steps.ChatCompletionStep)
192202
and root_step.inputs
193203
and "prompt" in root_step.inputs
194204
):
@@ -1107,7 +1117,7 @@ def _start_step(
11071117
# Check if we're in an existing trace context via ContextVars
11081118
current_step = tracer.get_current_step()
11091119
current_trace = tracer.get_current_trace()
1110-
1120+
11111121
if current_step is not None:
11121122
# We're inside an existing step context - add as nested
11131123
current_step.add_nested_step(step)
@@ -1128,7 +1138,7 @@ def _start_step(
11281138
trace = traces.Trace()
11291139
trace.add_step(step)
11301140
self._traces_by_root[run_id] = trace
1131-
1141+
11321142
# Track root steps
11331143
if parent_run_id is None:
11341144
self.root_steps.add(run_id)
@@ -1176,7 +1186,7 @@ def _end_step(
11761186

11771187
# Only upload if this is a standalone trace (not integrated with external trace)
11781188
has_standalone_trace = run_id in self._traces_by_root
1179-
1189+
11801190
# Only upload if: root step + has standalone trace + not part of external trace
11811191
if is_root_step and has_standalone_trace and not self._has_external_trace:
11821192
trace = self._traces_by_root.pop(run_id)

0 commit comments

Comments
 (0)