Skip to content
Draft
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
139 changes: 46 additions & 93 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:
"""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 @@ -987,58 +952,53 @@
if integration is None:
return f(self, *args, **kwargs)

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

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)

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

View check run for this annotation

@sentry/warden / warden: code-review

Inconsistent GEN_AI_AGENT_NAME handling between invoke and stream

The `new_invoke` function still sets `GEN_AI_AGENT_NAME` on the span using `run_name` (line 964-965), while `new_stream` had this code removed entirely. This creates inconsistent telemetry behavior where `invoke_agent` spans will have different attributes depending on whether `invoke()` or `stream()` is called. According to the PR description, `lc_agent_name` should be used as the agent name source through callback handlers, suggesting the setting in `new_invoke` should also be removed.
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)
_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,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agent name not set on span in stream path

Medium Severity

In _wrap_agent_executor_stream, the old code that set GEN_AI_AGENT_NAME on the span was removed along with the _push_agent call, but unlike _wrap_agent_executor_invoke (which preserved span.set_data(SPANDATA.GEN_AI_AGENT_NAME, run_name)), the stream path never sets the agent name on the agent span. This means streaming agent invocations will be missing the gen_ai.agent.name attribute on the top-level agent span.

Additional Locations (1)
Fix in Cursor Fix in Web

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will follow up and set the pipeline case in this scenario

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,24 +1010,19 @@
if integration is None:
return f(self, *args, **kwargs)

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

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 1025 in sentry_sdk/integrations/langchain.py

View check run for this annotation

@sentry/warden / warden: find-bugs

GEN_AI_AGENT_NAME not set in _wrap_agent_executor_stream

The refactoring removed the code that sets `GEN_AI_AGENT_NAME` attribute in `_wrap_agent_executor_stream`, while the equivalent code was preserved in `_wrap_agent_executor_invoke`. The old code had `if agent_name: span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)` which was not replaced when switching from `agent_name` to `run_name`. This causes inconsistent telemetry data between invoke and stream operations - stream operations will be missing the agent name attribute.
_set_tools_on_span(span, tools)

input = args[0].get("input") if len(args) >= 1 else None
Expand Down Expand Up @@ -1117,7 +1072,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 +1097,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