[GH-ISSUE #2673] [Issue]: Assistant prefill sent to Anthropic with trailing whitespace → HTTP 400 (perceived as a refusal/block) #1320

Closed
opened 2026-10-21 05:31:31 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2673

Originally assigned to: @kolacheee on GitHub.

Summary

A preset's assistant prefill is pushed as the trailing assistant message untrimmed, while the trimmed value is used only as a presence guard. Anthropic's Messages API rejects a final assistant message that ends in whitespace with HTTP 400 ("final assistant content cannot end with trailing whitespace"), which users perceive as a content block/refusal. Prefills ending in \n or a space are common, so this breaks many Anthropic generations that work fine in SillyTavern.

Expected behavior

A prefill that ends in whitespace (e.g. <thinking>\n) should still produce a successful Anthropic generation — the trailing edge should be stripped from what is sent to the API (the user-facing prefill rendering is separate), matching SillyTavern's behavior.

Actual behavior

appendGenerationTailMessages computes const assistantPrefill = options.assistantPrefill.trim() (used only as the guard) but pushes the untrimmed options.assistantPrefill as the final assistant message. On Anthropic that trailing assistant turn ends in whitespace → HTTP 400 → the generation fails and looks like a refusal/block.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • packages/server/src/routes/generate/generate-route-utils.ts:540-543 — appendGenerationTailMessages: trims to a local for the guard, but pushes content: options.assistantPrefill (untrimmed).
  • The prefill is the trailing assistant message for Anthropic (the Google user-regeneration append only fires for Google).
  • packages/server/src/services/llm/providers/anthropic.provider.ts emits the trailing assistant content untrimmed (formatAnthropicPayloadMessages ~line 194 text block; the string-content path), and mergeAnthropicPayloadMessages does not trim.
  • Anthropic Messages API → 400 "final assistant content cannot end with trailing whitespace".
  • The dry-run preview has the same pattern (dry-run-route.ts:1438-1439), so the preview would not reveal it.

Steps to reproduce

  1. On an Anthropic (Claude-direct) connection, set a preset/connection/chat assistant prefill that ends in whitespace — e.g. <thinking>\n or Sure, (trailing space).
  2. Send any message.
  3. Observe: the request's final assistant message ends in whitespace; Anthropic responds 400; the generation fails and appears to the user as a block/refusal. The same prefill works in SillyTavern (which strips the trailing edge).

Suggested fix

Push options.assistantPrefill.trimEnd() as the trailing assistant message content (strip the trailing edge Anthropic rejects; leading whitespace is fine and preserved). Mirror the same in the dry-run preview (dry-run-route.ts) so the preview matches what is sent. Harmless for OpenAI/Google (they do not reject trailing whitespace), and the user-facing prefill rendering is unaffected.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Identified via a source-level audit of the prompt-assembly / prefill path on staging, prompted by reports of presets that work on SillyTavern producing blocks/refusals on Marinara. This is one concrete cause; a broader prompt-ordering audit (e.g. system-message placement under strictRoleFormatting) is ongoing.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2673 Originally assigned to: @kolacheee on GitHub. ## Summary A preset's assistant **prefill** is pushed as the trailing assistant message **untrimmed**, while the trimmed value is used only as a presence guard. Anthropic's Messages API rejects a final assistant message that ends in whitespace with **HTTP 400** ("final assistant content cannot end with trailing whitespace"), which users perceive as a content block/refusal. Prefills ending in `\n` or a space are common, so this breaks many Anthropic generations that work fine in SillyTavern. ## Expected behavior A prefill that ends in whitespace (e.g. `<thinking>\n`) should still produce a successful Anthropic generation — the trailing edge should be stripped from what is sent to the API (the user-facing prefill rendering is separate), matching SillyTavern's behavior. ## Actual behavior `appendGenerationTailMessages` computes `const assistantPrefill = options.assistantPrefill.trim()` (used only as the guard) but pushes the **untrimmed** `options.assistantPrefill` as the final assistant message. On Anthropic that trailing assistant turn ends in whitespace → HTTP 400 → the generation fails and looks like a refusal/block. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - `packages/server/src/routes/generate/generate-route-utils.ts:540-543` — `appendGenerationTailMessages`: trims to a local for the guard, but pushes `content: options.assistantPrefill` (untrimmed). - The prefill is the trailing assistant message for Anthropic (the Google user-regeneration append only fires for Google). - `packages/server/src/services/llm/providers/anthropic.provider.ts` emits the trailing assistant content untrimmed (`formatAnthropicPayloadMessages` ~line 194 text block; the string-content path), and `mergeAnthropicPayloadMessages` does not trim. - Anthropic Messages API → `400` "final assistant content cannot end with trailing whitespace". - The dry-run preview has the same pattern (`dry-run-route.ts:1438-1439`), so the preview would not reveal it. ## Steps to reproduce 1. On an Anthropic (Claude-direct) connection, set a preset/connection/chat assistant prefill that ends in whitespace — e.g. `<thinking>\n` or `Sure, ` (trailing space). 2. Send any message. 3. Observe: the request's final assistant message ends in whitespace; Anthropic responds `400`; the generation fails and appears to the user as a block/refusal. The same prefill works in SillyTavern (which strips the trailing edge). ## Suggested fix Push `options.assistantPrefill.trimEnd()` as the trailing assistant message content (strip the trailing edge Anthropic rejects; leading whitespace is fine and preserved). Mirror the same in the dry-run preview (`dry-run-route.ts`) so the preview matches what is sent. Harmless for OpenAI/Google (they do not reject trailing whitespace), and the user-facing prefill rendering is unaffected. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Identified via a source-level audit of the prompt-assembly / prefill path on staging, prompted by reports of presets that work on SillyTavern producing blocks/refusals on Marinara. This is one concrete cause; a broader prompt-ordering audit (e.g. system-message placement under `strictRoleFormatting`) is ongoing. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:31:31 +07:00
Author
Owner

@kolacheee commented on GitHub (Jun 20, 2026):

assign me pls

<!-- gh-comment-id:4760176526 --> @kolacheee commented on GitHub (Jun 20, 2026): assign me pls
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1320
No description provided.