From 133b8a6a81a7a7edf838d12a84e52775bfaa9340 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kuzmik <98702584+alexkuzmik@users.noreply.github.com> Date: Fri, 17 Jan 2025 11:41:42 +0100 Subject: [PATCH] [OPIK-809] sdk sentry track decorator extract inputs may fail (#1069) * Add @track unit test for incorrect function inputs, now decorator handles these cases properly * Fix lint errors * Fix test name --- .../src/opik/decorator/inspect_helpers.py | 13 +++-- .../unit/decorator/test_tracker_outputs.py | 47 +++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/sdks/python/src/opik/decorator/inspect_helpers.py b/sdks/python/src/opik/decorator/inspect_helpers.py index c3e82343c9..12ce11c20b 100644 --- a/sdks/python/src/opik/decorator/inspect_helpers.py +++ b/sdks/python/src/opik/decorator/inspect_helpers.py @@ -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") diff --git a/sdks/python/tests/unit/decorator/test_tracker_outputs.py b/sdks/python/tests/unit/decorator/test_tracker_outputs.py index 86452481c0..b8c07eceff 100644 --- a/sdks/python/tests/unit/decorator/test_tracker_outputs.py +++ b/sdks/python/tests/unit/decorator/test_tracker_outputs.py @@ -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])