Tool Invocation 결과 누락 오류
Tool Invocation 결과 누락 오류 (Tool Invocation Missing Result Error)
generateText()나 streamText()를 쓸 때 "ToolInvocation must have a result" 오류가 나는 상황을 다루는 문서예요. execute 함수가 없는 도구를 호출했을 때 이 오류가 나며, 도구 결과를 서버에서 처리하거나 클라이언트에서 반드시 제공해야 한다는 게 핵심이에요.
출처: 문서
본문
문제 (Issue)
generateText() 또는 streamText()를 사용할 때, execute 함수가 없는 도구가 호출되면 "ToolInvocation must have a result" 오류가 발생할 수 있습니다.
원인 (Cause)
execute 함수가 없는 도구를 정의하고, 다른 수단(예: useChat의 onToolCall이나 addToolOutput 함수)으로 결과를 제공하지 않으면 오류가 발생합니다.
도구가 호출될 때마다 모델은 대화를 계속하기 전에 결과를 받을 것을 기대합니다. 결과가 없으면 모델은 도구 호출이 성공했는지 실패했는지 판단할 수 없고 대화 상태가 유효하지 않게 됩니다.
해결 방법 (Solution)
도구 결과를 처리하는 방법은 두 가지가 있습니다:
execute함수가 있는 도구를 사용한 서버 측 실행:
const tools = {
weather: tool({
description: 'Get the weather in a location',
inputSchema: z.object({
location: z
.string()
.describe('The city and state, e.g. "San Francisco, CA"'),
}),
execute: async ({ location }) => {
// Fetch and return weather data
return { temperature: 72, conditions: 'sunny', location };
},
}),
};
useChat를 사용한 클라이언트 측 실행(execute함수 생략)에서는addToolOutput으로 결과를 반드시 제공해야 합니다:
import { useChat } from '@ai-sdk/react';
import {
DefaultChatTransport,
lastAssistantMessageIsCompleteWithToolCalls,
} from 'ai';
const { messages, sendMessage, addToolOutput } = useChat({
// Automatically submit when all tool results are available
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
// Handle tool calls in onToolCall
onToolCall: async ({ toolCall }) => {
if (toolCall.toolName === 'getLocation') {
try {
const result = await getLocationData();
// Important: Don't await inside onToolCall to avoid deadlocks
addToolOutput({
tool: 'getLocation',
toolCallId: toolCall.toolCallId,
output: result,
});
} catch (err) {
// Important: Don't await inside onToolCall to avoid deadlocks
addToolOutput({
tool: 'getLocation',
toolCallId: toolCall.toolCallId,
state: 'output-error',
errorText: 'Failed to get location',
});
}
}
},
});
// For interactive UI elements:
const { messages, sendMessage, addToolOutput } = useChat({
transport: new DefaultChatTransport({ api: '/api/chat' }),
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
});
// Inside your JSX, when rendering tool calls:
<button
onClick={() =>
addToolOutput({
tool: 'myTool',
toolCallId, // must provide tool call ID
output: {
/* your tool result */
},
})
}
>
Confirm
</button>;
경고: 서버에서든 클라이언트에서든 도구를 처리하든, 대화가 계속되기 전에 각 도구 호출에는 대응하는 결과가 있어야 합니다.
더 알아보기 (Learn more)
- Azure OpenAI 스트리밍 느림
- 클라이언트 컴포넌트에서의 Server Actions
- useChat/useCompletion 스트림 출력에 0:... 가 텍스트 대신 표시됨
- Streamable UI 오류
- Tool Invocation 결과 누락 오류
- 배포했을 때 스트리밍 안 됨
- 프록시를 거칠 때 스트리밍 안 됨
- Vercel에 배포할 때 타임아웃
- 닫히지 않은 스트림
- useChat 스트림 파싱 실패
- Server Action plain objects 오류
- useChat 응답 없음
- useChat에서 커스텀 headers/body/credentials가 동작하지 않음
- Zod와 AI SDK 5에서 TypeScript 성능 문제
- useChat "An error occurred" 오류
- useChat에서 assistant 메시지 반복
- 스트림이 중단될 때 onEnd가 호출되지 않음
- 구조화된 출력과 함께하는 도구 호출
- 중단(abort)과 이어서 재개 가능한 스트림
- streamText가 조용히 실패함
- 스트리밍 상태는 표시되는데 텍스트가 안 나옴
- useChat과 함께하는 stale body 값
- onToolCall과 함께하는 타입 오류
- 지원되지 않는 모델 버전 오류
- OpenAI에서 객체 생성 실패
- 도구 결과 누락 오류
- 모델이 "LanguageModelV1" 타입에 할당되지 않음
- TypeScript 오류 "Cannot find namespace 'JSX'"
- React 오류 "Maximum update depth exceeded"
- Jest: '@ai-sdk/rsc' 모듈을 찾을 수 없음
- 많은 이미지를 처리할 때 높은 메모리 사용