Skip to content

Commit dd166a8

Browse files
committed
updated tool span
1 parent b0a2c7c commit dd166a8

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

sentry_sdk/integrations/langchain.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def on_llm_start(
257257
):
258258
# type: (SentryLangchainCallback, Dict[str, Any], List[str], UUID, Optional[List[str]], Optional[UUID], Optional[Dict[str, Any]], Any) -> Any
259259
"""Run when LLM starts running."""
260+
# import ipdb; ipdb.set_trace()
260261
with capture_internal_exceptions():
261262
if not run_id:
262263
return
@@ -286,6 +287,7 @@ def on_llm_start(
286287
def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
287288
# type: (SentryLangchainCallback, Dict[str, Any], List[List[BaseMessage]], UUID, Any) -> Any
288289
"""Run when Chat Model starts running."""
290+
# import ipdb; ipdb.set_trace()
289291
with capture_internal_exceptions():
290292
if not run_id:
291293
return
@@ -327,6 +329,7 @@ def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
327329
def on_chat_model_end(self, response, *, run_id, **kwargs):
328330
# type: (SentryLangchainCallback, LLMResult, UUID, Any) -> Any
329331
"""Run when Chat Model ends running."""
332+
# import ipdb; ipdb.set_trace()
330333
with capture_internal_exceptions():
331334
if not run_id:
332335
return
@@ -395,6 +398,7 @@ def on_llm_new_token(self, token, *, run_id, **kwargs):
395398
def on_llm_end(self, response, *, run_id, **kwargs):
396399
# type: (SentryLangchainCallback, LLMResult, UUID, Any) -> Any
397400
"""Run when LLM ends running."""
401+
# import ipdb; ipdb.set_trace()
398402
with capture_internal_exceptions():
399403
if not run_id:
400404
return
@@ -450,23 +454,27 @@ def on_llm_end(self, response, *, run_id, **kwargs):
450454
def on_llm_error(self, error, *, run_id, **kwargs):
451455
# type: (SentryLangchainCallback, Union[Exception, KeyboardInterrupt], UUID, Any) -> Any
452456
"""Run when LLM errors."""
457+
# import ipdb; ipdb.set_trace()
453458
with capture_internal_exceptions():
454459
self._handle_error(run_id, error)
455460

456461
def on_chat_model_error(self, error, *, run_id, **kwargs):
457462
# type: (SentryLangchainCallback, Union[Exception, KeyboardInterrupt], UUID, Any) -> Any
458463
"""Run when Chat Model errors."""
464+
# import ipdb; ipdb.set_trace()
459465
with capture_internal_exceptions():
460466
self._handle_error(run_id, error)
461467

462468
def on_chain_start(self, serialized, inputs, *, run_id, **kwargs):
463469
# type: (SentryLangchainCallback, Dict[str, Any], Dict[str, Any], UUID, Any) -> Any
464470
"""Run when chain starts running."""
471+
# import ipdb; ipdb.set_trace()
465472
pass
466473

467474
def on_chain_end(self, outputs, *, run_id, **kwargs):
468475
# type: (SentryLangchainCallback, Dict[str, Any], UUID, Any) -> Any
469476
"""Run when chain ends running."""
477+
# import ipdb; ipdb.set_trace()
470478
with capture_internal_exceptions():
471479
if not run_id or run_id not in self.span_map:
472480
return
@@ -479,10 +487,12 @@ def on_chain_end(self, outputs, *, run_id, **kwargs):
479487
def on_chain_error(self, error, *, run_id, **kwargs):
480488
# type: (SentryLangchainCallback, Union[Exception, KeyboardInterrupt], UUID, Any) -> Any
481489
"""Run when chain errors."""
490+
# import ipdb; ipdb.set_trace()
482491
self._handle_error(run_id, error)
483492

484493
def on_agent_action(self, action, *, run_id, **kwargs):
485494
# type: (SentryLangchainCallback, AgentAction, UUID, Any) -> Any
495+
# import ipdb; ipdb.set_trace()
486496
with capture_internal_exceptions():
487497
if not run_id:
488498
return
@@ -502,6 +512,7 @@ def on_agent_action(self, action, *, run_id, **kwargs):
502512

503513
def on_agent_finish(self, finish, *, run_id, **kwargs):
504514
# type: (SentryLangchainCallback, AgentFinish, UUID, Any) -> Any
515+
# import ipdb; ipdb.set_trace()
505516
with capture_internal_exceptions():
506517
if not run_id:
507518
return
@@ -523,28 +534,31 @@ def on_tool_start(self, serialized, input_str, *, run_id, **kwargs):
523534
with capture_internal_exceptions():
524535
if not run_id:
525536
return
537+
538+
tool_name = serialized.get("name") or kwargs.get("name")
539+
526540
watched_span = self._create_span(
527541
run_id,
528542
kwargs.get("parent_run_id"),
529543
op=OP.GEN_AI_EXECUTE_TOOL,
530-
name=serialized.get("name") or kwargs.get("name") or "AI tool usage",
544+
name=f"execute_tool {tool_name}",
531545
origin=LangchainIntegration.origin,
532546
)
533-
watched_span.span.set_data(
534-
SPANDATA.GEN_AI_TOOL_NAME, serialized.get("name")
535-
)
547+
span = watched_span.span
548+
549+
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "execute_tool")
550+
span.set_data(SPANDATA.GEN_AI_TOOL_NAME, tool_name)
551+
552+
tool_description = serialized.get("description")
553+
if tool_description is not None:
554+
span.set_data(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_description)
555+
536556
if should_send_default_pii() and self.include_prompts:
537557
set_data_normalized(
538-
watched_span.span,
539-
SPANDATA.GEN_AI_REQUEST_MESSAGES,
558+
span,
559+
SPANDATA.GEN_AI_TOOL_INPUT,
540560
kwargs.get("inputs", [input_str]),
541561
)
542-
if kwargs.get("metadata"):
543-
set_data_normalized(
544-
watched_span.span,
545-
SPANDATA.GEN_AI_REQUEST_METADATA,
546-
kwargs.get("metadata"),
547-
)
548562

549563
def on_tool_end(self, output, *, run_id, **kwargs):
550564
# type: (SentryLangchainCallback, str, UUID, Any) -> Any
@@ -557,14 +571,13 @@ def on_tool_end(self, output, *, run_id, **kwargs):
557571
if not span_data:
558572
return
559573
if should_send_default_pii() and self.include_prompts:
560-
set_data_normalized(
561-
span_data.span, SPANDATA.GEN_AI_RESPONSE_TEXT, output
562-
)
574+
set_data_normalized(span_data.span, SPANDATA.GEN_AI_TOOL_OUTPUT, output)
563575
self._exit_span(span_data, run_id)
564576

565577
def on_tool_error(self, error, *args, run_id, **kwargs):
566578
# type: (SentryLangchainCallback, Union[Exception, KeyboardInterrupt], UUID, Any) -> Any
567579
"""Run when tool errors."""
580+
# import ipdb; ipdb.set_trace()
568581
# TODO(shellmayr): how to correctly set the status when the toolfails
569582
if run_id and run_id in self.span_map:
570583
span_data = self.span_map[run_id]

0 commit comments

Comments
 (0)