Skip to content

Commit fcf4323

Browse files
authoredJun 6, 2024··
fix (core): filter out empty assistant text messages (#1874)
1 parent 9fb3b4d commit fcf4323

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed
 

Diff for: ‎.changeset/eleven-maps-yawn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
fix (core): filter out empty assistant text messages

Diff for: ‎packages/core/core/generate-text/generate-text.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,6 @@ describe('maxToolRoundtrips', () => {
428428
{
429429
role: 'assistant',
430430
content: [
431-
{ type: 'text', text: '' },
432431
{
433432
type: 'tool-call',
434433
toolCallId: 'call-1',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { convertToLanguageModelMessage } from './convert-to-language-model-prompt';
2+
3+
describe('convertToLanguageModelMessage', () => {
4+
describe('assistant message', () => {
5+
it('should ignore empty text parts', async () => {
6+
const result = convertToLanguageModelMessage({
7+
role: 'assistant',
8+
content: [
9+
{
10+
type: 'text',
11+
text: '',
12+
},
13+
{
14+
type: 'tool-call',
15+
toolName: 'toolName',
16+
toolCallId: 'toolCallId',
17+
args: {},
18+
},
19+
],
20+
});
21+
22+
expect(result).toEqual({
23+
role: 'assistant',
24+
content: [
25+
{
26+
type: 'tool-call',
27+
args: {},
28+
toolCallId: 'toolCallId',
29+
toolName: 'toolName',
30+
},
31+
],
32+
});
33+
});
34+
});
35+
});

Diff for: ‎packages/core/core/prompt/convert-to-language-model-prompt.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ export function convertToLanguageModelMessage(
100100
};
101101
}
102102

103-
return { role: 'assistant', content: message.content };
103+
return {
104+
role: 'assistant',
105+
content: message.content.filter(
106+
// remove empty text parts:
107+
part => part.type !== 'text' || part.text !== '',
108+
),
109+
};
104110
}
105111

106112
case 'tool': {

0 commit comments

Comments
 (0)