{"record":{"id":"0bfeb6e4f34ea1e6","repo":"vercel/ai","slug":"deepseek-assistant-prefix-completion-requires-the","errorCode":null,"errorMessage":"DeepSeek assistant prefix completion requires the prefixed assistant message to be the final message.","messagePattern":"DeepSeek assistant prefix completion requires the prefixed assistant message to be the final message\\.","errorType":"exception","errorClass":"InvalidPromptError","httpStatus":null,"severity":"error","filePath":"packages/deepseek/src/chat/convert-to-deepseek-chat-messages.ts","lineNumber":269,"sourceCode":"              feature: `user message part type: ${part.type}`,\n            });\n          }\n        }\n\n        messages.push({\n          role: 'user',\n          content: userContent,\n          ...(deepseekMessageOptions?.name != null && {\n            name: deepseekMessageOptions.name,\n          }),\n        });\n\n        break;\n      }\n      case 'assistant': {\n        if (deepseekMessageOptions?.prefix === true) {\n          if (index !== prompt.length - 1) {\n            throw new InvalidPromptError({\n              prompt,\n              message:\n                'DeepSeek assistant prefix completion requires the prefixed assistant message to be the final message.',\n            });\n          }\n\n          if (!supportsAssistantPrefixCompletion) {\n            throw new UnsupportedFunctionalityError({\n              functionality: 'DeepSeek assistant prefix completion',\n              message:\n                'DeepSeek assistant prefix completion requires a beta base URL ending in `/beta`.',\n            });\n          }\n        }\n\n        let text = '';\n        let reasoning: string | undefined;\n","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/deepseek/src/chat/convert-to-deepseek-chat-messages.ts#L251-L287","documentation":"DeepSeek assistant prefix completion requires the prefixed assistant message to be the last message in the prompt. The SDK throws InvalidPromptError when a message with deepseek.prefix === true appears at an index other than prompt.length - 1.","triggerScenarios":"Placing prefix: true on an assistant message that is followed by more messages — e.g. prefixing the first assistant turn of a multi-turn conversation instead of the final one.","commonSituations":"Trying to force the model to continue from an early turn; history-building code that appends a system or user message after the prefixed assistant message; caching examples showing mid-conversation prefixes.","solutions":["Move the prefixed assistant message to the end of the messages array","Remove trailing messages after the prefixed assistant message","Drop prefix: true if mid-conversation steering was intended (it is unsupported)"],"exampleFix":"// before\nmessages: [\n  { role: 'assistant', content: 'The capital of France is', providerOptions: { deepseek: { prefix: true } } },\n  { role: 'user', content: 'thanks' }\n]\n// after\nmessages: [\n  { role: 'user', content: 'What is the capital of France?' },\n  { role: 'assistant', content: 'The capital of France is', providerOptions: { deepseek: { prefix: true } } }\n]","handlingStrategy":"validation","validationCode":"function assertPrefixIsLast(messages) {\n  const i = messages.findIndex(m => m.providerOptions?.deepseek?.prefix === true);\n  if (i !== -1 && i !== messages.length - 1) {\n    throw new Error('prefixed assistant message must be the final message');\n  }\n}","typeGuard":"function isFinalPrefixed(messages): boolean {\n  const last = messages[messages.length - 1];\n  return !!last && last.role === 'assistant' && last.providerOptions?.deepseek?.prefix === true || !messages.some(m => m.providerOptions?.deepseek?.prefix === true);\n}","tryCatchPattern":"try { await generateText(...); } catch (e) { if (InvalidPromptError.isInstance(e) && /final message/.test(e.message)) { /* reorder messages */ } throw e; }","preventionTips":["Always append the prefixed assistant message last after building history","Never append system/user messages after a prefixed assistant turn","Add an ordering assertion in your chat-history builder tests"],"tags":["deepseek","prefix-completion","message-order"],"backgroundTag":"invalid-message-order","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}