-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Closed
Description
Description
If we switch models in the middle of a conversation, we get :
Error [AI_APICallError]: Text part is missing a thought_signature in content position 2, part position 1.
Not sure how/if this can be fixed.
example :
import {
createUIMessageStream,
createUIMessageStreamResponse,
streamText,
type UIMessage,
convertToModelMessages,
} from "ai";
import { google } from "@ai-sdk/google";
export const maxDuration = 60;
export const runtime = "nodejs";
export async function POST(req: Request) {
const {
messages,
}: {
messages: UIMessage[];
} = await req.json();
const stream = createUIMessageStream({
originalMessages: messages,
execute: async ({ writer }) => {
const result = streamText({
model: google("gemini-3-pro-preview"), // Default model
messages: convertToModelMessages(messages),
system:
"You are a helpful AI assistant. When asked to generate images, you'll switch to the image model automatically.",
prepareStep: async ({ messages, stepNumber }) => {
// Get the last user message to check for image generation request
const lastUserMessage = messages
.slice()
.reverse()
.find((msg: any) => msg.role === "user");
if (lastUserMessage) {
const content = Array.isArray(lastUserMessage.content)
? lastUserMessage.content
.filter((part: any) => part.type === "text")
.map((part: any) => part.text)
.join(" ")
: lastUserMessage.content;
// Switch to image model if "generate an image" is detected
if (
typeof content === "string" &&
content.toLowerCase().includes("generate an image")
) {
console.log(
"🎨 Switching to gemini-3-image-preview for image generation"
);
return {
model: google("gemini-3-pro-image-preview"),
};
}
}
// Default: continue with gemini-3-pro-preview
console.log("💬 Using gemini-3-pro-preview for text generation");
return {};
},
});
writer.merge(result.toUIMessageStream({ sendStart: true }));
// Wait for the result to finish
await result.response;
},
});
return createUIMessageStreamResponse({ stream });
}AI SDK Version
"ai": "^5.0.98",
"@ai-sdk/google": "^2.0.40",
Code of Conduct
- I agree to follow this project's Code of Conduct
rahulkarajgikar