Skip to content

fix(ai-orchestrator): resolve Anthropic 400 error (assistant message prefill)#139

Merged
SteveJRobertson merged 2 commits into
mainfrom
138-fix-anthropic-prefill-error
Jun 3, 2026
Merged

fix(ai-orchestrator): resolve Anthropic 400 error (assistant message prefill)#139
SteveJRobertson merged 2 commits into
mainfrom
138-fix-anthropic-prefill-error

Conversation

@SteveJRobertson

Copy link
Copy Markdown
Owner

Summary

Closes #138

The Anthropic API returns a 400 Bad Request error when the a11y persona is invoked after the mesh router, with the error: This model does not support assistant message prefill. The conversation must end with a user message.

This is fixed by adding a prefill guard that detects when the message array ends with an AIMessage and appends a HumanMessage before invoking the Anthropic client. This is Anthropic-specific; OpenAI and other models are permissive.

Changes

  • File: libs/ai-orchestrator/src/orchestrator/llm-persona-node.ts

    • Extract isAnthropicModel check before client instantiation for reuse
    • Add prefill guard (lines 75–84): if Anthropic client and last message is AIMessage, append HumanMessage with content: "Please proceed with your task based on the context above."
  • File: libs/ai-orchestrator/src/orchestrator/llm-persona-node.spec.ts

Test Results

  • 279 tests passing (including 3 new prefill guard tests)
  • Type checking: No errors
  • Linting: No errors

Copilot Review Triage

  • Blocker — No security, correctness, or async/error handling issues
  • Major — No missing error paths, incomplete logic, or type unsafety
  • Minor — All edge cases covered by tests
  • Nit — Code style and naming consistent with project

Deferred follow-ups

None. This fix is complete and ready to merge. After merge, webhook-listener service on Mac Mini will require a rebuild and restart to consume the updated ai-orchestrator library.

…age error

- Extract isAnthropicModel check before client instantiation
- Add prefill guard: if Anthropic client and last message is AIMessage, append HumanMessage
- Guard message: 'Please proceed with your task based on the context above.'
- Add 3 new test cases validating guard behavior:
  * Appends HumanMessage for Anthropic when needed
  * Does NOT append for OpenAI (already permissive)
  * Does NOT append when message ends with HumanMessage (already valid)
- All 279 tests passing, no type/lint errors
- Fixes issue #138: a11y persona 400 error after mesh router
Copilot AI review requested due to automatic review settings June 3, 2026 21:20
@vercel

vercel Bot commented Jun 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
isolate-ui Ignored Ignored Preview Jun 3, 2026 9:25pm

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an Anthropic-specific 400 error in the ai-orchestrator persona node flow by guarding against “assistant message prefill” scenarios when invoking Claude models after mesh routing.

Changes:

  • Refactors Anthropic model detection into a reusable isAnthropicModel boolean before client selection.
  • Adds an Anthropic prefill guard that appends a fallback HumanMessage when the prompt would otherwise end in an AIMessage.
  • Adds unit tests covering the Anthropic guard behavior and ensuring OpenAI behavior is unchanged.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
libs/ai-orchestrator/src/orchestrator/llm-persona-node.ts Adds Anthropic-specific prompt-ending guard before invoking the LLM client.
libs/ai-orchestrator/src/orchestrator/llm-persona-node.spec.ts Adds tests validating the guard for Anthropic and non-guard behavior for OpenAI / already-human-ending prompts.

Comment thread libs/ai-orchestrator/src/orchestrator/llm-persona-node.ts
Comment thread libs/ai-orchestrator/src/orchestrator/llm-persona-node.spec.ts Outdated
…d logic

- Change guard condition from 'if AIMessage' to 'if NOT HumanMessage'
  to handle edge case where state.messages is empty (only SystemMessage in array)
- Remove unused humanMessageCount variable; add explicit array length assertion
- Add 4th test case: empty state.messages with guard appending HumanMessage
- All 280 tests passing

Addresses review comments:
- PRRT_kwDORem8Z86G5a8s: Ensure guard handles SystemMessage-only case
- PRRT_kwDORem8Z86G5a9P: Remove unused variable, assert on array length
@SteveJRobertson SteveJRobertson merged commit 6dd84e7 into main Jun 3, 2026
6 checks passed
@SteveJRobertson SteveJRobertson deleted the 138-fix-anthropic-prefill-error branch June 3, 2026 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: resolve Anthropic 400 error (assistant message prefill) in LLM persona node

2 participants