Skip to content

Commit a6eed59

Browse files
committed
Tool input and output
1 parent 7c1076b commit a6eed59

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
logger,
2121
match_regex_list,
2222
qualname_from_function,
23+
safe_repr,
2324
to_string,
2425
try_convert,
2526
is_sentry_url,
@@ -876,19 +877,15 @@ def _get_span_op(template):
876877
return op
877878

878879

879-
def _get_input_attributes(template, kwargs):
880-
# type: (Union[str, "SpanTemplate"], dict[str, Any]) -> dict[str, Any]
880+
def _get_input_attributes(template, args, kwargs):
881+
# type: (Union[str, "SpanTemplate"], tuple[Any, ...], dict[str, Any]) -> dict[str, Any]
881882
"""
882883
Get input attributes for the given span template.
883884
"""
884885
attributes = {} # type: dict[str, Any]
885886

886-
for key, value in list(kwargs.items()):
887-
if template in [
888-
SpanTemplate.AI_AGENT,
889-
SpanTemplate.AI_TOOL,
890-
SpanTemplate.AI_CHAT,
891-
]:
887+
if template in [SpanTemplate.AI_AGENT, SpanTemplate.AI_TOOL, SpanTemplate.AI_CHAT]:
888+
for key, value in list(kwargs.items()):
892889
if key == "model" and isinstance(value, str):
893890
attributes[SPANDATA.GEN_AI_REQUEST_MODEL] = value
894891
elif key == "model_name" and isinstance(value, str):
@@ -929,6 +926,11 @@ def _get_input_attributes(template, kwargs):
929926
elif key == "top_k" and isinstance(value, int):
930927
attributes.setdefault(SPANDATA.GEN_AI_REQUEST_TOP_K, []).append(value)
931928

929+
if template == SpanTemplate.AI_TOOL:
930+
attributes[SPANDATA.GEN_AI_TOOL_INPUT] = safe_repr(
931+
{"args": args, "kwargs": kwargs}
932+
)
933+
932934
return attributes
933935

934936

@@ -984,6 +986,9 @@ def _get_output_attributes(template, result):
984986
elif hasattr(result, "model_name") and isinstance(result.model_name, str):
985987
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result.model_name
986988

989+
if template == SpanTemplate.AI_TOOL:
990+
attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = safe_repr(result)
991+
987992
return attributes
988993

989994

@@ -1019,7 +1024,7 @@ def _set_input_attributes(span, template, name, f, args, kwargs):
10191024
if docstring is not None:
10201025
attributes[SPANDATA.GEN_AI_TOOL_DESCRIPTION] = docstring
10211026

1022-
attributes.update(_get_input_attributes(template, kwargs))
1027+
attributes.update(_get_input_attributes(template, args, kwargs))
10231028
span.set_data(attributes)
10241029

10251030

@@ -1114,7 +1119,7 @@ def sync_wrapper(*args, **kwargs):
11141119
function_name = name or qualname_from_function(f) or ""
11151120
with start_span_func(
11161121
op=_get_span_op(template),
1117-
name=_get_span_name(template, function_name),
1122+
name=_get_span_name(template, function_name, kwargs),
11181123
) as span:
11191124
_set_input_attributes(span, template, function_name, f, args, kwargs)
11201125

0 commit comments

Comments
 (0)