Skip to content

feat(vercel-ai): Add rerank support and fix token attribute mapping#19144

Merged
sergical merged 7 commits intodevelopfrom
feat/vercel-ai-rerank-token-mapping
Feb 4, 2026
Merged

feat(vercel-ai): Add rerank support and fix token attribute mapping#19144
sergical merged 7 commits intodevelopfrom
feat/vercel-ai-rerank-token-mapping

Conversation

@sergical
Copy link
Member

@sergical sergical commented Feb 3, 2026

Summary

This PR adds support for the Vercel AI SDK rerank operation and fixes token attribute mapping to follow OpenTelemetry semantic conventions.

Changes

Core (@sentry/core):

  • Add ai.rerank span support in the Vercel AI span processor
  • Map ai.usage.inputTokens/outputTokensgen_ai.usage.input_tokens/output_tokens
  • Map ai.response.avgOutputTokensPerSecond → standardized attribute name
  • Add tests for rerank and token mapping

Node (@sentry/node):

  • Add rerank to INSTRUMENTED_METHODS to auto-enable telemetry for rerank operations
  • Add null check for Vercel AI methods before patching

Why No E2E Tests for Rerank

E2E tests for rerank functionality cannot be added due to a bug in the AI SDK's MockRerankingModelV3. When calling rerank() with the mock model, the SDK crashes with:

TypeError: Cannot read properties of undefined (reading 'total')

This happens because the AI SDK's internal telemetry code expects usage data that the mock model doesn't provide (and isn't required by the RerankingModelV3 interface). The rerank instrumentation is fully covered by unit tests instead.

See: https://github.com/vercel/ai/blob/main/packages/ai/src/test/mock-reranking-model-v3.ts

Checklist

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

Closes #19145 (added automatically)

sergical and others added 2 commits February 3, 2026 11:59
- Add support for ai.rerank and ai.rerank.doRerank spans
- Map parent span token attributes (ai.usage.inputTokens/outputTokens)
  to gen_ai.usage.input_tokens/output_tokens
- Map ai.response.avgOutputTokensPerSecond to expected attribute name

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Enable automatic telemetry for the `rerank` method in the
Node.js Vercel AI integration.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

Codecov Results 📊

535 passed | ⏭️ 51 skipped | Total: 586 | Pass Rate: 91.3% | Execution Time: 14m 35s

All tests are passing successfully.


Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 25.44 kB - -
@sentry/browser - with treeshaking flags 23.89 kB - -
@sentry/browser (incl. Tracing) 42.27 kB - -
@sentry/browser (incl. Tracing, Profiling) 46.93 kB - -
@sentry/browser (incl. Tracing, Replay) 80.9 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.5 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 85.61 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 97.79 kB - -
@sentry/browser (incl. Feedback) 42.15 kB - -
@sentry/browser (incl. sendFeedback) 30.12 kB - -
@sentry/browser (incl. FeedbackAsync) 35.12 kB - -
@sentry/browser (incl. Metrics) 26.54 kB - -
@sentry/browser (incl. Logs) 26.7 kB - -
@sentry/browser (incl. Metrics & Logs) 27.36 kB - -
@sentry/react 27.16 kB - -
@sentry/react (incl. Tracing) 44.52 kB - -
@sentry/vue 29.86 kB - -
@sentry/vue (incl. Tracing) 44.09 kB - -
@sentry/svelte 25.45 kB - -
CDN Bundle 27.97 kB - -
CDN Bundle (incl. Tracing) 43.04 kB - -
CDN Bundle (incl. Logs, Metrics) 28.81 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 43.87 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 67.76 kB - -
CDN Bundle (incl. Tracing, Replay) 79.79 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 80.67 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 85.23 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.14 kB - -
CDN Bundle - uncompressed 81.81 kB - -
CDN Bundle (incl. Tracing) - uncompressed 127.52 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 84.65 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 130.36 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 208.03 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 244.13 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 246.95 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 256.93 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 259.74 kB - -
@sentry/nextjs (client) 46.87 kB - -
@sentry/sveltekit (client) 42.66 kB - -
@sentry/node-core 52.18 kB - -
@sentry/node 166.29 kB +0.06% +99 B 🔺
@sentry/node - without tracing 93.97 kB - -
@sentry/aws-serverless 109.48 kB +0.01% +1 B 🔺

View base workflow run

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 11,466 - 8,520 +35%
GET With Sentry 2,001 17% 1,591 +26%
GET With Sentry (error only) 7,560 66% 5,784 +31%
POST Baseline 1,154 - 1,106 +4%
POST With Sentry 602 52% 529 +14%
POST With Sentry (error only) 1,050 91% 996 +5%
MYSQL Baseline 4,012 - 3,197 +25%
MYSQL With Sentry 567 14% 382 +48%
MYSQL With Sentry (error only) 3,269 81% 2,570 +27%

View base workflow run

sergical and others added 4 commits February 3, 2026 14:38
Methods like `rerank` were added in AI SDK v6, but the instrumentation
supports versions >=3.0.0 <7. Without this check, `new Proxy(undefined, ...)`
would throw a TypeError and crash all instrumentation on older SDK versions.

Also adds E2E test app for AI SDK v6 to test rerank functionality.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
MockLanguageModelV3 requires content array instead of text field.
MockRerankingModelV3 uses results/documentIndex/relevanceScore format.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

);
expect(rerankPipelineSpan).toBeDefined();
expect(rerankPipelineSpan?.data?.['gen_ai.request.rerank.documents_count']).toBe(3);
expect(rerankPipelineSpan?.data?.['gen_ai.response.rerank.top_score']).toBe(0.95);
Copy link

Choose a reason for hiding this comment

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

E2E test expects unmapped rerank attributes

Medium Severity

The E2E test expects rerank-specific attributes under the gen_ai.* namespace (gen_ai.request.rerank.query, gen_ai.request.rerank.documents_count, gen_ai.response.rerank.top_score), but the production code doesn't include any attribute mappings for rerank-specific data. Based on the existing codebase pattern, unmapped ai.* attributes get prefixed with vercel. and become vercel.ai.*. This mismatch means either the test expectations are incorrect (should use vercel.ai.*) or the production code is missing attribute mappings for rerank. Per the review rules, this flags that the test may not properly test the newly added behavior.

Fix in Cursor Fix in Web

The E2E test for AI SDK v6 rerank functionality cannot work because
of a bug in the AI SDK's MockRerankingModelV3.

When calling rerank() with MockRerankingModelV3, the SDK crashes with:
`TypeError: Cannot read properties of undefined (reading 'total')`

This happens because the AI SDK's internal telemetry code expects
usage data that the mock model doesn't provide (and isn't required
by the RerankingModelV3 interface).

The rerank instrumentation code is still fully functional and covered
by unit tests in packages/core/test/lib/tracing/vercel-ai-rerank.test.ts.

Note: Rerank mocking isn't available for E2E tests until the AI SDK
fixes MockRerankingModelV3. Track: https://github.com/vercel/ai/issues

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link
Member

@RulaKhaled RulaKhaled left a comment

Choose a reason for hiding this comment

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

Overall looks good, let's jus test this API in dev-packages/node-integration-tests/suites/tracing/vercelai/v6

Copy link
Member

@RulaKhaled RulaKhaled left a comment

Choose a reason for hiding this comment

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

We chatted async, it seems the mock client provided by Vercel AI hasn’t implemented doRerank yet: https://github.com/vercel/ai/blob/main/packages/ai/src/test/mock-reranking-model-v3.ts#L15

lgtm, thank you

@sergical sergical merged commit 54eb671 into develop Feb 4, 2026
416 of 418 checks passed
@sergical sergical deleted the feat/vercel-ai-rerank-token-mapping branch February 4, 2026 12:30
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.

feat(vercel-ai): Add rerank support and fix token attribute mapping

2 participants