Skip to content

Commit 3b147f8

Browse files
committed
prevent double instrumentation of llm spans
1 parent dfc0967 commit 3b147f8

File tree

1 file changed

+16
-0
lines changed
  • python/instrumentation/openinference-instrumentation-smolagents/src/openinference/instrumentation/smolagents

1 file changed

+16
-0
lines changed

python/instrumentation/openinference-instrumentation-smolagents/src/openinference/instrumentation/smolagents/_wrappers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ def __call__(
331331
) -> Any:
332332
if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
333333
return wrapped(*args, **kwargs)
334+
335+
if _has_active_llm_parent_span():
336+
return wrapped(*args, **kwargs)
337+
334338
arguments = _bind_arguments(wrapped, *args, **kwargs)
335339
span_name = f"{instance.__class__.__name__}.generate"
336340
model = instance
@@ -423,6 +427,18 @@ def _output_value_and_mime_type_for_tool_span(
423427
# TODO: handle other types
424428

425429

430+
def _has_active_llm_parent_span() -> bool:
431+
"""
432+
Returns true if there is a currently actively LLM span.
433+
"""
434+
current_span = trace_api.get_current_span()
435+
return (
436+
current_span.is_recording
437+
and current_span.get_span_context().is_valid
438+
and current_span.attributes.get(OPENINFERENCE_SPAN_KIND) == LLM
439+
)
440+
441+
426442
# span attributes
427443
INPUT_MIME_TYPE = SpanAttributes.INPUT_MIME_TYPE
428444
INPUT_VALUE = SpanAttributes.INPUT_VALUE

0 commit comments

Comments
 (0)