validateToolArguments function
Validates arguments against the tool's JSON-schema schema and returns
a new map with coerced values and injected defaults. Undeclared keys pass
through unchanged. The input map is never mutated.
Throws ToolValidationException listing every violation when validation fails. Streamed partial JSON never reaches this function: providers accumulate ToolCall.partialArguments and only parse the complete JSON into ToolCall.arguments, so validation always runs on the final arguments of a finished tool call.
Implementation
Map<String, dynamic> validateToolArguments({
required Map<String, dynamic> arguments,
required Map<String, dynamic> schema,
required String toolName,
}) {
final errors = <String>[];
final validated = _validateObject(arguments, schema, '', errors);
if (errors.isNotEmpty) {
throw ToolValidationException(
toolName,
'Validation failed for tool "$toolName":\n'
'${errors.map((e) => ' - $e').join('\n')}\n\n'
'Received arguments:\n${jsonEncode(arguments)}',
);
}
return validated;
}