Claude 4.6+ rejects requests containing consecutive assistant messages,
treating the trailing one as an unsupported prefill. This occurs when
CrewAgentExecutor appends multiple assistant messages during tool-use
iterations in the ReAct loop.
Added _merge_consecutive_messages() static method to BaseLLM that
merges consecutive same-role messages by joining their text content
with double newlines. Messages with list content (tool-use blocks,
multimodal) are left untouched.
The merge is called in both:
- LLM._format_messages_for_provider() for the LiteLLM path
- AnthropicCompletion._format_messages_for_anthropic() for native path
Added 7 unit tests covering:
- Consecutive assistant message merging (LiteLLM and native paths)
- Alternating messages preserved
- List content not merged
- Original messages not mutated
- Non-Anthropic models unaffected
- Consecutive user messages also merged
Fixes#4798
Co-Authored-By: João <joao@crewai.com>