[PR #2674] [MERGED] fix(anthropic): trim trailing whitespace from the final assistant turn before send (#2673, #2679) #5715

Closed
opened 2026-10-22 04:18:46 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2674
Author: @kolacheee
Created: 6/20/2026
Status: ✅ Merged
Merged: 6/21/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/anthropic-prefill-trailing-whitespace


📝 Commits (2)

  • cdee7e8 fix(prompt): trim trailing whitespace from assistant prefill before send (#2673)
  • 7501a09 fix(anthropic): trim trailing whitespace from the final assistant turn at serialization (#2679)

📊 Changes

3 files changed (+38 additions, -5 deletions)

View changed files

📝 packages/server/src/routes/generate/dry-run-route.ts (+3 -1)
📝 packages/server/src/routes/generate/generate-route-utils.ts (+5 -1)
📝 packages/server/src/services/llm/providers/anthropic.provider.ts (+30 -3)

📄 Description

Linked issues

Closes #2673
Closes #2679

Why this change

Reports of "presets that work in SillyTavern get blocked/refused in Marinara" trace, in part, to a literal API error being surfaced as a refusal. Anthropic rejects a final assistant turn whose content ends in whitespace with HTTP 400 (final assistant content must not end with trailing whitespace). Trailing newlines/spaces on the final assistant turn are extremely common, so a large fraction of Anthropic generations fail — and the failure looks like a content block to the user.

This PR fixes that in two layers:

  1. Prefill case (#2673): a preset's assistant prefill was sent as the trailing assistant message untrimmed. Prefills ending in \n or a space (<thinking>\n, Sure, ) are extremely common.
  2. No-prefill case (#2679): the prefill-only fix lives in the prefill helper, so it misses turns with no prefill. The trailing assistant message can instead be a depth-injected role:assistant section, or — under markdown/none wrap — the last chat-history assistant message. These still reached Anthropic untrimmed and produced the same 400.

What changed

  • Prefill (#2673): appendGenerationTailMessages (generate-route-utils.ts) now pushes assistantPrefill.trimEnd() as the trailing assistant message instead of the untrimmed value. The dry-run preview (dry-run-route.ts) is updated the same way so the preview matches what is actually sent.
  • Serialization (#2679): new trimTrailingAssistantWhitespace() in anthropic.provider.ts strips the trailing edge of the last assistant message at the point of serialization, applied in both chat() (after mergeConsecutiveMessages) and chatComplete() (before formatAnthropicPayloadMessages). This centralizes the guard so every trailing-assistant surface — prefill, depth-injected assistant, merged assistant, history assistant — is covered in one place, generalizing the prefill fix.

Only the trailing edge Anthropic rejects is stripped; leading whitespace and non-trailing turns are preserved, and user-facing prefill rendering is unchanged. Harmless for OpenAI / Google (they do not reject trailing whitespace).

Validation

ℹ️ Boxes are intentionally left unchecked — per CONTRIBUTING.md / CLAUDE.md, these are a human to-do list, not proof of work by an agent.

Automated (done by the agent): pnpm check passed locally — TypeScript + ESLint + build (exit 0) — for both the prefill change and the serialization change.

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

On an Anthropic connection:

  • Prefill (#2673): set a preset/chat assistant prefill ending in a newline or space (e.g. <thinking>\n) and send → should succeed (previously 400'd). Confirm the displayed reply still begins with the intended prefill.
  • No-prefill, depth-injected (#2679): with an empty prefill, add a depth-0 role:assistant injected section / lorebook entry whose content ends in a newline, and send → should succeed (previously 400'd).
  • No-prefill, history (#2679): with an empty prefill and wrapFormat = markdown or none, make the most recent chat-history message an assistant message ending in \n, and send → should succeed.
  • Confirm OpenAI / Google generations with a prefill are unchanged.
  • Confirm the dry-run preview's trailing assistant message matches the sent request.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

No docs/version changes.

UI evidence (if applicable)

N/A — server-only change.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Fixed improper whitespace handling in assistant message content during prompt generation and API request formatting, improving reliability of generation workflows when using assistant prefill.
    • Enhanced assistant content processing to ensure consistent validation and formatting across dry-run operations, generation requests, and message API interactions.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2674 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 6/20/2026 **Status:** ✅ Merged **Merged:** 6/21/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/anthropic-prefill-trailing-whitespace` --- ### 📝 Commits (2) - [`cdee7e8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cdee7e8f7110833cd14be6afc95df287f96ef0af) fix(prompt): trim trailing whitespace from assistant prefill before send (#2673) - [`7501a09`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7501a0934e4a076d9a4c85e218ce8930fabd2749) fix(anthropic): trim trailing whitespace from the final assistant turn at serialization (#2679) ### 📊 Changes **3 files changed** (+38 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate/dry-run-route.ts` (+3 -1) 📝 `packages/server/src/routes/generate/generate-route-utils.ts` (+5 -1) 📝 `packages/server/src/services/llm/providers/anthropic.provider.ts` (+30 -3) </details> ### 📄 Description <!-- Target branch: `staging` --> ## Linked issues Closes #2673 Closes #2679 ## Why this change Reports of "presets that work in SillyTavern get blocked/refused in Marinara" trace, in part, to a literal API error being surfaced as a refusal. Anthropic rejects a final **assistant** turn whose content ends in whitespace with **HTTP 400** (`final assistant content must not end with trailing whitespace`). Trailing newlines/spaces on the final assistant turn are extremely common, so a large fraction of Anthropic generations fail — and the failure looks like a content block to the user. This PR fixes that in two layers: 1. **Prefill case (#2673):** a preset's assistant **prefill** was sent as the trailing assistant message **untrimmed**. Prefills ending in `\n` or a space (`<thinking>\n`, `Sure, `) are extremely common. 2. **No-prefill case (#2679):** the prefill-only fix lives in the prefill helper, so it misses turns with **no prefill**. The trailing assistant message can instead be a depth-injected `role:assistant` section, or — under `markdown`/`none` wrap — the last chat-history assistant message. These still reached Anthropic untrimmed and produced the same 400. ## What changed - **Prefill (#2673):** `appendGenerationTailMessages` (`generate-route-utils.ts`) now pushes `assistantPrefill.trimEnd()` as the trailing assistant message instead of the untrimmed value. The dry-run preview (`dry-run-route.ts`) is updated the same way so the preview matches what is actually sent. - **Serialization (#2679):** new `trimTrailingAssistantWhitespace()` in `anthropic.provider.ts` strips the trailing edge of the **last assistant message at the point of serialization**, applied in both `chat()` (after `mergeConsecutiveMessages`) and `chatComplete()` (before `formatAnthropicPayloadMessages`). This centralizes the guard so **every** trailing-assistant surface — prefill, depth-injected assistant, merged assistant, history assistant — is covered in one place, generalizing the prefill fix. Only the trailing edge Anthropic rejects is stripped; leading whitespace and non-trailing turns are preserved, and user-facing prefill rendering is unchanged. Harmless for OpenAI / Google (they do not reject trailing whitespace). ## Validation > ℹ️ Boxes are intentionally left unchecked — per `CONTRIBUTING.md` / `CLAUDE.md`, these are a human to-do list, not proof of work by an agent. **Automated (done by the agent):** `pnpm check` passed locally — TypeScript + ESLint + build (exit `0`) — for both the prefill change and the serialization change. - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes On an **Anthropic** connection: - **Prefill (#2673):** set a preset/chat assistant prefill ending in a newline or space (e.g. `<thinking>\n`) and send → should succeed (previously `400`'d). Confirm the displayed reply still begins with the intended prefill. - **No-prefill, depth-injected (#2679):** with an empty prefill, add a depth-0 `role:assistant` injected section / lorebook entry whose content ends in a newline, and send → should succeed (previously `400`'d). - **No-prefill, history (#2679):** with an empty prefill and `wrapFormat` = `markdown` or `none`, make the most recent chat-history message an assistant message ending in `\n`, and send → should succeed. - Confirm OpenAI / Google generations with a prefill are unchanged. - Confirm the dry-run preview's trailing assistant message matches the sent request. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) No docs/version changes. ## UI evidence (if applicable) N/A — server-only change. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Fixed improper whitespace handling in assistant message content during prompt generation and API request formatting, improving reliability of generation workflows when using assistant prefill. * Enhanced assistant content processing to ensure consistent validation and formatting across dry-run operations, generation requests, and message API interactions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:46 +07:00
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#5715
No description provided.