Commit 4df7cc7
Add MAPPING span tracing for tool toModelOutput transforms (#16347)
## Description
This PR adds observability tracing for tool `toModelOutput`
transformations by emitting a `MAPPING` span type. When a tool defines a
`toModelOutput` function and it executes successfully, a child span is
created to track the transformation of the tool result before it's
passed to the model.
### Changes Made
1. **New Span Type**: Added `MAPPING` span type to `SpanType` enum in
`observability/types/tracing.ts` to represent inline data transforms
between pipeline stages.
2. **New Attributes Interface**: Created `MappingAttributes` interface
to capture mapping-specific metadata:
- `mappingType`: Identifier of the mapping (e.g., `toModelOutput`)
- `toolCallId`: Associated tool call ID when mapping operates on a tool
result
3. **Span Emission Logic**: Updated `llm-mapping-step.ts` to:
- Create a child `MAPPING` span when `toModelOutput` is defined and the
tool result is non-null
- Capture the tool result as span input and the transformed output as
span output
- Mark the span as errored if `toModelOutput` throws, then re-throw the
error
- Skip span creation if the tool has no `toModelOutput` or if the result
is null/undefined
4. **Comprehensive Tests**: Added four test cases covering:
- Successful mapping span emission with correct attributes and output
- No span emission when tool lacks `toModelOutput`
- No span emission when tool result is null/undefined
- Error handling when `toModelOutput` throws
## Related Issue(s)
Fixes #15486
## Type of Change
- [ ] Bug fix (non-breaking change that fixes an issue)
- [x] 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
- [x] 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
- [x] I have addressed all Coderabbit comments on this PR
https://claude.ai/code/session_018bEDqEtGS2XkdsqBjye6JC
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5
When a tool changes its result before the AI model sees it, the system
now records that transformation in traces so you can see both the
original tool output and what the model actually received.
## Overview
This PR adds observability for tool toModelOutput transformations by
introducing a MAPPING span type and emitting a child MAPPING span when a
tool defines toModelOutput and the tool result is non-null. The span
records the raw tool result as input and the transformed model-facing
output as span output, and marks the span errored if the transform
throws. No mapping span is created when toModelOutput is absent or the
tool result is null/undefined.
## Changes
- Added SpanType.MAPPING = 'mapping' and exported MappingAttributes in
packages/core/src/observability/types/tracing.ts to represent inline
data transforms and carry mapping-specific attributes (mappingType,
toolCallId).
- Updated
packages/core/src/loop/workflows/agentic-execution/llm-mapping-step.ts
to:
- create a child MAPPING span (EntityType.TOOL) when tool.toModelOutput
exists and the tool result is non-null,
- set span input to the raw tool result and end the span with the
transformed modelOutput,
- mark the span errored (endSpan: true) and re-throw if toModelOutput
throws,
- avoid creating a span when toModelOutput is undefined or tool result
is null/undefined,
- attach modelOutput into providerMetadata.mastra.modelOutput when
present.
- Added tests in
packages/core/src/loop/workflows/agentic-execution/llm-mapping-step.test.ts
covering:
- successful MAPPING span emission with expected attributes and output,
- no span when the tool lacks toModelOutput,
- no span when tool result is null/undefined,
- error handling when toModelOutput throws (span errored and error
re-thrown).
- Updated client SDK route types
(client-sdks/client-js/src/route-types.generated.ts) to include
'mapping' in relevant union types.
- Documented the change in the changeset (.changeset/loud-ads-double.md)
and regenerated client types as part of the commit.
## Issue Resolution
Addresses issue #15486 by capturing toModelOutput return values in
tracing so traces can distinguish "not invoked" vs "no-op" vs
"transformed" for tool-to-model mappings.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent ccaa8fa commit 4df7cc7
5 files changed
Lines changed: 253 additions & 8 deletions
File tree
- .changeset
- client-sdks/client-js/src
- packages/core/src
- loop/workflows/agentic-execution
- observability/types
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11363 | 11363 | | |
11364 | 11364 | | |
11365 | 11365 | | |
| 11366 | + | |
11366 | 11367 | | |
11367 | 11368 | | |
11368 | 11369 | | |
| |||
11528 | 11529 | | |
11529 | 11530 | | |
11530 | 11531 | | |
11531 | | - | |
| 11532 | + | |
| 11533 | + | |
11532 | 11534 | | |
11533 | 11535 | | |
11534 | 11536 | | |
| |||
11735 | 11737 | | |
11736 | 11738 | | |
11737 | 11739 | | |
| 11740 | + | |
11738 | 11741 | | |
11739 | 11742 | | |
11740 | 11743 | | |
| |||
11901 | 11904 | | |
11902 | 11905 | | |
11903 | 11906 | | |
11904 | | - | |
| 11907 | + | |
| 11908 | + | |
11905 | 11909 | | |
11906 | 11910 | | |
11907 | 11911 | | |
| |||
12091 | 12095 | | |
12092 | 12096 | | |
12093 | 12097 | | |
12094 | | - | |
| 12098 | + | |
| 12099 | + | |
12095 | 12100 | | |
12096 | 12101 | | |
12097 | 12102 | | |
| |||
12274 | 12279 | | |
12275 | 12280 | | |
12276 | 12281 | | |
12277 | | - | |
| 12282 | + | |
| 12283 | + | |
12278 | 12284 | | |
12279 | 12285 | | |
12280 | 12286 | | |
| |||
12451 | 12457 | | |
12452 | 12458 | | |
12453 | 12459 | | |
12454 | | - | |
| 12460 | + | |
| 12461 | + | |
12455 | 12462 | | |
12456 | 12463 | | |
12457 | 12464 | | |
| |||
12549 | 12556 | | |
12550 | 12557 | | |
12551 | 12558 | | |
12552 | | - | |
| 12559 | + | |
| 12560 | + | |
12553 | 12561 | | |
12554 | 12562 | | |
12555 | 12563 | | |
| |||
12854 | 12862 | | |
12855 | 12863 | | |
12856 | 12864 | | |
| 12865 | + | |
12857 | 12866 | | |
12858 | 12867 | | |
12859 | 12868 | | |
| |||
Lines changed: 194 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1195 | 1195 | | |
1196 | 1196 | | |
1197 | 1197 | | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
1198 | 1392 | | |
Lines changed: 25 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
| 117 | + | |
114 | 118 | | |
115 | 119 | | |
116 | 120 | | |
| |||
121 | 125 | | |
122 | 126 | | |
123 | 127 | | |
124 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
125 | 148 | | |
126 | 149 | | |
127 | 150 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
87 | 89 | | |
88 | 90 | | |
89 | 91 | | |
| |||
342 | 344 | | |
343 | 345 | | |
344 | 346 | | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
345 | 358 | | |
346 | 359 | | |
347 | 360 | | |
| |||
647 | 660 | | |
648 | 661 | | |
649 | 662 | | |
| 663 | + | |
650 | 664 | | |
651 | 665 | | |
652 | 666 | | |
| |||
0 commit comments