Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
156 changes: 58 additions & 98 deletions sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextvars
import itertools
import sys
import json
Expand Down Expand Up @@ -162,44 +161,6 @@
return content


# Contextvar to track agent names in a stack for re-entrant agent support
_agent_stack: "contextvars.ContextVar[Optional[List[Optional[str]]]]" = (
contextvars.ContextVar("langchain_agent_stack", default=None)
)


def _push_agent(agent_name: "Optional[str]") -> None:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to be getting rid of this code 🔥

"""Push an agent name onto the stack."""
stack = _agent_stack.get()
if stack is None:
stack = []
else:
# Copy the list to maintain contextvar isolation across async contexts
stack = stack.copy()
stack.append(agent_name)
_agent_stack.set(stack)


def _pop_agent() -> "Optional[str]":
"""Pop an agent name from the stack and return it."""
stack = _agent_stack.get()
if stack:
# Copy the list to maintain contextvar isolation across async contexts
stack = stack.copy()
agent_name = stack.pop()
_agent_stack.set(stack)
return agent_name
return None


def _get_current_agent() -> "Optional[str]":
"""Get the current agent name (top of stack) without removing it."""
stack = _agent_stack.get()
if stack:
return stack[-1]
return None


def _get_system_instructions(messages: "List[List[BaseMessage]]") -> "List[str]":
system_instructions = []

Expand Down Expand Up @@ -465,9 +426,11 @@
if ai_system:
span.set_data(SPANDATA.GEN_AI_SYSTEM, ai_system)

agent_name = _get_current_agent()
if agent_name:
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)
agent_metadata = kwargs.get("metadata")
if isinstance(agent_metadata, dict) and "lc_agent_name" in agent_metadata:
span.set_data(
SPANDATA.GEN_AI_AGENT_NAME, agent_metadata["lc_agent_name"]
)

for key, attribute in DATA_FIELDS.items():
if key in all_params and all_params[key] is not None:
Expand Down Expand Up @@ -665,9 +628,11 @@
if tool_description is not None:
span.set_data(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_description)

agent_name = _get_current_agent()
if agent_name:
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)
agent_metadata = kwargs.get("metadata")
if isinstance(agent_metadata, dict) and "lc_agent_name" in agent_metadata:
span.set_data(
SPANDATA.GEN_AI_AGENT_NAME, agent_metadata["lc_agent_name"]
)

if should_send_default_pii() and self.include_prompts:
set_data_normalized(
Expand Down Expand Up @@ -793,9 +758,7 @@
span.set_data(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens)


def _get_request_data(
obj: "Any", args: "Any", kwargs: "Any"
) -> "tuple[Optional[str], Optional[List[Any]]]":
def _get_available_tools(obj: "Any") -> "Optional[List[Any]]":
"""
Get the agent name and available tools for the agent.
"""
Expand All @@ -810,6 +773,13 @@
)
tools = tools if tools and len(tools) > 0 else None

return tools


def _get_run_name(obj: "Any", args: "Any") -> "Optional[str]":
agent = getattr(obj, "agent", None)
runnable = getattr(agent, "runnable", None)
runnable_config = getattr(runnable, "config", {})
try:
agent_name = None
if len(args) > 1:
Expand All @@ -819,7 +789,7 @@
except Exception:
pass

return (agent_name, tools)
return agent_name


def _simplify_langchain_tools(tools: "Any") -> "Optional[List[Any]]":
Expand Down Expand Up @@ -987,58 +957,53 @@
if integration is None:
return f(self, *args, **kwargs)

agent_name, tools = _get_request_data(self, args, kwargs)
start_span_function = get_start_span_function()

run_name = _get_run_name(self, args)
with start_span_function(
op=OP.GEN_AI_INVOKE_AGENT,
name=f"invoke_agent {agent_name}" if agent_name else "invoke_agent",
name=f"invoke_agent {run_name}" if run_name else "invoke_agent",
origin=LangchainIntegration.origin,
) as span:
_push_agent(agent_name)
try:
if agent_name:
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)
if run_name:
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, run_name)

span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent")
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, False)
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent")
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, False)

_set_tools_on_span(span, tools)
tools = _get_available_tools(self)
_set_tools_on_span(span, tools)

# Run the agent
result = f(self, *args, **kwargs)
# Run the agent
result = f(self, *args, **kwargs)

input = result.get("input")
if (
input is not None
and should_send_default_pii()
and integration.include_prompts
):
normalized_messages = normalize_message_roles([input])
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(
normalized_messages, span, scope
input = result.get("input")
if (
input is not None
and should_send_default_pii()
and integration.include_prompts
):
normalized_messages = normalize_message_roles([input])
scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(
normalized_messages, span, scope
)
if messages_data is not None:
set_data_normalized(
span,
SPANDATA.GEN_AI_REQUEST_MESSAGES,
messages_data,
unpack=False,
)
if messages_data is not None:
set_data_normalized(
span,
SPANDATA.GEN_AI_REQUEST_MESSAGES,
messages_data,
unpack=False,
)

output = result.get("output")
if (
output is not None
and should_send_default_pii()
and integration.include_prompts
):
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output)
output = result.get("output")
if (
output is not None
and should_send_default_pii()
and integration.include_prompts
):
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output)

return result
finally:
# Ensure agent is popped even if an exception occurs
_pop_agent()
return result

return new_invoke

Expand All @@ -1050,25 +1015,22 @@
if integration is None:
return f(self, *args, **kwargs)

agent_name, tools = _get_request_data(self, args, kwargs)
start_span_function = get_start_span_function()

run_name = _get_run_name(self, args)

span = start_span_function(
op=OP.GEN_AI_INVOKE_AGENT,
name=f"invoke_agent {agent_name}" if agent_name else "invoke_agent",
name=f"invoke_agent {run_name}" if run_name else "invoke_agent",
origin=LangchainIntegration.origin,
)
span.__enter__()

_push_agent(agent_name)

if agent_name:
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)

span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent")
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)

Check warning on line 1030 in sentry_sdk/integrations/langchain.py

View check run for this annotation

@sentry/warden / warden: code-review

GEN_AI_AGENT_NAME not set in new_stream function

The `new_stream` function retrieves `run_name` at line 1020 but never sets `SPANDATA.GEN_AI_AGENT_NAME` on the span, unlike `new_invoke` which correctly sets it (line 967-968). This is an oversight in the refactoring - the old code had `span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)` which was removed but not replaced. This breaks parity between streaming and non-streaming agent invocations and conflicts with the PR's stated goal to set agent name as `gen_ai.agent.name`.

tools = _get_available_tools(self)
_set_tools_on_span(span, tools)

Check warning on line 1033 in sentry_sdk/integrations/langchain.py

View check run for this annotation

@sentry/warden / warden: find-bugs

Agent name (gen_ai.agent.name) not set on span in _wrap_agent_executor_stream

In the stream function `_wrap_agent_executor_stream`, the `run_name` is obtained via `_get_run_name()` (line 1020) and used in the span name (line 1024), but unlike `_wrap_agent_executor_invoke` (which sets `SPANDATA.GEN_AI_AGENT_NAME` on lines 967-968), the stream function never sets this span data. This creates inconsistent telemetry behavior between invoke and stream operations - streaming agents will not have the `gen_ai.agent.name` attribute set on their spans. This directly contradicts the PR title which claims to 'Set agent name as gen_ai.agent.name'.

input = args[0].get("input") if len(args) >= 1 else None
if (
Expand Down Expand Up @@ -1117,7 +1079,6 @@
raise
finally:
# Ensure cleanup happens even if iterator is abandoned or fails
_pop_agent()
span.__exit__(*exc_info)

async def new_iterator_async() -> "AsyncIterator[Any]":
Expand All @@ -1143,7 +1104,6 @@
raise
finally:
# Ensure cleanup happens even if iterator is abandoned or fails
_pop_agent()
span.__exit__(*exc_info)

if str(type(result)) == "<class 'async_generator'>":
Expand Down
6 changes: 6 additions & 0 deletions tests/integrations/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def test_langchain_create_agent(
assert chat_spans[0]["origin"] == "auto.ai.langchain"

assert chat_spans[0]["data"]["gen_ai.system"] == "openai-chat"
assert chat_spans[0]["data"]["gen_ai.agent.name"] == "word_length_agent"

assert chat_spans[0]["data"]["gen_ai.usage.input_tokens"] == 10
assert chat_spans[0]["data"]["gen_ai.usage.output_tokens"] == 20
assert chat_spans[0]["data"]["gen_ai.usage.total_tokens"] == 30
Expand Down Expand Up @@ -415,6 +417,10 @@ def test_tool_execution_span(
assert chat_spans[1]["origin"] == "auto.ai.langchain"
assert tool_exec_span["origin"] == "auto.ai.langchain"

assert chat_spans[0]["data"]["gen_ai.agent.name"] == "word_length_agent"
assert chat_spans[1]["data"]["gen_ai.agent.name"] == "word_length_agent"
assert tool_exec_span["data"]["gen_ai.agent.name"] == "word_length_agent"

assert chat_spans[0]["data"]["gen_ai.usage.input_tokens"] == 142
assert chat_spans[0]["data"]["gen_ai.usage.output_tokens"] == 50
assert chat_spans[0]["data"]["gen_ai.usage.total_tokens"] == 192
Expand Down
Loading