Skip to content

Commit 5cae55a

Browse files
committed
added span exist check
1 parent 3adb69c commit 5cae55a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/callback_handler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def on_llm_end(
115115
) -> None:
116116
span = self.span_manager.get_span(run_id)
117117

118+
if span is None:
119+
# If the span does not exist, we cannot set attributes or end it
120+
return
121+
118122
finish_reasons: List[str] = []
119123
for generation in getattr(response, "generations", []): # type: ignore
120124
for chat_generation in generation:
@@ -175,6 +179,9 @@ def on_llm_error(
175179

176180
def _handle_error(self, error: BaseException, run_id: UUID):
177181
span = self.span_manager.get_span(run_id)
182+
if span is None:
183+
# If the span does not exist, we cannot set the error status
184+
return
178185
span.set_status(Status(StatusCode.ERROR, str(error)))
179186
span.set_attribute(
180187
ErrorAttributes.ERROR_TYPE, type(error).__qualname__

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/span_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,5 @@ def end_span(self, run_id: UUID) -> None:
9898
state.span.end()
9999

100100
def get_span(self, run_id: UUID) -> Span:
101-
return self.spans[run_id].span
101+
state = self.spans.get(run_id)
102+
return state.span if state else None

0 commit comments

Comments
 (0)