Skip to content

Commit

Permalink
fix(decorators): merge trace tags and metadata on updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp committed Jan 22, 2025
1 parent d4ac214 commit f0ab24d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions langfuse/decorators/langfuse_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,18 @@ def update_current_trace(
if v is not None
}

# metadata and tags are merged server side. Send separate update event to avoid merging them SDK side
server_merged_attributes = ["metadata", "tags"]
if any(attribute in params_to_update for attribute in server_merged_attributes):
self.client_instance.trace(
id=trace_id,
**{
k: v
for k, v in params_to_update.items()
if k in server_merged_attributes
},
)

_observation_params_context.get()[trace_id].update(params_to_update)

def update_current_observation(
Expand Down
28 changes: 28 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,3 +1541,31 @@ def main():
assert parsed_reference_string["content_type"] == "application/pdf"
assert parsed_reference_string["media_id"] is not None
assert parsed_reference_string["source"] == "bytes"


def test_merge_metadata_and_tags():
mock_trace_id = create_uuid()

@observe
def nested():
langfuse_context.update_current_trace(
metadata={"key2": "value2"}, tags=["tag2"]
)

@observe
def main():
langfuse_context.update_current_trace(
metadata={"key1": "value1"}, tags=["tag1"]
)

nested()

main(langfuse_observation_id=mock_trace_id)

langfuse_context.flush()

trace_data = get_api().trace.get(mock_trace_id)

assert trace_data.metadata == {"key1": "value1", "key2": "value2"}

assert trace_data.tags == ["tag1", "tag2"]

0 comments on commit f0ab24d

Please sign in to comment.