vercel/ai · error · InvalidPromptError

DeepSeek assistant prefix completion requires the prefixed a

Error message

DeepSeek assistant prefix completion requires the prefixed assistant message to be the final message.

What it means

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.

Source

Thrown at packages/deepseek/src/chat/convert-to-deepseek-chat-messages.ts:269

              feature: `user message part type: ${part.type}`,
            });
          }
        }

        messages.push({
          role: 'user',
          content: userContent,
          ...(deepseekMessageOptions?.name != null && {
            name: deepseekMessageOptions.name,
          }),
        });

        break;
      }
      case 'assistant': {
        if (deepseekMessageOptions?.prefix === true) {
          if (index !== prompt.length - 1) {
            throw new InvalidPromptError({
              prompt,
              message:
                'DeepSeek assistant prefix completion requires the prefixed assistant message to be the final message.',
            });
          }

          if (!supportsAssistantPrefixCompletion) {
            throw new UnsupportedFunctionalityError({
              functionality: 'DeepSeek assistant prefix completion',
              message:
                'DeepSeek assistant prefix completion requires a beta base URL ending in `/beta`.',
            });
          }
        }

        let text = '';
        let reasoning: string | undefined;

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Move the prefixed assistant message to the end of the messages array
  2. Remove trailing messages after the prefixed assistant message
  3. Drop prefix: true if mid-conversation steering was intended (it is unsupported)

Example fix

// before
messages: [
  { role: 'assistant', content: 'The capital of France is', providerOptions: { deepseek: { prefix: true } } },
  { role: 'user', content: 'thanks' }
]
// after
messages: [
  { role: 'user', content: 'What is the capital of France?' },
  { role: 'assistant', content: 'The capital of France is', providerOptions: { deepseek: { prefix: true } } }
]
Defensive patterns

Strategy: validation

Validate before calling

function assertPrefixIsLast(messages) {
  const i = messages.findIndex(m => m.providerOptions?.deepseek?.prefix === true);
  if (i !== -1 && i !== messages.length - 1) {
    throw new Error('prefixed assistant message must be the final message');
  }
}

Type guard

function isFinalPrefixed(messages): boolean {
  const last = messages[messages.length - 1];
  return !!last && last.role === 'assistant' && last.providerOptions?.deepseek?.prefix === true || !messages.some(m => m.providerOptions?.deepseek?.prefix === true);
}

Try / catch

try { await generateText(...); } catch (e) { if (InvalidPromptError.isInstance(e) && /final message/.test(e.message)) { /* reorder messages */ } throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/0bfeb6e4f34ea1e6. Report an issue: GitHub.