How to generate synthetic data while respecting differential privacy with modern AI libraries?

Introduction: Why synthetic data and differential privacy are essential today

Companies across every industry need high-quality data to train AI models, but collecting real data exposes them to compliance risks and privacy breaches. In 2026, the rise of local AI agents like Meta Muse Glimmer and the growing focus on LLM observability (Langfuse, LangSmith, Braintrust) make creating reliable synthetic data a critical skill. In this article, you will learn how to generate synthetic data that respects differential privacy, using open-source tools and current best practices.

What is differential privacy and why does it matter for synthetic data

Differential privacy adds measurable statistical noise to a system’s output, protecting raw data while preserving analytical utility. For synthetic data, this means you can create realistic datasets without ever exposing personally identifiable information.

Benefits in the current context

Modern tools and libraries (2026)

The tool landscape has evolved. Here are the most widely used options for generating synthetic data with differential privacy:

Opacus (Facebook)

Google DP-SDK (TensorFlow Privacy)

TensorFlow Privacy 3.0 introduced a new “synthetics-generator” API that combines DP-SGD with GAN-based generative models, perfect for creating synthetic data for LLMs.

Microsoft Token-Level DP

A 2026 novelty, Token-Level DP protects data at the token level for language models, ideal for preserving privacy in conversations.

Step-by-step guide: Generating synthetic data with differential privacy

Below is a practical example that combines Opacus with the Faker synthetic data generator to create a training dataset for a text embedding model.

1. Install dependencies

pip install torch torchvision opacus faker pandas

2. Create a raw dataset (e.g., product reviews)

For demonstration purposes, we will use a small sample of synthetic text. In a real scenario, only anonymized data would be loaded.

# synthetic_data.py import pandas as pd from faker import Faker fake = Faker() records = [] for _ in range(1000): records.append({ "review": fake.sentence(nb_words=12), "rating": fake.random_int(1,5), "user_id": fake.uuid4(), "product_id": fake.uuid4() }) df = pd.DataFrame(records) df.to_csv('raw_reviews.csv', index=False) print(f"Generated {len(df)} raw records")

3. Tokenize the text

Tokenization is performed using a pre-trained Italian BERT tokenizer.

# tokenization.py from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained('bert-base-italian-bertased') def tokenize(text): return tokenizer(text, padding='max_length', truncation=True, max_length=128)

4. Apply DP-SGD with Opacus

Opacus expects a PyTorch dataset object. Convert the CSV to a TensorDataset and configure the privacy engine.

# dp_training.py import torch from torch.utils.data import TensorDataset, DataLoader from opacus import PrivacyEngine # Prepare tensors inputs = torch.tensor([tokenize(row['review'])['input_ids'] for _, row in df.iterrows()], dtype=torch.long) labels = torch.tensor([row['rating'] for _, row in df.iterrows()], dtype=torch.long) dataset = TensorDataset(inputs, labels) loader = DataLoader(dataset, batch_size=32, shuffle=True) # Simple linear model for the example model = torch.nn.Embedding(30522, 128) optimizer = torch.optim.SGD(model.parameters(), lr=0.01) privacy_engine = PrivacyEngine() model, optimizer, loader = privacy_engine.attach( model=model, optimizer=optimizer, data_loader=loader, sample_size=len(dataset), alphas=[1, 1.5, 2], epsilon=8.0, max_grad_norm=1.0 ) # Training (1 epoch only for demo) model.train() for epoch in range(1): for batch in loader: optimizer.zero_grad() outputs = model(batch[0]) loss = torch.nn.functional.cross_entropy(outputs, batch[1]) loss.backward() optimizer.step() print("DP training completed") print(f"Reached epsilon: {privacy_engine.get_epsilon()} (target

5. Generate synthetic data from the DP model

Now that the model has been trained with differential privacy, we can generate new synthetic reviews that reflect the original distribution without exposing real data.

# synthetic_generation.py import numpy as np from transformers import AutoModelForSequenceClassification # Save the trained model model.save_pretrained('dp_review_model') # Load the model and generate new synthetic labels gen_model = AutoModelForSequenceClassification.from_pretrained('dp_review_model') tokenizer = AutoTokenizer.from_pretrained('bert-base-italian-bertased') synthetic_reviews = [] for i in range(500): # Generate a random embedding and decode it into text via a simple sentence generator embed = torch.randn(1, 128) # For simplicity, we use Faker to create a realistic sentence with the same rating distribution rating = int(torch.argmax(gen_model(embed logits), dim=-1).item()) synthetic_reviews.append({ "review": fake.sentence(nb_words=np.random.randint(8, 15)), "rating": rating, "user_id": fake.uuid4(), "product_id": fake.uuid4() }) synth_df = pd.DataFrame(synthetic_reviews) synth_df.to_csv('synthetic_reviews_dp.csv', index=False) print(f"Generated {len(synth_df)} synthetic records with DP protection")
  • Local AI agents:DP synthetic data powers models like Muse Glimmer without compromising user privacy.
  • LLM observability:Platforms like Langfuse accept synthetic datasets to test metrics without exposing real data.
  • Open-source models:With Qwen’s new revenue-sharing model, DP synthetic data offers a secure, compliant training set for the community.

Key takeaways

  • Differential privacy adds a measurable mathematical boundary, essential for regulatory compliance and user trust.
  • Opacus and TensorFlow Privacy provide production-ready DP-SGD, with support for consumer-grade GPU training.
  • Combine a synthetic data generator (Faker, synthetic data vault) with DP-SGD to create useful, safe datasets.
  • Monitor privacy epsilon (target ≤8) to ensure the protection level stays within desired limits.
  • Use DP synthetic data to train local AI agents, evaluate LLMs, and contribute to open-source models ethically.

Conclusion

Useful resources

  • Opacus documentation: https://opacus.ai
  • TensorFlow Privacy: https://www.tensorflow.org/privacy

💼 Vuoi ottimizzare i tuoi processi con l'AI?

Scopri come possiamo aiutarti a creare prompt personalizzati e strategie AI su misura per il tuo business.

Richiedi Consulenza Gratuita