Skip to content

Function: repairAssistantPrefill()

repairAssistantPrefill(messages): boolean

Repair a Mastra/AI SDK message replay that Databricks-hosted Claude rejects with "This model does not support assistant message prefill. The conversation must end with a user message.".

The bug pattern: when an assistant turn streams text and a tool_call, the AI SDK persists them as two separate assistant entries (text-only and tool-call-only). On the next agent step the tool-call entry is replayed before the tool result and the text entry is replayed after it, so the conversation ends with a trailing assistant text message. Anthropic interprets that as a prefill request and rejects it on Databricks (the upstream Bedrock route disallows prefill).

Fix: when the last message is an assistant text with no tool_calls and the chain immediately before it is assistant(tool_calls=...) followed only by tool(...) results, fold the trailing text back into the content of that opening assistant and drop the duplicate.

ServingChatMessage[]

boolean