Typing toolCalls
and toolResults
thats being generated from generateText
#4540
-
Example code: const { text, toolResults, toolCalls } = await generateText({
model,
messages,
system: systemPrompt,
tools: tools(user.id),
}); Tool results type: const toolResults: ToolResultArray<{
toolFunction1: CoreTool<ZodObject<{}, "strip", ZodTypeAny, {}, {}>, ToolFunctionReturnValue> & {
...;
};
toolFunction2: CoreTool<...> & {
...;
};
}> Let's say I want to process tool results in a separated function like: // how do I type `toolResults` properly so that it can show me the right tool names with their corresponding result and args type?
const processToolResults = (toolResults: any) => {
// process tool results
} Here is how toolResults is typed: readonly toolResults: ToolResultArray<TOOLS>; // `toolResults` field of the return type of `generateText`
type TOOLS = Record<string, CoreTool>
type ToolResultArray<TOOLS extends Record<string, CoreTool>> = Array<ToolResultUnion<TOOLS>>;
type ToolResultUnion<TOOLS extends Record<string, CoreTool>> = ToToolResultObject<ToToolsWithDefinedExecute<ToToolsWithExecute<TOOLS>>> I was messing around with it but before diving deeper into finding a solution, wanted to make sure that there is no simple solution for it which I am not aware of. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Hey! What are you trying to use the types for? The toolCalls and toolResults are strongly typed by default (https://sdk.vercel.ai/cookbook/node/call-tools#accessing-tool-results) for processing calls / results. Is that what you're looking for? |
Beta Was this translation helpful? Give feedback.
-
Could you give me a TS playground? |
Beta Was this translation helpful? Give feedback.
-
Thanks for responding so quick. I ended up now patching the ai package and exporting these types here: import { ToolCallArray, ToolResultArray } from 'ai'; From there I did: export type MyTools = typeof tools;
export type MyToolResultArray = ToolResultArray<MyTools>;
export type MyToolCallArray = ToolCallArray<MyTools>; Now I can do this: export const processToolResults = (toolResults: MyToolResultArray) => {} And |
Beta Was this translation helpful? Give feedback.
-
If we can't get something more simple I'd love you guys to export these: import { ToolCallArray, ToolResultArray } from 'ai'; So that no one struggles with typing. Thank you for your hard work! |
Beta Was this translation helpful? Give feedback.
wait we already export toolcallunion lol