KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Streamline Test Data Creation with 4D AIKit
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: October 20, 2025
Creating realistic test data manually is time-consuming and often results in limited variety. Before 4D AIKit, generating test data required integrating with external AI services or third-party APIs, which meant managing additional dependencies and switching between different tools.

4D AIKit brings AI-powered data generation directly into 4D. While it requires an OpenAI API key, the integration and data handling are fully managed within the 4D environment. This eliminates the need for custom API calls, external setup, or leaving the development workflow.

Example :

// Initialize AIKit client with OpenAI API key
var $client := cs.AIKit.OpenAI.new($apikey)

// Create a generator with clear instructions
var $generator : Object
$generator := $client.chat.create(\
  "You are a JSON data generator. \r" +\
  "Always return valid JSON arrays only. \r" +\
  "Do NOT include markdown, code fences, text, or explanations. \r" +\
  "Your response must start with '[' and end with ']'. \r" +\
  "Never escape quotes or wrap the JSON in a string.")

// Define prompt to request structured customer data
var $prompt : Text
$prompt := "Generate 15 customers with: name, email, phone, city, country."

// Generate JSON data from AIKit
var $jsonData : Object
$jsonData := $generator.prompt($prompt)
var $response : Text
$response := $jsonData.choice.message.content

// Parse JSON into a collection
var $customers : Collection
$customers := JSON Parse($response)

// Insert each customer into the database
For each ($customer; $customers)
  var $entity : Object
  $entity := ds.Customer.new()
  $entity.name := $customer.name
  $entity.email := $customer.email
  $entity.phone := $customer.phone
  $entity.city := $customer.city
  $entity.country := $customer.country
  $entity.is_test_data := True // Flag for cleanup
  $entity.save()
End for each


// Tip : Cleanup test data after use, in order to add real data
var $testRecords : cs.CustomerSelection
$testRecords := ds.Customer.query("is_test_data = :1"; True)
$testRecords.drop()


Here are the 15 generated customers with diverse names, locations, and contact information :