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: code-review completed Mar 31, 2026 in 3m 39s

2 issues

code-review: Found 2 issues (1 high, 1 medium)

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.


Duration: 3m 37s · Tokens: 889.9k in / 12.2k out · Cost: $1.52 (+merge: $0.00)

Annotations

Check failure on line 792 in sentry_sdk/integrations/langchain.py

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: code-review

UnboundLocalError when exception occurs in _get_run_name

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.

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

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: code-review

GEN_AI_AGENT_NAME not set when using run_name as fallback in new_stream

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.