Skip to content

feat(langchain): Change LLM span operation to generate_text#5705

Open
ericapisani wants to merge 3 commits intomasterfrom
ep/sdk-669-9f5
Open

feat(langchain): Change LLM span operation to generate_text#5705
ericapisani wants to merge 3 commits intomasterfrom
ep/sdk-669-9f5

Conversation

@ericapisani
Copy link
Member

@ericapisani ericapisani commented Mar 19, 2026

Update the LangChain integration to use gen_ai.generate_text as the span operation for LLM calls instead of gen_ai.pipeline. This aligns with the more specific semantics of what's happening when an LLM is invoked directly, as opposed to a broader pipeline execution.

Changes:

  • Span operation changed from OP.GEN_AI_PIPELINE to OP.GEN_AI_GENERATE_TEXT
  • Span name updated to generate_text {model} to include model info
  • GEN_AI_OPERATION_NAME span data set to "generate_text"
  • GEN_AI_PIPELINE_NAME span data set when a pipeline name is provided in kwargs

ericapisani and others added 2 commits March 19, 2026 10:57
…re agent name

Update the LangChain integration to track LLM operations as gen_ai.generate_text
instead of gen_ai.pipeline. This provides more specific operation semantics for
LLM invocations. Additionally, capture the agent name when an LLM is invoked
within an agent context using _push_agent/_pop_agent tracking.

- Change span operation from OP.GEN_AI_PIPELINE to OP.GEN_AI_GENERATE_TEXT
- Update span name to include model information
- Add GEN_AI_OPERATION_NAME span data
- Add GEN_AI_AGENT_NAME span data when agent context is active
- Add GEN_AI_PIPELINE_NAME span data when pipeline name is provided
- Update tests to check for new operation type and span data fields

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@linear-code
Copy link

linear-code bot commented Mar 19, 2026

@github-actions
Copy link
Contributor

github-actions bot commented Mar 19, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 6.91s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 0.00%. Project has 14382 uncovered lines.
✅ Project coverage is 30.24%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
langchain.py 3.26% ⚠️ 594 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    25.33%    30.24%    +4.91%
==========================================
  Files          189       189         —
  Lines        20613     20617        +4
  Branches      6738      6740        +2
==========================================
+ Hits          5222      6235     +1013
- Misses       15391     14382     -1009
- Partials       429       474       +45

Generated by Codecov Action

@github-actions
Copy link
Contributor

Codecov Results 📊

8 passed | Total: 8 | Pass Rate: 100% | Execution Time: 2.05s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -19
Passed Tests 📉 -19
Failed Tests
Skipped Tests

All tests are passing successfully.

❌ Patch coverage is 0.00%. Project has 14877 uncovered lines.
❌ Project coverage is 27.81%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
langchain.py 3.24% ⚠️ 597 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    28.33%    27.81%    -0.52%
==========================================
  Files          189       189         —
  Lines        20600     20607        +7
  Branches      6732      6736        +4
==========================================
+ Hits          5836      5730      -106
- Misses       14764     14877      +113
- Partials       545       525       -20

Generated by Codecov Action

@ericapisani ericapisani marked this pull request as ready for review March 19, 2026 11:40
@ericapisani ericapisani requested a review from a team as a code owner March 19, 2026 11:40
Comment on lines +380 to +382
agent_name = _get_current_agent()
if agent_name:
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on_llm_start can be triggered by an agent, so this adds extra information that we were previously missing (mirrors what we do for chat spans)

Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, please see my Slack message regarding the agent name 🙏

Here's the comment by the way, so it doesn't get lost

I feel like I'm missing something, but currently langchain has no agent name parameter.
We extract the run_name to set the agent name, but this is semantically wrong. The agent name is an attribute of the agent that is constant across invocations. The run_name is just the name of the specific invocation, not the name of the agent.
We should consider removing the gen_ai.agent.name attribute from all langchain spans. Sending the wrong data is worse than not sending the attribute at all.

Move agent name capture out of on_llm_start to be handled in follow-up
PRs. Remove associated tests and unused imports.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Mar 20, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (asgi) Add option to disable suppressing chained exceptions by alexander-alderman-webb in #5714
  • (langchain) Change LLM span operation to generate_text by ericapisani in #5705
  • (logging) Separate ignore lists for events/breadcrumbs and sentry logs by sl0thentr0py in #5698

Bug Fixes 🐛

Anthropic

  • Set exception info on streaming span when applicable by alexander-alderman-webb in #5683
  • Patch AsyncStream.close() and AsyncMessageStream.close() to finish spans by alexander-alderman-webb in #5675
  • Patch Stream.close() and MessageStream.close() to finish spans by alexander-alderman-webb in #5674

Documentation 📚

  • Add note on AI PRs to CONTRIBUTING.md by sentrivana in #5696

Internal Changes 🔧

  • Add -latest alias for each integration test suite by sentrivana in #5706
  • Use date-based branch names for toxgen PRs by sentrivana in #5704
  • 🤖 Update test matrix with new releases (03/19) by github-actions in #5703
  • Add client report tests for span streaming by sentrivana in #5677

Other

  • Update CHANGELOG.md by sentrivana in #5685

🤖 This preview updates automatically when you update the PR.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@ericapisani ericapisani changed the title feat(langchain): Change LLM span operation to generate_text and capture agent name feat(langchain): Change LLM span operation to generate_text Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants