Skip to content

Commit a662dea

Browse files
authored
chore (ai): remove sendExtraMessageFields (vercel#6120)
## Background The message id and createdAt fields are required for any solution that implements a realistic chatbot. If more control over the request is needed, filtering can be done using `prepareRequestBody`. ## Summary Remove `sendExtraMessageFields` (default to true).
1 parent a571d6e commit a662dea

File tree

22 files changed

+254
-164
lines changed

22 files changed

+254
-164
lines changed

.changeset/poor-kids-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': major
3+
---
4+
5+
chore (ai): remove sendExtraMessageFields

content/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export default function Chat({
9696
const { input, handleInputChange, handleSubmit, messages } = useChat({
9797
id, // use the provided chat ID
9898
initialMessages, // initial messages if provided
99-
sendExtraMessageFields: true, // send id and createdAt for each message
10099
});
101100

102101
// simplified rendering code, extend as needed:
@@ -120,8 +119,6 @@ export default function Chat({
120119
## Storing messages
121120

122121
`useChat` sends the chat id and the messages to the backend.
123-
We have enabled the `sendExtraMessageFields` option to send the id and createdAt fields,
124-
meaning that we store messages in the `useChat` message format.
125122

126123
<Note>
127124
The `useChat` message format is different from the `ModelMessage` format. The

content/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ Allows you to easily create a conversational user interface for your chatbot app
152152
description:
153153
'An optional literal that sets the mode of credentials to be used on the request. Defaults to same-origin.',
154154
},
155-
{
156-
name: 'sendExtraMessageFields',
157-
type: 'boolean',
158-
isOptional: true,
159-
description:
160-
"An optional boolean that determines whether to send extra fields you've added to `messages`. Defaults to `false` and only the `content` and `role` fields will be sent to the API endpoint. If set to `true`, the `name`, `data`, and `annotations` fields will also be sent.",
161-
},
162155
{
163156
name: 'maxSteps',
164157
type: 'number',

content/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ Allows you to create text completion based capabilities for your application. It
9898
description:
9999
'An optional literal that sets the mode of credentials to be used on the request. Defaults to same-origin.',
100100
},
101-
{
102-
name: 'sendExtraMessageFields',
103-
type: 'boolean',
104-
description:
105-
"An optional boolean that determines whether to send extra fields you've added to `messages`. Defaults to `false` and only the `content` and `role` fields will be sent to the API endpoint.",
106-
},
107101
{
108102
name: 'streamProtocol',
109103
type: "'text' | 'data'",
@@ -125,7 +119,8 @@ Allows you to create text completion based capabilities for your application. It
125119
description:
126120
'React only. Custom throttle wait time in milliseconds for the completion and data updates. When specified, throttles how often the UI updates during streaming. Default is undefined, which disables throttling.',
127121
},
128-
]}
122+
123+
]}
129124
/>
130125

131126
### Returns

examples/next-openai/app/use-chat-persistence-single-message-image-output/[id]/chat.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export default function Chat({
1111
api: '/api/use-chat-persistence-single-message-image-output',
1212
id, // use the provided chatId
1313
initialMessages, // initial messages if provided
14-
sendExtraMessageFields: true, // send id and createdAt for each message
1514
generateId: createIdGenerator({ prefix: 'msgc', size: 16 }), // id format for client-side messages
1615

1716
// only send the last message to the server:

examples/next-openai/app/use-chat-persistence-single-message-tools/[id]/chat.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default function Chat({
1212
api: '/api/use-chat-persistence-single-message-tools',
1313
id, // use the provided chatId
1414
initialMessages, // initial messages if provided
15-
sendExtraMessageFields: true, // send id and createdAt for each message
1615
generateId: createIdGenerator({ prefix: 'msgc', size: 16 }), // id format for client-side messages
1716

1817
// only send the last message to the server:

examples/next-openai/app/use-chat-persistence-single-message/[id]/chat.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export default function Chat({
1111
api: '/api/use-chat-persistence-single-message',
1212
id, // use the provided chatId
1313
initialMessages, // initial messages if provided
14-
sendExtraMessageFields: true, // send id and createdAt for each message
1514
generateId: createIdGenerator({ prefix: 'msgc', size: 16 }), // id format for client-side messages
1615

1716
// only send the last message to the server:

examples/next-openai/app/use-chat-persistence/[id]/chat.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export default function Chat({
1111
api: '/api/use-chat-persistence',
1212
id, // use the provided chatId
1313
initialMessages, // initial messages if provided
14-
sendExtraMessageFields: true, // send id and createdAt for each message
1514
generateId: createIdGenerator({ prefix: 'msgc', size: 16 }), // id format for client-side messages
1615
});
1716

examples/next-openai/app/use-chat-resilient-persistence/[id]/chat.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default function Chat({
1212
api: '/api/use-chat-resilient-persistence',
1313
id, // use the provided chatId
1414
initialMessages, // initial messages if provided
15-
sendExtraMessageFields: true, // send id and createdAt for each message
1615
generateId: createIdGenerator({ prefix: 'msgc', size: 16 }), // id format for client-side messages
1716
});
1817

examples/next-openai/app/use-chat-resume/chat.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export function Chat({
2828
id: chatId,
2929
api: '/api/use-chat-resume',
3030
initialMessages,
31-
sendExtraMessageFields: true,
3231
onError: error => {
3332
console.error('Error streaming text:', error);
3433
},
@@ -43,15 +42,15 @@ export function Chat({
4342
}, []);
4443

4544
return (
46-
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch gap-8">
45+
<div className="flex flex-col w-full max-w-md gap-8 py-24 mx-auto stretch">
4746
<Link href={`/use-chat-resume/${chatId}`} target="_noblank">
4847
Chat Id: {chatId}
4948
</Link>
5049

5150
<div>Status: {status}</div>
5251

5352
{messages.map(message => (
54-
<div key={message.id} className="whitespace-pre-wrap flex flex-row">
53+
<div key={message.id} className="flex flex-row whitespace-pre-wrap">
5554
<div className="min-w-12">
5655
{message.role === 'user' ? 'User: ' : 'AI: '}
5756
</div>

examples/nuxt-openai/pages/use-chat-request/index.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { createIdGenerator } from 'ai';
44
55
const { input, handleSubmit, messages } = useChat({
66
api: '/api/use-chat-request',
7-
sendExtraMessageFields: true,
87
generateId: createIdGenerator({ prefix: 'msgc', size: 16 }),
98
109
experimental_prepareRequestBody({ messages }) {

packages/ai/core/types/ui-messages.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,6 @@ either synchronously or asynchronously.
315315
*/
316316
body?: object;
317317

318-
/**
319-
* Whether to send extra message fields such as `message.id` and `message.createdAt` to the API.
320-
* Defaults to `false`. When set to `true`, the API endpoint might need to
321-
* handle the extra fields before forwarding the request to the AI service.
322-
*/
323-
sendExtraMessageFields?: boolean;
324-
325318
/**
326319
Streaming protocol that is used. Defaults to `data`.
327320
*/

packages/react/src/use-chat.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export function useChat({
129129
id,
130130
initialMessages,
131131
initialInput = '',
132-
sendExtraMessageFields,
133132
onToolCall,
134133
experimental_prepareRequestBody,
135134
maxSteps = 1,
@@ -266,19 +265,9 @@ Default is undefined, which disables throttling.
266265
throttleWaitMs,
267266
);
268267

269-
// Do an optimistic update to the chat state to show the updated messages immediately:
270-
const previousMessages = messagesRef.current;
268+
// Do an optimistic update to show the updated messages immediately:
271269
throttledMutate(chatMessages, false);
272270

273-
const constructedMessagesPayload = sendExtraMessageFields
274-
? chatMessages
275-
: chatMessages.map(({ role, content, annotations, parts }) => ({
276-
role,
277-
content,
278-
...(annotations !== undefined && { annotations }),
279-
...(parts !== undefined && { parts }),
280-
}));
281-
282271
const existingData = streamDataRef.current;
283272

284273
await callChatApi({
@@ -290,7 +279,7 @@ Default is undefined, which disables throttling.
290279
requestBody: chatRequest.body,
291280
}) ?? {
292281
id: chatId,
293-
messages: constructedMessagesPayload,
282+
messages: chatMessages,
294283
data: chatRequest.data,
295284
...extraMetadataRef.current.body,
296285
...chatRequest.body,
@@ -377,7 +366,6 @@ Default is undefined, which disables throttling.
377366
mutateStreamData,
378367
streamDataRef,
379368
streamProtocol,
380-
sendExtraMessageFields,
381369
experimental_prepareRequestBody,
382370
onToolCall,
383371
maxSteps,

0 commit comments

Comments
 (0)