Skip to content

fix(core): re-stamp MODEL_GENERATION span when fallback model takes over#15503

Merged
epinzur merged 2 commits into
mastra-ai:mainfrom
intojhanurag:fix/fallback-model-span-attribution
Apr 20, 2026
Merged

fix(core): re-stamp MODEL_GENERATION span when fallback model takes over#15503
epinzur merged 2 commits into
mastra-ai:mainfrom
intojhanurag:fix/fallback-model-span-attribution

Conversation

@intojhanurag
Copy link
Copy Markdown
Contributor

@intojhanurag intojhanurag commented Apr 19, 2026

Description

Fixes fallback model attribution in observability traces. When an agent's primary model failed and the fallback model served the response, the MODEL_GENERATION span kept the primary
model's model and provider attributes, so exporters like Langfuse attributed usage and cost to the wrong (failed) model or dropped them entirely.

Before

image

After

image

Related Issue(s)

Fixes #13547

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Test update

Checklist

  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works
  • I have addressed all Coderabbit comments on this PR

ELI5

Imagine you ask a friend for help, but they can't and a backup friend does it. When someone asks who helped, you should say the backup friend. This PR ensures the system records the backup model when the main model fails, so usage and cost are attributed to the model that actually answered.

Summary

This PR fixes fallback model attribution in observability traces: when an agent's primary model fails and a fallback model serves the response, the MODEL_GENERATION span is re-stamped with the fallback model's modelId and provider so exporters (e.g., Langfuse) attribute usage and cost to the correct model.

Key changes:

  • Implementation (packages/core/src/loop/workflows/agentic-execution/llm-execution-step.ts): inside executeStreamWithFallbackModels, call modelSpanTracker.updateGeneration() when a fallback model (activeFallbackModelIndex > 0) is selected to re-stamp the MODEL_GENERATION span with the active model's name, model, and provider attributes.
  • Test (packages/core/src/loop/workflows/agentic-execution/llm-execution-step.test.ts): added test "re-stamps MODEL_GENERATION span attributes when a fallback model takes over" which mocks modelSpanTracker, forces the primary model to throw, runs a successful secondary model, and asserts updateGeneration is called with the secondary model's attributes.
  • Release notes (.changeset/slick-goats-cover.md): added a changeset describing the patch release and observability fix.

Design note:

  • The change reuses the existing update pattern used for processor-driven model swaps, implemented in Mastra core so downstream exporters (Langfuse, Datadog, Arize, otel-exporter) automatically receive corrected attribution.

Related: fixes #13547

Signed-off-by: Anurag Ojha <aojharaj2004@gmail.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 19, 2026

🦋 Changeset detected

Latest commit: d94661a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 21 packages
Name Type
@mastra/core Patch
mastracode Patch
@mastra/mcp-docs-server Patch
@internal/playground Patch
@mastra/client-js Patch
@mastra/opencode Patch
@mastra/longmemeval Patch
mastra Patch
@mastra/deployer-cloud Patch
@mastra/deployer-vercel Patch
@mastra/playground-ui Patch
@mastra/react Patch
@mastra/server Patch
@mastra/deployer Patch
create-mastra Patch
@mastra/express Patch
@mastra/fastify Patch
@mastra/hono Patch
@mastra/koa Patch
@mastra/deployer-cloudflare Patch
@mastra/deployer-netlify Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 19, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: dcad2747-c04d-4a94-9110-24d45594c0eb

📥 Commits

Reviewing files that changed from the base of the PR and between 0312b1a and d94661a.

📒 Files selected for processing (1)
  • .changeset/slick-goats-cover.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/slick-goats-cover.md

Walkthrough

Re-stamps MODEL_GENERATION spans when a fallback model runs after a primary model failure so span attributes (model, provider, name) reflect the actual model that produced the response and downstream exporters receive correct generation attribution.

Changes

Cohort / File(s) Summary
Changeset Documentation
\.changeset/slick-goats-cover.md
Adds a changeset file documenting a patch release for @mastra/core describing the observability fix for fallback model attribution.
Implementation
packages/core/src/loop/workflows/agentic-execution/llm-execution-step.ts
When a fallback model is selected (activeFallbackModelIndex > 0), calls modelSpanTracker.updateGeneration() to re-stamp the MODEL_GENERATION span with the fallback model's model and provider.
Tests
packages/core/src/loop/workflows/agentic-execution/llm-execution-step.test.ts
Adds a test verifying that modelSpanTracker.updateGeneration is called with span metadata reflecting the secondary/fallback model after a retryable APICallError from the primary model.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the specific change (re-stamping MODEL_GENERATION span) and its trigger condition (fallback model takeover), using imperative mood and proper formatting with the 'fix' prefix.
Linked Issues check ✅ Passed The PR directly addresses issue #13547 by implementing model attribution fixes for fallback models in observability spans, ensuring Langfuse and other exporters correctly track fallback model usage and costs.
Out of Scope Changes check ✅ Passed All changes are focused on fallback model span attribution: a changeset entry, test case validation, and a targeted implementation in llm-execution-step.ts, with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.changeset/slick-goats-cover.md:
- Line 5: Update the changeset (.changeset/slick-goats-cover.md) to be an
outcome-first, user-focused release note: remove internal implementation
references like "MODEL_GENERATION span" and technical jargon (e.g., "exporters",
"Langfuse"), shorten to one or two sentences that state the user-visible impact
— e.g., that fallback models are now correctly attributed in traces so usage and
cost reporting are accurate — and keep the entry brief and non-technical per the
repo guidelines.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 74653a43-29e2-4080-b854-7487b427cdbb

📥 Commits

Reviewing files that changed from the base of the PR and between 1bb53d1 and 0312b1a.

📒 Files selected for processing (3)
  • .changeset/slick-goats-cover.md
  • packages/core/src/loop/workflows/agentic-execution/llm-execution-step.test.ts
  • packages/core/src/loop/workflows/agentic-execution/llm-execution-step.ts

Comment thread .changeset/slick-goats-cover.md Outdated
Signed-off-by: Anurag Ojha <aojharaj2004@gmail.com>
@intojhanurag intojhanurag requested a review from epinzur April 20, 2026 04:08
@epinzur epinzur merged commit 1bd5104 into mastra-ai:main Apr 20, 2026
42 of 43 checks passed
@intojhanurag intojhanurag deleted the fix/fallback-model-span-attribution branch April 20, 2026 07:09
intojhanurag added a commit to intojhanurag/mastra that referenced this pull request Apr 21, 2026
…ver (mastra-ai#15503)

## Description

Fixes fallback model attribution in observability traces. When an
agent's primary model failed and the fallback model served the response,
the `MODEL_GENERATION` span kept the primary
model's `model` and `provider` attributes, so exporters like Langfuse
attributed usage and cost to the wrong (failed) model or dropped them
entirely.

## Before
<img width="1512" height="982" alt="image"
src="https://github.com/user-attachments/assets/28900c25-7403-4f35-ad09-34529247f85f"
/>

## After
<img width="1512" height="982" alt="image"
src="https://github.com/user-attachments/assets/6244ef59-4b7d-4eef-912a-33060433444b"
/>

## Related Issue(s)

Fixes mastra-ai#13547

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Test update

## Checklist

- [x] I have made corresponding changes to the documentation (if
applicable)
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have addressed all Coderabbit comments on this PR

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5

Imagine you ask a friend for help, but they can't and a backup friend
does it. When someone asks who helped, you should say the backup friend.
This PR ensures the system records the backup model when the main model
fails, so usage and cost are attributed to the model that actually
answered.

## Summary

This PR fixes fallback model attribution in observability traces: when
an agent's primary model fails and a fallback model serves the response,
the MODEL_GENERATION span is re-stamped with the fallback model's
modelId and provider so exporters (e.g., Langfuse) attribute usage and
cost to the correct model.

Key changes:
- Implementation
(packages/core/src/loop/workflows/agentic-execution/llm-execution-step.ts):
inside executeStreamWithFallbackModels, call
modelSpanTracker.updateGeneration() when a fallback model
(activeFallbackModelIndex > 0) is selected to re-stamp the
MODEL_GENERATION span with the active model's name, model, and provider
attributes.
- Test
(packages/core/src/loop/workflows/agentic-execution/llm-execution-step.test.ts):
added test "re-stamps MODEL_GENERATION span attributes when a fallback
model takes over" which mocks modelSpanTracker, forces the primary model
to throw, runs a successful secondary model, and asserts
updateGeneration is called with the secondary model's attributes.
- Release notes (.changeset/slick-goats-cover.md): added a changeset
describing the patch release and observability fix.

Design note:
- The change reuses the existing update pattern used for
processor-driven model swaps, implemented in Mastra core so downstream
exporters (Langfuse, Datadog, Arize, otel-exporter) automatically
receive corrected attribution.

Related: fixes mastra-ai#13547
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Anurag Ojha <aojharaj2004@gmail.com>
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.

Support fallback models in @mastra/langfuse

2 participants