fix(langchain): Set agent name as gen_ai.agent.name for chat and tool spans
#5877
3 issues
Medium
new_stream does not set GEN_AI_AGENT_NAME when agent_name is None but run_name exists - `sentry_sdk/integrations/langchain.py:1032-1033`
In new_stream, when agent_name from metadata is None but run_name exists, the span name correctly falls back to run_name (line 1022-1023), but GEN_AI_AGENT_NAME data attribute is never set (line 1032-1033 only checks agent_name is not None). This differs from new_invoke which sets GEN_AI_AGENT_NAME to run_name when truthy. Users calling stream() without explicit metadata but with a configured run_name will have inconsistent telemetry - the span name shows the agent name but the data attribute doesn't.
Low
AttributeError if metadata=None prevents subsequent span data from being set - `sentry_sdk/integrations/langchain.py:429`
If langchain passes metadata=None explicitly in kwargs, kwargs.get("metadata", {}) returns None (not {}), causing .get("lc_agent_name") to raise AttributeError. While capture_internal_exceptions() suppresses this, the remaining span data (DATA_FIELDS, tools, prompts/messages) will not be recorded since they're in the same with block. The safer pattern is (kwargs.get("metadata") or {}).get("lc_agent_name").
GEN_AI_AGENT_NAME not set when using run_name in stream function - `sentry_sdk/integrations/langchain.py:1032-1033`
In new_stream, when agent_name (from metadata) is None but run_name is available, the span name correctly uses run_name (line 1022-1023), but GEN_AI_AGENT_NAME is not set (lines 1032-1033 only check agent_name is not None). This is inconsistent with new_invoke which sets GEN_AI_AGENT_NAME when run_name is set (lines 963-964). Users expecting consistent agent name data between invoke and stream will not see the agent name in stream spans when using run_name configuration.
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 1 | 4m 6s | $1.62 |
| find-bugs | 2 | 6m 20s | $2.82 |
| skill-scanner | 0 | 2m 32s | $0.53 |
| security-review | 0 | 2m 4s | $0.94 |
Duration: 15m 1s · Tokens: 4.0M in / 41.5k out · Cost: $5.94 (+extraction: $0.01, +dedup: $0.00, +merge: $0.00, +fix_gate: $0.01)