Skip to main content

push_message_block

Function push_message_block 

Source
pub fn push_message_block(
    messages: &mut Vec<BetaMessageParam>,
    role: BetaMessageRole,
    block: BetaContentBlockParam,
)
Expand description

Append a single content block to a Claude messages list, building a well-formed conversation as we go:

  • Consecutive blocks for the same role are merged into one message, instead of producing two adjacent same-role messages (which the Claude API rejects).
  • Whenever a tool_result block is appended to a user message, we make sure the immediately preceding assistant message contains a matching tool_use block. If none exists (for example, when a client uses the OpenAI Responses API with previous_response_id and only sends new function_call_output items), we synthesize a placeholder tool_use so the request still satisfies the API’s pairing rule: “Each tool_result block must have a corresponding tool_use block in the previous message.”

Use this helper from every transform that produces Claude messages from a non-Claude source. It centralises the invariants so each converter can stay focused on its own input format.