Skip to content

Commit 6abe578

Browse files
feat(langchain): Record run_name in on_tool_start (#5925)
Set the `run_name` LangChain attribute as the `gen_ai.function_id` attribute in `on_tool_start`.
1 parent 3559ee9 commit 6abe578

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

sentry_sdk/integrations/langchain.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ def on_tool_start(
641641
SPANDATA.GEN_AI_AGENT_NAME, agent_metadata["lc_agent_name"]
642642
)
643643

644+
run_name = kwargs.get("name")
645+
if run_name:
646+
span.set_data(
647+
SPANDATA.GEN_AI_FUNCTION_ID,
648+
run_name,
649+
)
650+
644651
if should_send_default_pii() and self.include_prompts:
645652
set_data_normalized(
646653
span,

tests/integrations/langchain/test_langchain.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ def test_langchain_chat_with_run_name(
233233
assert chat_spans[0]["data"][SPANDATA.GEN_AI_FUNCTION_ID] == "my-snazzy-pipeline"
234234

235235

236+
def test_langchain_tool_call_with_run_name(
237+
sentry_init,
238+
capture_events,
239+
):
240+
sentry_init(
241+
integrations=[
242+
LangchainIntegration(
243+
include_prompts=True,
244+
)
245+
],
246+
traces_sample_rate=1.0,
247+
send_default_pii=True,
248+
)
249+
events = capture_events()
250+
251+
with start_transaction():
252+
get_word_length.invoke(
253+
{"word": "eudca"},
254+
config={"run_name": "my-snazzy-pipeline"},
255+
)
256+
257+
tx = events[0]
258+
tool_spans = list(x for x in tx["spans"] if x["op"] == "gen_ai.execute_tool")
259+
assert len(tool_spans) == 1
260+
assert tool_spans[0]["data"][SPANDATA.GEN_AI_FUNCTION_ID] == "my-snazzy-pipeline"
261+
262+
236263
@pytest.mark.skipif(
237264
LANGCHAIN_VERSION < (1,),
238265
reason="LangChain 1.0+ required (ONE AGENT refactor)",

0 commit comments

Comments
 (0)