Skip to content

fix: MOIM injection causing trailing assistant message (#7425)#7459

Closed
michaelneale wants to merge 1 commit into
mainfrom
fix/moim-trailing-assistant-v2
Closed

fix: MOIM injection causing trailing assistant message (#7425)#7459
michaelneale wants to merge 1 commit into
mainfrom
fix/moim-trailing-assistant-v2

Conversation

@michaelneale
Copy link
Copy Markdown
Collaborator

Tentative fix for #7425.

When a conversation ends with a text-only assistant message (no tool call), MOIM was inserted before it, leaving the assistant message trailing. Anthropic rejects this with 400 'conversation must end with user message'.

Fix: insert MOIM after the last assistant message when it's the final message. Includes regression test.

Note: supersedes the approach on fix/moim-trailing-assistant with a cleaner match-based implementation and a more thorough regression test that also verifies the trailing assistant message is preserved (not dropped).

When the conversation ends with a text-only assistant message (no tool
call), inject_moim was inserting the MOIM user message *before* it,
leaving the assistant message trailing. This violates the API constraint
that conversations must end with a user message, producing a 400 error
from Anthropic.

Fix: detect when the last assistant message is also the last message in
the conversation, and insert MOIM *after* it instead of before. This
ensures the conversation always ends with a user message while preserving
the trailing assistant content.
@michaelneale
Copy link
Copy Markdown
Collaborator Author

hey @tlongwell-block does this one make sense?

@michaelneale
Copy link
Copy Markdown
Collaborator Author

I think this may make sense @angiejones

@michaelneale michaelneale marked this pull request as ready for review February 24, 2026 02:29
Copilot AI review requested due to automatic review settings February 24, 2026 02:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical bug (#7425) where MOIM injection causes API errors when a conversation ends with a text-only assistant message. The fix modifies the inject_moim function to insert MOIM after (instead of before) a trailing assistant message, ensuring conversations always end with a user message as required by the Anthropic API.

Changes:

  • Updated MOIM injection logic to detect when the last message is an assistant message and insert MOIM after it in that case
  • Added comprehensive regression test verifying the fix and ensuring the trailing assistant message is preserved

@tlongwell-block
Copy link
Copy Markdown
Collaborator

Fixed in #7424

@tlongwell-block
Copy link
Copy Markdown
Collaborator

tlongwell-block commented Feb 24, 2026

Fat-fingered that close, sorry! Yeah, @michaelneale we looked at this in #7421

It is not caused by the MOIM. Just truncated tool call requests

Also, it is important that the MOIM not be the latest message in context or it distracts goose too much

@michaelneale
Copy link
Copy Markdown
Collaborator Author

@tlongwell-block well given that, should we close this one anyway?

@tlongwell-block
Copy link
Copy Markdown
Collaborator

Yes, I think so. It just felt rude closing your PR 🙃

@jamadeo jamadeo closed this Feb 28, 2026
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.

4 participants