buildToolResponsesJson static method

String buildToolResponsesJson(
  1. List<({String name, Object? response})> responses
)

Build the role-tool message that returns a turn's tool results to the model. One message carries every result, so a model that made parallel calls reads all of their answers before it continues. The item shape mirrors upstream Python Conversation._handle_tool_calls ({"type": "tool_response", "name", "response"}), which the Gemma 4 and FunctionGemma data processors format natively — as <|tool_response>response:NAME{...}<tool_response|> and <start_function_response>response:NAME{...}<end_function_response>.

It has to be role tool. Sent as a user message, the template opens a new user turn and then a new model turn, and FunctionGemma answers that by calling the same function again instead of reading the result.

Implementation

static String buildToolResponsesJson(
  List<({String name, Object? response})> responses,
) => jsonEncode({
  'role': 'tool',
  'content': [
    for (final r in responses)
      {'type': 'tool_response', 'name': r.name, 'response': r.response},
  ],
});