fix(langchain): Set agent name as gen_ai.agent.name for chat and tool spans
#5877
3 issues
find-bugs: Found 3 issues (2 medium, 1 low)
Medium
AttributeError when metadata is explicitly None - `sentry_sdk/integrations/langchain.py:429`
The expression kwargs.get("metadata", {}).get("lc_agent_name") will raise AttributeError if metadata is explicitly passed as None. The default value {} only applies when the key is absent, not when it's present with value None. While this is caught by capture_internal_exceptions(), it causes the entire callback body to exit early, skipping subsequent span data setup for DATA_FIELDS, tools, and request messages.
Also found at:
sentry_sdk/integrations/langchain.py:960
Incorrect return type annotation in _get_available_tools - `sentry_sdk/integrations/langchain.py:757`
The function _get_available_tools has an incorrect return type annotation. It declares -> "tuple[Optional[str], Optional[List[Any]]]" (tuple of agent name and tools), but the function only returns tools (a single value). This was likely leftover from when the original _get_request_data function returned both values. Type checkers will report false errors when callers correctly use this function's return value as a non-tuple.
Low
Missing return type annotation in _get_run_name - `sentry_sdk/integrations/langchain.py:775`
The function _get_run_name lacks a return type annotation. This is inconsistent with other functions in the same module and makes it harder to reason about the function's behavior. The function returns Optional[str] (either the run name or None).
Duration: 5m 36s · Tokens: 2.1M in / 18.6k out · Cost: $3.03 (+extraction: $0.00, +merge: $0.00, +fix_gate: $0.01)
Annotations
Check warning on line 429 in sentry_sdk/integrations/langchain.py
sentry-warden / warden: find-bugs
AttributeError when metadata is explicitly None
The expression `kwargs.get("metadata", {}).get("lc_agent_name")` will raise `AttributeError` if `metadata` is explicitly passed as `None`. The default value `{}` only applies when the key is absent, not when it's present with value `None`. While this is caught by `capture_internal_exceptions()`, it causes the entire callback body to exit early, skipping subsequent span data setup for `DATA_FIELDS`, tools, and request messages.
Check warning on line 960 in sentry_sdk/integrations/langchain.py
sentry-warden / warden: find-bugs
[QGV-H7L] AttributeError when metadata is explicitly None (additional location)
The expression `kwargs.get("metadata", {}).get("lc_agent_name")` will raise `AttributeError` if `metadata` is explicitly passed as `None`. The default value `{}` only applies when the key is absent, not when it's present with value `None`. While this is caught by `capture_internal_exceptions()`, it causes the entire callback body to exit early, skipping subsequent span data setup for `DATA_FIELDS`, tools, and request messages.
Check warning on line 757 in sentry_sdk/integrations/langchain.py
sentry-warden / warden: find-bugs
Incorrect return type annotation in _get_available_tools
The function `_get_available_tools` has an incorrect return type annotation. It declares `-> "tuple[Optional[str], Optional[List[Any]]]"` (tuple of agent name and tools), but the function only returns `tools` (a single value). This was likely leftover from when the original `_get_request_data` function returned both values. Type checkers will report false errors when callers correctly use this function's return value as a non-tuple.