-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Description
I am encountering an AI_InvalidPromptError when using the tool helper. The top-level error message suggests a mismatch between UI/Model messages, but the underlying cause is a schema validation failure (AI_TypeValidationError) on the tool result.
My tool returns an array of objects where some properties are optional (string | undefined). When the tool executes, some objects have these properties set to undefined, causing the keys to be stripped during JSON serialization. The SDK's internal validation for the tool-result message then fails, seemingly expecting a uniform shape across all items in the array or failing to handle the missing keys that were typed as optional.
Reproduction:
- Define a tool that returns an array of objects.
- Ensure the return type infers specific fields as
string | undefined. - Return an array where some items have the field defined, and others have it as
undefined. - Trigger the tool via
generateTextorstreamText.
Code Snippet:
import { tool } from 'ai';
import { z } from 'zod';
// Tool Definition
const listTasksTool = tool({
description: 'List tasks',
inputSchema: z.object({
limit: z.number().optional(),
}),
// No explicit outputSchema provided, relying on inference
execute: async ({ limit = 20 }) => {
// Simulating database response where 'startedAt' is optional
const tasks = [
{
id: "1",
state: "completed",
startedAt: "2025-10-26T17:59:40.065Z", // Field exists
completedAt: "2025-10-26T18:00:31.713Z"
},
{
id: "2",
state: "archived",
startedAt: undefined, // Field is undefined (will be stripped in JSON)
completedAt: undefined
}
];
return tasks;
},
});
// Inferred Return Type confirms optionality:
// Tool<..., { id: string; startedAt: string | undefined; ... }[]>The Issue:
The resulting tool-result message passed to the messages array looks like this (keys are missing for the second item):
{
"role": "tool",
"content": [
{
"type": "tool-result",
"toolName": "tasks---list_tasks",
"output": {
"type": "json",
"value": [
{ "id": "1", "startedAt": "2025-10-26..." },
{ "id": "2" } // "startedAt" is missing entirely here
]
}
}
]
}Error Log:
The application crashes with the following error:
AI_InvalidPromptError: Invalid prompt: The messages must be a ModelMessage[]. If you have passed a UIMessage[], you can use convertToModelMessages to convert them.
Upon inspecting the cause, it reveals an AI_TypeValidationError (Zod validation failure) on the missing keys in the array:
{
"name": "AI_TypeValidationError",
"cause": [
{
"code": "invalid_union",
"errors": [
// ... validation path showing failure on index 1 of output.value ...
{
"code": "invalid_type",
"expected": "string",
"message": "Invalid input: expected string, received undefined",
"path": ["startedAt"]
}
]
}
]
}Expected Behavior:
The SDK should handle tool-result payloads where keys are missing if the value was undefined, or the generic inference should be lenient enough to accept heterogeneous arrays in output.value without requiring an explicit outputSchema that forces null. The error message regarding ModelMessage[] vs UIMessage[] is also confusing in this context as the messages are structurally correct aside from the tool payload validation.
AI SDK Version
- ai: 5.0.106
Code of Conduct
- I agree to follow this project's Code of Conduct