Skip to content

Commit 91776d8

Browse files
committed
feat(gen_ai): add function set_conversation_id and managing functions on the Scope and apply it on the Span on .finish()
1 parent 33ba1d2 commit 91776d8

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

sentry_sdk/ai/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,11 @@ def truncate_and_annotate_messages(
697697
scope._gen_ai_original_message_count[span.span_id] = len(messages)
698698

699699
return truncated_messages
700+
701+
702+
def set_conversation_id(conversation_id: str) -> None:
703+
"""
704+
Set the conversation_id in the scope.
705+
"""
706+
scope = sentry_sdk.get_current_scope()
707+
scope.set_conversation_id(conversation_id)

sentry_sdk/scope.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class Scope:
221221
"_breadcrumbs",
222222
"_n_breadcrumbs_truncated",
223223
"_gen_ai_original_message_count",
224+
"_gen_ai_conversation_id",
224225
"_event_processors",
225226
"_error_processors",
226227
"_should_capture",
@@ -303,6 +304,8 @@ def __copy__(self) -> "Scope":
303304

304305
rv._attributes = self._attributes.copy()
305306

307+
rv._gen_ai_conversation_id = self._gen_ai_conversation_id
308+
306309
return rv
307310

308311
@classmethod
@@ -720,6 +723,8 @@ def clear(self) -> None:
720723

721724
self._attributes: "Attributes" = {}
722725

726+
self._gen_ai_conversation_id: "Optional[str]" = None
727+
723728
@_attr_setter
724729
def level(self, value: "LogLevelStr") -> None:
725730
"""
@@ -912,6 +917,26 @@ def remove_extra(
912917
"""Removes a specific extra key."""
913918
self._extras.pop(key, None)
914919

920+
def set_conversation_id(self, conversation_id: str) -> None:
921+
"""
922+
Sets the conversation ID for gen_ai spans.
923+
924+
:param conversation_id: The conversation ID to set.
925+
"""
926+
self._gen_ai_conversation_id = conversation_id
927+
928+
def get_conversation_id(self) -> "Optional[str]":
929+
"""
930+
Gets the conversation ID for gen_ai spans.
931+
932+
:returns: The conversation ID, or None if not set.
933+
"""
934+
return self._gen_ai_conversation_id
935+
936+
def remove_conversation_id(self) -> None:
937+
"""Removes the conversation ID."""
938+
self._gen_ai_conversation_id = None
939+
915940
def clear_breadcrumbs(self) -> None:
916941
"""Clears breadcrumb buffer."""
917942
self._breadcrumbs: "Deque[Breadcrumb]" = deque()

sentry_sdk/tracing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,12 @@ def finish(
676676
self.timestamp = datetime.now(timezone.utc)
677677

678678
scope = scope or sentry_sdk.get_current_scope()
679+
680+
# Copy conversation_id from scope to span data if this is a gen_ai span
681+
conversation_id = scope.get_conversation_id()
682+
if conversation_id and any(key.startswith("gen_ai.") for key in self._data):
683+
self.set_data("gen_ai.conversation.id", conversation_id)
684+
679685
maybe_create_breadcrumbs_from_span(scope, self)
680686

681687
return None

0 commit comments

Comments
 (0)