Skip to content

defensive check

d8c06f8
Select commit
Loading
Failed to load commit list.
Open

fix(langchain): Set agent name as gen_ai.agent.name for chat and tool spans #5877

defensive check
d8c06f8
Select commit
Loading
Failed to load commit list.
@sentry/warden / warden completed Mar 31, 2026 in 6m 45s

4 issues

High

UnboundLocalError when exception occurs in _get_run_name - `sentry_sdk/integrations/langchain.py:783-792`

In _get_run_name, the variable agent_name is only defined inside the try block (line 784). If an exception occurs before the assignment (e.g., args doesn't support len() or indexing), the except block swallows the exception, but the return agent_name on line 792 will raise UnboundLocalError: local variable 'agent_name' referenced before assignment. This will cause the langchain integration to fail when invoking agents with unexpected argument types.

Medium

GEN_AI_AGENT_NAME not set when using run_name as fallback in new_stream - `sentry_sdk/integrations/langchain.py:1036-1037`

In new_stream, when agent_name is None but run_name is available, the span name includes run_name (line 1027) but GEN_AI_AGENT_NAME is never set (line 1036 only checks agent_name). This is inconsistent with new_invoke which sets GEN_AI_AGENT_NAME whenever run_name is available (line 967-968). This causes telemetry data to be incomplete when streaming with a run_name but no explicit agent_name.

new_invoke ignores lc_agent_name metadata unlike new_stream - `sentry_sdk/integrations/langchain.py:961-968`

The new_invoke function does not check kwargs.get('metadata', {}).get('lc_agent_name') like new_stream does. When users pass metadata={'lc_agent_name': 'MyAgent'} to invoke(), the agent name from metadata is ignored and only run_name (from args) is considered. This contradicts the PR's stated goal of setting agent name as gen_ai.agent.name and creates inconsistent behavior between invoke() and stream() methods.

Low

Misleading docstring in _get_available_tools describes removed functionality - `sentry_sdk/integrations/langchain.py:762-764`

The _get_available_tools function has a docstring that says 'Get the agent name and available tools for the agent' but after the refactoring, this function only returns tools - the agent name retrieval was moved to _get_run_name. This documentation mismatch could confuse developers maintaining this code.

4 skills analyzed
Skill Findings Duration Cost
code-review 2 3m 37s $1.52
find-bugs 2 6m 20s $3.08
skill-scanner 0 2m 43s $0.54
security-review 0 2m 23s $1.05

Duration: 15m 2s · Tokens: 3.8M in / 43.3k out · Cost: $6.21 (+merge: $0.00, +dedup: $0.01, +extraction: $0.00, +fix_gate: $0.01)