anthropic.internal._render_messages

(internal) The API wants user and assistant to alternate, and several paths here can leave two of the same role side by side: a dropped assistant turn (a reasoning-only reply lowers to no blocks) leaves the user turns that bracketed it adjacent, and the rendered prompt can end on the role the journal opens with. The leading-role repair above only fixes an assistant-FIRST list, so a mid-conversation clash reached the wire.

Reference version

Signature

function anthropic.internal._render_messages(

(internal) The API wants user and assistant to alternate, and several paths here can leave two of the same role side by side: a dropped assistant turn (a reasoning-only reply lowers to no blocks) leaves the user turns that bracketed it adjacent, and the rendered prompt can end on the role the journal opens with. The leading-role repair above only fixes an assistant-FIRST list, so a mid-conversation clash reached the wire.

Concatenating the block lists of same-role neighbours repairs every such shape. Block order is preserved and messages are joined in order, so a cache_control marker still sits on exactly the block it was placed on. _push_tool_result's grouping becomes a special case of this.

Source:<builtin>/anthropic/ns_internal/messages.bamlbytes 2232022815