Skip to content

Commit

Permalink
[OPIK-809] sdk sentry track decorator extract inputs may fail (#1069)
Browse files Browse the repository at this point in the history
* Add @track unit test for incorrect function inputs, now decorator handles these cases properly

* Fix lint errors

* Fix test name
  • Loading branch information
alexkuzmik authored Jan 17, 2025
1 parent 40dc733 commit 133b8a6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
13 changes: 9 additions & 4 deletions sdks/python/src/opik/decorator/inspect_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ def extract_inputs(
) -> Dict[str, Any]:
sig = inspect.signature(func)

bound_args = sig.bind(*args, **kwargs) # type: ignore
bound_args.apply_defaults()

arg_dict = dict(bound_args.arguments)
try:
bound_args = sig.bind(*args, **kwargs) # type: ignore
bound_args.apply_defaults()
arg_dict = dict(bound_args.arguments)
except TypeError:
arg_dict = {
"args": args,
"kwargs": kwargs,
}

if "self" in arg_dict:
arg_dict.pop("self")
Expand Down
47 changes: 47 additions & 0 deletions sdks/python/tests/unit/decorator/test_tracker_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,3 +1255,50 @@ def f():

assert len(fake_backend.trace_trees) == 1
assert_equal(EXPECTED_TRACE_TREE, fake_backend.trace_trees[0])


def test_track__function_called_with_wrong_arguments__trace_is_still_created_with_attached_type_error__inputs_captured_in_another_format(
fake_backend,
):
@tracker.track
def f(x):
return "the-output"

with pytest.raises(TypeError):
f(y=5)

tracker.flush_tracker()

EXPECTED_TRACE_TREE = TraceModel(
id=ANY_BUT_NONE,
name="f",
input={"args": tuple(), "kwargs": {"y": 5}},
output=None,
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
error_info={
"exception_type": "TypeError",
"traceback": ANY_STRING(),
"message": ANY_STRING(),
},
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="f",
input={"args": tuple(), "kwargs": {"y": 5}},
output=None,
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
error_info={
"exception_type": "TypeError",
"traceback": ANY_STRING(),
"message": ANY_STRING(),
},
spans=[],
)
],
)

assert len(fake_backend.trace_trees) == 1

assert_equal(EXPECTED_TRACE_TREE, fake_backend.trace_trees[0])

0 comments on commit 133b8a6

Please sign in to comment.