Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def _end_span(self, span: Span, run_id: UUID) -> None:
token = self.spans[run_id].token
if token:
self._safe_detach_context(token)
assoc_token = self.spans[run_id].association_properties_token
if assoc_token:
self._safe_detach_context(assoc_token)

del self.spans[run_id]

Expand Down Expand Up @@ -266,6 +269,7 @@ def _create_span(
entity_path: str = "",
metadata: Optional[dict[str, Any]] = None,
) -> Span:
association_properties_token = None
if metadata is not None:
current_association_properties = (
context_api.get_value("association_properties") or {}
Expand All @@ -277,7 +281,7 @@ def _create_span(
if v is not None
}
try:
context_api.attach(
association_properties_token = context_api.attach(
context_api.set_value(
"association_properties",
{**current_association_properties, **sanitized_metadata},
Expand All @@ -286,7 +290,7 @@ def _create_span(
except Exception:
# If setting association properties fails, continue without them
# This doesn't affect the core span functionality
pass
association_properties_token = None

if parent_run_id is not None and parent_run_id in self.spans:
span = self.tracer.start_span(
Expand Down Expand Up @@ -330,7 +334,14 @@ def _create_span(
)

self.spans[run_id] = SpanHolder(
span, token, None, [], workflow_name, entity_name, entity_path
span,
token,
None,
[],
workflow_name,
entity_name,
entity_path,
association_properties_token=association_properties_token,
)

if parent_run_id is not None and parent_run_id in self.spans:
Expand Down Expand Up @@ -599,16 +610,6 @@ def on_chain_end(
span.set_attribute(SpanAttributes.GEN_AI_TASK_OUTPUT, output_json)

self._end_span(span, run_id)
if parent_run_id is None:
try:
context_api.attach(
context_api.set_value(
SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY, False
)
)
except Exception:
# If context reset fails, it's not critical for functionality
pass

@dont_throw
def on_chat_model_start(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SpanHolder:
entity_path: str
start_time: float = field(default_factory=time.time)
request_model: Optional[str] = None
association_properties_token: Any = None


def _message_type_to_role(message_type: str) -> str:
Expand Down