fix(langchain): Set agent name as gen_ai.agent.name for chat and tool spans
#5877
5 issues
Medium
Span name can be None in new_invoke when run_name is not set - `sentry_sdk/integrations/langchain.py:960`
In new_invoke, the span name is set directly to run_name (line 960), which can be None if no run_name is configured. The old code had a fallback: f"invoke_agent {agent_name}" if agent_name else "invoke_agent". The new_stream function correctly handles this with a default span_name = "invoke_agent", but new_invoke does not. This inconsistency could result in spans with None names, causing unexpected behavior or errors in Sentry's span processing.
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
Incorrect return type annotation and docstring in _get_available_tools - `sentry_sdk/integrations/langchain.py:757-772`
The _get_available_tools function has a return type annotation of tuple[Optional[str], Optional[List[Any]]] but actually returns only tools (a single value, not a tuple). The docstring also incorrectly says 'Get the agent name and available tools'. This was left over from when this was _get_request_data which returned both values. This will cause type checker errors for callers expecting a tuple.
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).
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 2 | 4m 10s | $1.97 |
| find-bugs | 3 | 5m 36s | $3.02 |
| skill-scanner | 0 | 1m 45s | $0.57 |
| security-review | 0 | 3m 46s | $1.03 |
Duration: 15m 17s · Tokens: 4.1M in / 42.2k out · Cost: $6.62 (+extraction: $0.01, +merge: $0.00, +fix_gate: $0.01, +dedup: $0.00)