Skip to content

Commit 97cbcd9

Browse files
fix(langchain): changed dictionary access from spans[run_id] to spans.get(run_id) (#3403)
Co-authored-by: Shivam Patel <[email protected]>
1 parent c144774 commit 97cbcd9

File tree

1 file changed

+12
-10
lines changed
  • packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain

1 file changed

+12
-10
lines changed

packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,18 @@ def __call__(
229229
run_manager = kwargs.get("run_manager")
230230
if run_manager:
231231
run_id = run_manager.run_id
232-
span_holder = self._callback_manager.spans[run_id]
233-
234-
extra_headers = kwargs.get("extra_headers", {})
235-
236-
# Inject tracing context into the extra headers
237-
ctx = set_span_in_context(span_holder.span)
238-
TraceContextTextMapPropagator().inject(extra_headers, context=ctx)
239-
240-
# Update kwargs to include the modified headers
241-
kwargs["extra_headers"] = extra_headers
232+
span_holder = self._callback_manager.spans.get(run_id)
233+
234+
if span_holder:
235+
extra_headers = kwargs.get("extra_headers", {})
236+
ctx = set_span_in_context(span_holder.span)
237+
TraceContextTextMapPropagator().inject(extra_headers, context=ctx)
238+
kwargs["extra_headers"] = extra_headers
239+
else:
240+
logger.debug(
241+
"No span found for run_id %s, skipping header injection",
242+
run_id
243+
)
242244

243245
# In legacy chains like LLMChain, suppressing model instrumentations
244246
# within create_llm_span doesn't work, so this should helps as a fallback

0 commit comments

Comments
 (0)