diff --git a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py index 28315b7f10..dc12735498 100644 --- a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py +++ b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py @@ -472,6 +472,14 @@ def _create_llm_span( # If context setting fails, continue without suppression token token = None + # Detach orphaned context tokens before overwriting (see #3957). + existing = self.spans.get(run_id) + if existing is not None: + if existing.token: + self._safe_detach_context(existing.token) + if getattr(existing, "association_properties_token", None): + self._safe_detach_context(existing.association_properties_token) + self.spans[run_id] = SpanHolder( span, token, None, [], workflow_name, None, entity_path )