Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_current_span now supports updating the output #709

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions sdks/python/src/opik/api_objects/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ def update(self, **new_data: Any) -> "SpanData":
if key == "metadata":
self._update_metadata(value)
continue
elif key == "output":
self._update_output(value)
continue
elif key == "input":
self._update_input(value)
continue

self.__dict__[key] = value

Expand All @@ -282,6 +288,18 @@ def _update_metadata(self, new_metadata: Dict[str, Any]) -> None:
else:
self.metadata = dict_utils.deepmerge(self.metadata, new_metadata)

def _update_output(self, new_output: Dict[str, Any]) -> None:
if self.output is None:
self.output = new_output
else:
self.output = dict_utils.deepmerge(self.output, new_output)

def _update_input(self, new_input: Dict[str, Any]) -> None:
if self.input is None:
self.input = new_input
else:
self.input = dict_utils.deepmerge(self.input, new_input)

def init_end_time(self) -> "SpanData":
self.end_time = datetime_helpers.local_timestamp()

Expand Down
18 changes: 18 additions & 0 deletions sdks/python/src/opik/api_objects/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ def update(self, **new_data: Any) -> "TraceData":
if key == "metadata":
self._update_metadata(value)
continue
elif key == "output":
self._update_output(value)
continue
elif key == "input":
self._update_input(value)
continue

self.__dict__[key] = value

Expand All @@ -253,6 +259,18 @@ def _update_metadata(self, new_metadata: Dict[str, Any]) -> None:
else:
self.metadata = dict_utils.deepmerge(self.metadata, new_metadata)

def _update_output(self, new_output: Dict[str, Any]) -> None:
if self.output is None:
self.output = new_output
else:
self.output = dict_utils.deepmerge(self.output, new_output)

def _update_input(self, new_input: Dict[str, Any]) -> None:
if self.input is None:
self.input = new_input
else:
self.input = dict_utils.deepmerge(self.input, new_input)

def init_end_time(self) -> "TraceData":
self.end_time = datetime_helpers.local_timestamp()
return self
42 changes: 42 additions & 0 deletions sdks/python/tests/unit/decorator/test_tracker_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,48 @@ def f(x):
assert_equal(EXPECTED_TRACE_TREE, fake_backend.trace_trees[0])


def test_track__span_and_trace_input_output_updated_via_opik_context(fake_backend):
@tracker.track
def f(x):
opik_context.update_current_span(
input={"span-input-key": "span-input-value"},
output={"span-output-key": "span-output-value"},
)
opik_context.update_current_trace(
input={"trace-input-key": "trace-input-value"},
output={"trace-output-key": "trace-output-value"},
)

return "f-output"

f("f-input")
tracker.flush_tracker()

EXPECTED_TRACE_TREE = TraceModel(
id=ANY_BUT_NONE,
name="f",
input={"x": "f-input", "trace-input-key": "trace-input-value"},
output={"output": "f-output", "trace-output-key": "trace-output-value"},
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="f",
input={"x": "f-input", "span-input-key": "span-input-value"},
output={"output": "f-output", "span-output-key": "span-output-value"},
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[],
)
],
)

assert len(fake_backend.trace_trees) == 1

assert_equal(EXPECTED_TRACE_TREE, fake_backend.trace_trees[0])


def test_track__span_and_trace_updated_via_opik_context_with_feedback_scores__feedback_scores_are_also_logged(
fake_backend,
):
Expand Down