extractToolCalls static method

List<FunctionCallResponse> extractToolCalls(
  1. String jsonStr
)

Extract all function calls from an SDK response JSON string.

Accepts the structural variants observed in the wild:

  1. Top-level tool_calls list (Gemma 4 standard path).
  2. content array entries with type: "tool_call" (multimodal).
  3. Concatenated multi-document JSON — when the model emits two or more <|tool_call>...<tool_call|> blocks, the SDK serializes them as separate {role:assistant,tool_calls:[...]}{role:assistant,tool_calls:[...]} documents back-to-back rather than wrapping them in an array. We split on top-level }{ boundaries and parse each fragment.

Each call element may be either OpenAI-style ({type: "function", function: {name, arguments}}) or flat ({name, arguments}) — both accepted.

String values inside arguments (and nested maps/lists) are stripped of the <|"|> Gemma 4 escape token, which leaks through SDK parsing.

Implementation

static List<FunctionCallResponse> extractToolCalls(String jsonStr) {
  final result = <FunctionCallResponse>[];
  for (final fragment in _splitConcatenatedJson(jsonStr)) {
    final Map<String, dynamic> json;
    try {
      final parsed = jsonDecode(fragment);
      if (parsed is! Map<String, dynamic>) continue;
      json = parsed;
    } on FormatException {
      continue;
    }
    _harvestCalls(json, result);
  }
  // Web fallback: `@litert-lm/core` (0.12.1 / 0.14.0) does NOT convert Gemma 4
  // `<|tool_call>call:NAME{...}<tool_call|>` tokens into structured
  // `tool_calls` JSON — they stay as raw text (verified with Gemma 4 E4B on
  // web). Native (C++ liblitert_lm) does convert, so this only fires when the
  // structured pass found nothing but raw tokens are present.
  if (result.isEmpty && jsonStr.contains(_rawToolCallOpen)) {
    _harvestRawTokenCalls(jsonStr, result);
  }
  return result;
}