Skip to content

Commit fad4e06

Browse files
committed
agent span
1 parent 3d89081 commit fad4e06

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

sentry_sdk/integrations/langchain.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,17 @@ def new_invoke(self, *args, **kwargs):
723723
except Exception:
724724
agent_name = ""
725725

726+
agent = getattr(self, "agent", None)
727+
runnable = getattr(agent, "runnable", None)
728+
runnable_config = getattr(runnable, "config", {})
729+
# llm = getattr(self, "llm", None) or getattr(agent, "llm", None)
730+
tools = (
731+
getattr(self, "tools", None)
732+
or getattr(agent, "tools", None)
733+
or runnable_config.get("tools")
734+
or runnable_config.get("available_tools")
735+
)
736+
726737
# Create a span that will act as the parent for all callback-generated spans
727738
with sentry_sdk.start_span(
728739
op=OP.GEN_AI_INVOKE_AGENT,
@@ -735,17 +746,11 @@ def new_invoke(self, *args, **kwargs):
735746
if agent_name != "":
736747
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)
737748

738-
model_name = ""
739-
if hasattr(self, "agent") and hasattr(self.agent, "llm"):
740-
model_name = (
741-
getattr(self.agent.llm, "model_name", None)
742-
or getattr(self.agent.llm, "model", None)
743-
or ""
749+
if tools is not None and len(tools) > 0:
750+
set_data_normalized(
751+
span, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, tools, unpack=False
744752
)
745753

746-
if model_name != "":
747-
span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
748-
749754
result = f(self, *args, **kwargs)
750755

751756
input = result.get("input")
@@ -786,6 +791,17 @@ def new_stream(self, *args, **kwargs):
786791
except Exception:
787792
agent_name = ""
788793

794+
agent = getattr(self, "agent", None)
795+
runnable = getattr(agent, "runnable", None)
796+
runnable_config = getattr(runnable, "config", {})
797+
# llm = getattr(self, "llm", None) or getattr(agent, "llm", None)
798+
tools = (
799+
getattr(self, "tools", None)
800+
or getattr(agent, "tools", None)
801+
or runnable_config.get("tools")
802+
or runnable_config.get("available_tools")
803+
)
804+
789805
# Create a span that will act as the parent for all callback-generated spans
790806
span = sentry_sdk.start_span(
791807
op=OP.GEN_AI_INVOKE_AGENT,
@@ -800,6 +816,11 @@ def new_stream(self, *args, **kwargs):
800816
if agent_name != "":
801817
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)
802818

819+
if tools is not None and len(tools) > 0:
820+
set_data_normalized(
821+
span, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, tools, unpack=False
822+
)
823+
803824
input = args[0].get("input") if len(args) > 1 else None
804825
if input is not None:
805826
set_data_normalized(
@@ -810,17 +831,6 @@ def new_stream(self, *args, **kwargs):
810831
],
811832
)
812833

813-
model_name = ""
814-
if hasattr(self, "agent") and hasattr(self.agent, "llm"):
815-
model_name = (
816-
getattr(self.agent.llm, "model_name", None)
817-
or getattr(self.agent.llm, "model", None)
818-
or ""
819-
)
820-
821-
if model_name != "":
822-
span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
823-
824834
result = f(self, *args, **kwargs)
825835
old_iterator = result
826836

@@ -840,6 +850,10 @@ async def new_iterator_async():
840850
async for event in old_iterator:
841851
yield event
842852

853+
output = event.get("output")
854+
if output is not None:
855+
span.set_data(SPANDATA.GEN_AI_RESPONSE_TEXT, output)
856+
843857
span.__exit__(None, None, None)
844858

845859
if str(type(result)) == "<class 'async_generator'>":

0 commit comments

Comments
 (0)