Skip to content

Commit 31b6067

Browse files
catdalfonsocursoragentTylerBarnes
authored
fix: persist modelId in assistant message content.metadata (#12969)
Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Tyler Barnes <tylerdbarnes@gmail.com>
1 parent 57c7391 commit 31b6067

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

.changeset/olive-bees-film.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@mastra/core': patch
3+
---
4+
5+
Fixed assistant messages to persist `content.metadata.modelId` during streaming.
6+
This ensures stored and processed assistant messages keep the model identifier.
7+
Developers can now reliably read `content.metadata.modelId` from downstream storage adapters and processors.

packages/core/src/agent/__tests__/memory-metadata.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,40 @@ function memoryMetadataTests(version: 'v1' | 'v2') {
6666
}
6767
});
6868

69+
it.skipIf(version !== 'v2')(
70+
'should persist modelId in assistant message content.metadata using stream',
71+
async () => {
72+
const mockMemory = new MockMemory();
73+
const agent = new Agent({
74+
id: 'test-agent',
75+
name: 'Test Agent',
76+
instructions: 'test',
77+
model: dummyModel,
78+
memory: mockMemory,
79+
});
80+
81+
const res = await agent.stream('hello', {
82+
memory: {
83+
resource: 'user-1',
84+
thread: { id: 'thread-model-stream' },
85+
},
86+
});
87+
88+
await res.consumeStream();
89+
90+
const { messages } = await mockMemory.recall({
91+
threadId: 'thread-model-stream',
92+
perPage: false,
93+
});
94+
const assistantMessages = messages.filter(m => m.role === 'assistant');
95+
expect(assistantMessages.length).toBeGreaterThan(0);
96+
97+
for (const msg of assistantMessages) {
98+
expect(msg.content.metadata?.modelId).toBe('mock-model-id');
99+
}
100+
},
101+
);
102+
69103
it('should create a new thread with metadata using generate', async () => {
70104
const mockMemory = new MockMemory();
71105
const agent = new Agent({

packages/core/src/loop/workflows/agentic-execution/llm-execution-step.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ type ProcessOutputStreamOptions<OUTPUT = undefined> = {
5858
transportResolver?: () => StreamTransport | undefined;
5959
};
6060

61+
function buildResponseModelMetadata(runState: AgenticRunState): { metadata: Record<string, unknown> } | undefined {
62+
const modelId = runState.state.responseMetadata?.modelId;
63+
return modelId ? { metadata: { modelId } } : undefined;
64+
}
65+
6166
async function processOutputStream<OUTPUT = undefined>({
6267
tools,
6368
messageId,
@@ -131,6 +136,7 @@ async function processOutputStream<OUTPUT = undefined>({
131136
...(providerMetadata ? { providerMetadata } : {}),
132137
},
133138
],
139+
...buildResponseModelMetadata(runState),
134140
},
135141
createdAt: new Date(),
136142
};
@@ -274,6 +280,7 @@ async function processOutputStream<OUTPUT = undefined>({
274280
providerMetadata: chunk.payload.providerMetadata ?? runState.state.providerOptions,
275281
},
276282
],
283+
...buildResponseModelMetadata(runState),
277284
},
278285
createdAt: new Date(),
279286
};
@@ -313,6 +320,7 @@ async function processOutputStream<OUTPUT = undefined>({
313320
providerMetadata: chunk.payload.providerMetadata ?? runState.state.providerOptions,
314321
},
315322
],
323+
...buildResponseModelMetadata(runState),
316324
},
317325
createdAt: new Date(),
318326
};
@@ -346,6 +354,7 @@ async function processOutputStream<OUTPUT = undefined>({
346354
mimeType: chunk.payload.mimeType,
347355
},
348356
],
357+
...buildResponseModelMetadata(runState),
349358
},
350359
createdAt: new Date(),
351360
};
@@ -373,6 +382,7 @@ async function processOutputStream<OUTPUT = undefined>({
373382
},
374383
},
375384
],
385+
...buildResponseModelMetadata(runState),
376386
},
377387
createdAt: new Date(),
378388
};
@@ -1075,6 +1085,7 @@ export function createLLMExecutionStep<TOOLS extends ToolSet = ToolSet, OUTPUT =
10751085
providerExecuted: toolCall.providerExecuted,
10761086
};
10771087
}),
1088+
...buildResponseModelMetadata(runState),
10781089
},
10791090
createdAt: new Date(),
10801091
};

0 commit comments

Comments
 (0)