Skip to content

fix: persist all messages in multi-step tool calls#2

Open
seer-by-sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/chat-history-tool-calls
Open

fix: persist all messages in multi-step tool calls#2
seer-by-sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/chat-history-tool-calls

Conversation

@seer-by-sentry

@seer-by-sentry seer-by-sentry Bot commented Jun 3, 2026

Copy link
Copy Markdown

This PR addresses an AI_APICallError where Anthropic rejected API calls due to tool_use IDs without corresponding tool_result blocks immediately after them.

Root Cause:
The onFinish handler in app/(chat)/api/chat/route.ts was incorrectly saving only a single assistant message from the appendResponseMessages output. When streamText used tools with maxSteps > 1, it generated multiple messages (e.g., assistant[tool_use], user[tool_result], assistant[text]). However, only the first assistant message (often the tool_use block) was persisted to the database, leading to a corrupted conversation history where tool_use blocks were not followed by tool_result blocks.

Solution:
The onFinish handler has been updated to correctly save all relevant messages generated by appendResponseMessages (excluding the initial user message). This ensures that tool_use and tool_result pairs, along with the final assistant response, are fully persisted in the database, maintaining a valid conversation history for the LLM API.

Fixes SUPERMEMORY-BACKEND-ZW3

@vercel

vercel Bot commented Jun 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
infinite-chat Error Error Jun 3, 2026 11:47pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants