Comments (20)
Thanks everyone for trying to debug the issue and I appreciate everyone's patience! Like you all had suspected, the messages
property didn't follow the spec so the migration from render
to streamUI
wasn't trivial and caused errors.
#337 follows the messages spec and should fix the error! I will also have an upgrade guide up in the docs soon to provide more clarity about this change and prevent any future confusion.
from ai-chatbot.
The latest update "streamUI instead of render #324" does not allow messages with "role: system" between conversation (only at start) with new prompt param system.
The "problem" is:
The functions also uses messages with "role: system" to register the actions or events from the user in the context conversation.
The error Cannot destructure property 'role' error
is consequence of the function below that return undefined for "role: system"
This function is inside of dependency "ai": "^3.1.1"
// core/prompt/convert-to-language-model-prompt.ts
function convertToLanguageModelPrompt(prompt) {
const languageModelMessages = [];
if (prompt.system != null) {
languageModelMessages.push({ role: "system", content: prompt.system });
}
switch (prompt.type) {
case "prompt": {
languageModelMessages.push({
role: "user",
content: [{ type: "text", text: prompt.prompt }]
});
break;
}
case "messages": {
languageModelMessages.push(
...prompt.messages.map((message) => {
switch (message.role) {
case "user": {
if (typeof message.content === "string") {
return {
role: "user",
content: [{ type: "text", text: message.content }]
};
}
return {
role: "user",
content: message.content.map(
(part) => {
var _a;
switch (part.type) {
case "text": {
return part;
}
case "image": {
if (part.image instanceof URL) {
return {
type: "image",
image: part.image,
mimeType: part.mimeType
};
}
const imageUint8 = convertDataContentToUint8Array(
part.image
);
return {
type: "image",
image: imageUint8,
mimeType: (_a = part.mimeType) != null ? _a : detectImageMimeType(imageUint8)
};
}
}
}
)
};
}
case "assistant": {
if (typeof message.content === "string") {
return {
role: "assistant",
content: [{ type: "text", text: message.content }]
};
}
return { role: "assistant", content: message.content };
}
case "tool": {
return message;
}
}
})
);
break;
}
default: {
const _exhaustiveCheck = prompt;
throw new Error(`Unsupported prompt type: ${_exhaustiveCheck}`);
}
}
return languageModelMessages;
}
I tested in compiled module inside node_modules/ai/rsc/dist/rsc-server.msj
function convertToLanguageModelPrompt by adding
case "system": {
return message;
}
This works but the correction must be made in the repository of the dependency "ai": "^3.1.1".
And this depends on whether it is considered good practice to send system messages inside of the conversation to save new context (user events)
from ai-chatbot.
Replacing all function roles to assistant works. And now ai SDK version [email protected] has support for system message https://github.com/vercel/ai/blob/main/packages/core/core/prompt/message.ts#L12
from ai-chatbot.
the messages sent to openai are not formatted properly.
Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.
I was testing. This works.
const toolCallMessage: CoreMessage = { role: "assistant", content: [ { type: "tool-call", toolName: "showStockPurchase", toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp", args: z.object({ symbol: z.string(), price: z.number(), defaultAmount: z.number(), }), }, ], }; const toolMessage: CoreMessage = { role: "tool", content: [ { result: JSON.stringify({ symbol: "AAPL", price: 150, defaultAmount: 100, status: "completed", }), type: "tool-result", toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp", toolName: "showStockPurchase", }, ], }; const all = [...messages, toolCallMessage, toolMessage];
@athrael-soju
This is not a solution, just a problem description. I am still learning this. So, I don't think I am the best person for the PR.
from ai-chatbot.
Because the new SDK uses streamUI
ai-chatbot/lib/chat/actions.tsx
Line 140 in 095550d
And old one uses a render
ai-chatbot/lib/chat/actions.tsx
Line 144 in d5f7361
The bug appears when using streamUI, not render. If you revert to the previous commit it works fine with render.
from ai-chatbot.
Can anyone please explain why https://chat.vercel.ai/ works perfectly fine, but the main branch on localhost does not?
Isn't the main branch from the repo deployed to this domain?
from ai-chatbot.
the messages sent to openai are not formatted properly.
Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.
I was testing. This works.
const toolCallMessage: CoreMessage = {
role: "assistant",
content: [
{
type: "tool-call",
toolName: "showStockPurchase",
toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp",
args: z.object({
symbol: z.string(),
price: z.number(),
defaultAmount: z.number(),
}),
},
],
};
const toolMessage: CoreMessage = {
role: "tool",
content: [
{
result: JSON.stringify({
symbol: "AAPL",
price: 150,
defaultAmount: 100,
status: "completed",
}),
type: "tool-result",
toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp",
toolName: "showStockPurchase",
},
],
};
const all = [...messages, toolCallMessage, toolMessage];
from ai-chatbot.
The above didn't solve my problem. There is a phantom object that is undefined
on my end. And that's causing the error
Obviously a little sanity check addresses this but I'm curious where and why an operation returns undefined
in the first place and fix it there instead.
from ai-chatbot.
@kevb10's sanity check also works for me, using patch-package to create a patch file instead of making edits inside of node_modules
(this way, I won't lose my changes as soon as I run npm update
or npm install
). Still not an ideal solution, obviously, but good enough for the time being.
Just ran
npm install patch-package postinstall-postinstall --save-dev
Followed by:
npx patch-package @ai-sdk/openai
And the issue went away. Still interested in a more permanent solution, though.
from ai-chatbot.
Maybe because that might point to some old commit.
from ai-chatbot.
Same error. Any solution?
from ai-chatbot.
@kevb10 you can track your messages in the function streamUI locate in node_modules/ai/rsc/dist/rsc-server.mjs
Check messages before and after
And you can do testing with the function convertToLanguageModelPrompt(validatedPrompt)
to check what message return undefined
P.S. remember that you are in the dependency
from ai-chatbot.
Thanks @kevb10. It didn't solve my problem either, but it's nice to see progress on this issue
Update: I implemented your sanity check and it works for me.
from ai-chatbot.
Just noting that I'm also running into this bug and have been troubleshooting it a bit today. This affects any component that makes use of function or system role messages and happens after an unsupported message role type has been inserted into the chat history (which creates an undefined chat message).
What I've found mirrors what some of the other commenters have noted: adjusting the node_modules/ai/rsc/dist/rsc-server.msj file's convertToLanguageModelPrompt() function to handle more of the message types defined in actions.tsx (specifically 'function' and 'system') seems to eliminate the error, although it's not a good solution since it's a workaround in the dependency and I'm not clear what other effects that change might be having.
Something in the RSC framework is choking on handling message types of 'function', 'tool', 'data', and in some cases 'system' and it's causing them to become undefined messages in the chat history array, which is creating downstream problems elsewhere in the app.
I'm also not clear on whether those roles are intended to be functional in the RSC or with GPT-4 and just aren't working here yet, or if the app is trying to do something it shouldn't be and using the roles incorrectly. The message role functionality is still very new to me!
from ai-chatbot.
Honestly, with all the bugs combined, you're better off using the vercel-ai-src example
from ai-chatbot.
@prashantbhudwal Could you raise a PR please?
from ai-chatbot.
Same kind of error here with this error message: Cannot destructure property 'role' of '.for' as it is undefined. at convertToOpenAIChatMessage
Can't have the app reply after showing me a card.
Time for me to study some docs đ and try some implementations !! GLHF
UPDATE:
I've tried to check out to the previous commit and the bug disapears:
from ai-chatbot.
Because the new SDK uses streamUI
ai-chatbot/lib/chat/actions.tsx
Line 140 in 095550d
And old one uses a render
ai-chatbot/lib/chat/actions.tsx
Line 144 in d5f7361
from ai-chatbot.
I have the same error after clicking on the first generated component, I update everything even nextjs but it didn't work
from ai-chatbot.
Yes, this is an issue with the new code. I guess it should be reverted or fixed? I tried a few things to resolve it, but wasn't able to.
from ai-chatbot.
Related Issues (20)
- How to handle multistep toll call with streamUI? HOT 6
- When building and using Redis on-premise HOT 1
- Google OAuth HOT 1
- Chat messages disappearing when clicked on New Chat button and coming back to last chat, chats showing after page refresh HOT 3
- All non OpenAI providers giving errors like Type 'GoogleGenerativeAILanguageModel' is not assignable to type 'LanguageModelV1'. HOT 2
- Vercel.json
- When using tools call - double divider is rendered
- Is this project likely to continue? HOT 1
- I got a blank result HOT 2
- If this app is not active, could you please archive?
- side bar actions doesn't work when scroll is activate HOT 1
- Stalling on some threads randomly - `undefined` runResult for certain threads. HOT 2
- Image generation using DALL-E 3 or other models
- CODESPACE ERROR: `x-forwarded-host` header with value `xxx-3000.app.github.dev` does not match `origin` header with value `localhost:3000` from a forwarded Server Actions request. Aborting the action. HOT 3
- error 500 Internal issue
- The text streaming is broken on latest next@15 and react@rc
- Unexpected stock UI generation HOT 2
- This Edge Function has crashed.
- How to create an embeddable component for others to use on thier website?
- Sidebar chat title and New chat button does not work when UI is generated as a response HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
đ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. đđđ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google â¤ď¸ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ai-chatbot.