Skip to content

@sqlrooms/ai-core / fixIncompleteToolCalls

Function: fixIncompleteToolCalls() ​

fixIncompleteToolCalls(messages, incompleteToolError?, options?): UIMessage<unknown, UIDataTypes, UITools>[]

Validates and completes UIMessages to ensure all tool-call parts have corresponding tool-result parts. This is important when canceling with AbortController, which may leave incomplete tool-calls. Assumes sequential tool execution (only one tool runs at a time).

For any output-error tool part (cancelled or failed), the partial/invalid input is moved to rawInput and input is set to undefined. The AI SDK only validates output-error input when it is defined, so this prevents "Type validation failed ... parts[n].input" errors from poisoning a session after the user stops a tool call mid-stream.

Parameters ​

ParameterTypeDefault valueDescription
messagesUIMessage<unknown, UIDataTypes, UITools>[]undefinedThe messages to validate and complete
incompleteToolErrorstringTOOL_CALL_CANCELLEDError text for synthesized tool results
options{ completeApprovalRequests?: boolean; }{}Completion behavior for states normally kept interactive
options.completeApprovalRequests?booleanundefined-

Returns ​

UIMessage<unknown, UIDataTypes, UITools>[]

Cleaned messages with completed tool-call/result pairs