extractToolCalls static method
Extract all function calls from an SDK response JSON string.
Accepts the structural variants observed in the wild:
- Top-level
tool_callslist (Gemma 4 standard path). contentarray entries withtype: "tool_call"(multimodal).- 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;
}