Synthetic Data Generation with DSPy
The scarcest thing in AI testing is a good test set. You can generate one from the documents you already have.
8 min read8 মিনিটে পড়া যায় · 6 sections6টি সেকশন · 4 sources4টি সোর্স · updated Nov 2026আপডেট: Nov 2026
Verify synthetic tests before you trust themবিশ্বাস করার আগে synthetic টেস্ট যাচাই করুন
Watch generated questions shrink to a golden set at each check, then switch verification off.প্রতিটা যাচাইয়ে তৈরি প্রশ্ন কমে golden set-এ নামতে দেখুন, তারপর যাচাই বন্ধ করে দেখুন।
You’ll learnআপনি শিখবেন
- The four stages of the pipelineপাইপলাইনের চারটা ধাপ
- The three checks: supported, answerable, distinctতিনটা যাচাই: supported, answerable, distinct
- Why a human reads a sampleকেন মানুষ নমুনা পড়ে
- Why an unverified set is a liabilityকেন যাচাই না করা সেট একটা দায়
In plain words
Synthetic data is test data you generate rather than collect. For AI testing it solves the real problem: a hand-written eval set is small, biased towards what you already thought of, and expensive to grow.
Two tools do this, from opposite ends:
- Giskard's quality scan generates test scenarios from a knowledge base. Each generator probes one way an assistant goes wrong (hallucination, sycophancy, split or multi-topic questions, out-of-scope questions), and every scenario is tagged with the quality it tests and the component it stresses. It replaced the v2 RAGET tool Source 3সোর্স 3 Source 4সোর্স 4.
- DSPy optimizes a prompt program against a metric. You describe the task "write a question that this document answers", and it compiles a pipeline that does it well, so it can be used to build the question generator Source 1সোর্স 1 Source 2সোর্স 2.
The pipeline the two are usually combined into has four stages, and the third is the one that matters:
source documents
-> generate questions the model writes them from each document
-> generate answers a second model answers them, using the document
-> VERIFY the answers is the answer supported by the document?
-> golden dataset only the questions that survivedThe verification stage is what separates a useful synthetic dataset from a large pile of wrong test cases. An unverified synthetic dataset is a liability: it teaches the model to be confidently wrong and then tests you on it.
সহজ English
Synthetic data is test data you generate instead of collecting it. For AI testing, it fixes a real problem. A hand-written test set is small. It leans toward the ideas you already had. It is costly to grow.
Two tools help, from opposite ends:
- Giskard's quality scan makes test scenarios from a knowledge base. Each generator probes one way an assistant goes wrong: hallucination, sycophancy, split or multi-topic questions, and out-of-scope questions. Every scenario is tagged with the quality it tests and the component it stresses. It replaced the v2 RAGET tool Source 3সোর্স 3 Source 4সোর্স 4.
- DSPy improves a prompt program against a metric. You describe the task: "write a question that this document answers". It compiles a pipeline that does the task well. So you can use it to build the question generator Source 1সোর্স 1 Source 2সোর্স 2.
People usually combine the two into a pipeline with four stages. The third stage matters most:
source documents
-> generate questions the model writes them from each document
-> generate answers a second model answers them, using the document
-> VERIFY the answers is the answer supported by the document?
-> golden dataset only the questions that survivedThe verification stage is what makes a dataset useful. Without it, you get a large pile of wrong test cases. An unverified synthetic dataset is a liability. It teaches the model to be confidently wrong. Then it tests you on that.
বাংলা ব্যাখ্যা
Synthetic data হলো সংগ্রহ না করে তৈরি করা test data। AI টেস্টিং-এ এটা আসল সমস্যাটা সমাধান করে: হাতে লেখা eval set ছোট, আপনি যা ভেবেছেন তার দিকে বাঁকানো, আর বড় করা দামি।
দুটো টুল এটা করে, দুই প্রান্ত থেকে:
- Giskard-এর quality scan knowledge base থেকে test scenario তৈরি করে। প্রতিটা generator assistant-এর ভুল করার একটা উপায় probe করে (hallucination, sycophancy, split বা multi-topic প্রশ্ন, out-of-scope প্রশ্ন), আর প্রতিটা scenario ট্যাগ করা থাকে কোন গুণ আর কোন component যাচাই করছে। এটা v2-এর RAGET tool-এর জায়গা নিয়েছে Source 3সোর্স 3 Source 4সোর্স 4।
- DSPy একটা metric-এর বিপরীতে একটা prompt program optimize করে। আপনি কাজটা বর্ণনা করেন "এই document-এর উত্তর দেয় এমন একটা প্রশ্ন লেখো", আর সেটা একটা pipeline compile করে যেটা ভালো করে, তাই প্রশ্ন generator বানাতে ব্যবহার করা যায় Source 1সোর্স 1 Source 2সোর্স 2।
দুটোকে যোগ করে যে pipeline বানানো হয় তার চারটা ধাপ, আর তৃতীয়টাই গুরুত্বপূর্ণ:
source documents
-> generate questions the model writes them from each document
-> generate answers a second model answers them, using the document
-> VERIFY the answers is the answer supported by the document?
-> golden dataset only the questions that survivedverification ধাপটাই কাজের synthetic dataset আর ভুল টেস্ট কেসের বড় গাদার পার্থক্য। যাচাই না করা synthetic dataset একটা দায়: এটা model-কে আত্মবিশ্বাসী ভুল হতে শেখায়, আর তারপর সেটা দিয়ে আপনাকেই টেস্ট করে।
How it works
The generation step is a prompt that asks a model to write questions a document answers, with a difficulty and a type. DSPy makes this reliable by optimising the prompt program against a metric, so the questions get better without you editing the prompt by hand each time Source 2সোর্স 2.
The answer step uses a second model that sees the document and answers the question. Two models is deliberate: the question writer and the answerer have different failure modes, and a single model answering its own question is much more likely to produce a confident, self-consistent, wrong pair.
The verification step is where the dataset is made trustworthy, and it has three checks:
supported is every claim in the answer in the document?
answerable does the document actually contain the answer?
distinct is this question different from the ones already in the set?The third check is the one people forget, and without it a big generated dataset is mostly the same question forty times. Giskard's quality scan tags each scenario by kind, which gives you a natural way to see which kinds of failure your set already covers Source 3সোর্স 3.
The last step is a human sample. Not all of them: a few dozen, read by a person, to estimate the false rate of the verification. The verification is itself a model call, and a model that verifies answers is also fallible.
The DSPy side has one more thing worth using: the optimizer. It compiles a prompt program against your metric, so the question generator gets better at producing questions your verifier accepts, and the metric becomes part of the system rather than a separate script Source 1সোর্স 1.
সহজ English
The generation step is a prompt. It asks a model to write questions that a document answers, with a difficulty and a type. DSPy makes this dependable. It tunes the prompt program against a metric. The questions get better, and you do not edit the prompt by hand each time Source 2সোর্স 2.
The answer step uses a second model. It sees the document and answers the question. Using two models is on purpose. The question writer and the answerer fail in different ways. One model answering its own question is much more likely to give a confident, self-consistent, wrong pair.
The verification step is where the dataset becomes trustworthy. It has three checks:
supported is every claim in the answer in the document?
answerable does the document actually contain the answer?
distinct is this question different from the ones already in the set?People forget the third check. Without it, a big generated dataset is mostly the same question forty times. Giskard's quality scan tags each scenario by kind. That shows you which kinds of failure your set already covers Source 3সোর্স 3.
The last step is a human sample. Not every item, just a few dozen. A person reads them to estimate how often the verification is wrong. The verification is itself a model call. A model that verifies answers can be wrong too.
The DSPy side has one more useful part: the optimizer. It compiles a prompt program against your metric. The question generator gets better at making questions your verifier accepts. The metric becomes part of the system, not a separate script Source 1সোর্স 1.
বাংলা ব্যাখ্যা
তৈরির ধাপ হলো এমন একটা prompt যা model-কে বলে একটা document-এর উত্তর দেয় এমন প্রশ্ন লিখতে, একটা কঠিনতা আর একটা ধরনসহ। DSPy এটাকে নির্ভরযোগ্য করে metric-এর বিপরীতে prompt program optimize করে, তাই প্রশ্ন উন্নত হয় আপনাকে প্রতিবার prompt হাতে সম্পাদনা না করেই Source 2সোর্স 2।
উত্তরের ধাপ দ্বিতীয় একটা model ব্যবহার করে যে document দেখে প্রশ্নের উত্তর দেয়। দুটো model জোড়া দেওয়া ইচ্ছাকৃত: প্রশ্ন লেখা আর উত্তর দেওয়ার failure mode আলাদা, আর একই model নিজের প্রশ্নের উত্তর দিলে confident, self-consistent কিন্তু ভুল জোড়া বানানোর সম্ভাবনা অনেক বেশি।
verification ধাপ যেখানে dataset ভরসারযোগ্য হয়, আর এতে তিনটা যাচাই:
supported is every claim in the answer in the document?
answerable does the document actually contain the answer?
distinct is this question different from the ones already in the set?তৃতীয় যাচাইটাই মানুষ ভুলে যায়, আর ছাড়া বড় তৈরি dataset বেশিরভাগ একই প্রশ্ন চল্লিশবার। Giskard-এর quality scan প্রতিটা scenario ধরন অনুযায়ী ট্যাগ করে, যেটা দিয়ে দেখা যায় আপনার set কোন ধরনের ব্যর্থতা ইতিমধ্যে কভার করছে Source 3সোর্স 3।
শেষ ধাপ হলো মানুষের নমুনা। সব নয়: কয়েক ডজন, একজন মানুষ পড়বে, যাতে verification-এর false rate-টা আন্দাজ করা যায়। verification-ও একটা model call, আর উত্তর যাচাই করা model-ও ভুল হতে পারে।
DSPy-এর দিকে আরেকটা ব্যবহারযোগ্য জিনিস আছে: optimizer। এটা আপনার metric-এর বিপরীতে একটা prompt program compile করে, তাই প্রশ্ন generator আপনার verifier যা accept করে সেটা বানাতে ভালো হয়, আর metric আলাদা script না থেকে সিস্টেমের অংশ হয় Source 1সোর্স 1।
In practice
The generation prompt is the design, and it asks for the type and the difficulty:
QUESTION_PROGRAM = """Write one question that the document below answers.
The question must:
- be answerable from the document alone
- name no fact that the document does not state
- differ from every other question in the set
Difficulty: {difficulty}
Type: {question_type}
Document:
{document}
"""The verification is where the dataset is made safe, and the three checks are separate assertions:
def test_the_generated_answer_is_supported_by_the_document(question, answer, document):
claims = split_into_claims(answer)
for claim in claims:
assert claim in document, f"unsupported claim: {claim}"def test_the_generated_question_is_not_a_duplicate(question, existing_questions):
embedding = embed(question)
for other in existing_questions:
assert cosine(embedding, embed(other)) < 0.95, (
f"near-duplicate of: {other}"
)The duplicate check is the one that changes the dataset the most, and it needs an embedding rather than a string compare, because "How long is the return window?" and "What is the return period?" are the same question.
And the human sample, which measures the verifier itself:
def test_the_verifier_agrees_with_a_human_on_a_sample(verified, human_reviewed):
disagreements = [c for c in verified if c.id in human_reviewed
and c.verified != human_reviewed[c.id]]
rate = len(disagreements) / len(human_reviewed)
report.add("verifier_false_rate", rate)
assert rate < 0.05, f"the verifier disagrees with humans {rate:.1%} of the time"That last test is the one that makes the whole approach honest. The verifier is a model, and a model that checks answers is also fallible, so its error rate has to be measured rather than assumed.
The DSPy optimizer then improves the generator against that same metric:
import dspy
class WriteQuestion(dspy.Signature):
document: str = dspy.InputField()
question: str = dspy.OutputField()
def verifier_metric(example, pred, trace=None):
return verify(pred.question, example.document) # True or False
program = dspy.ChainOfThought(WriteQuestion)
trainset = [dspy.Example(document=d).with_inputs("document") for d in verified_documents]
optimizer = dspy.BootstrapFewShot(metric=verifier_metric, max_bootstrapped_demos=8)
compiled = optimizer.compile(program, trainset=trainset)Training the optimizer on the verified examples only is the point: the optimizer learns what a good question looks like from the questions that survived, not from the ones that were thrown away.
সহজ English
The generation prompt is the design. It asks for the type and the difficulty:
QUESTION_PROGRAM = """Write one question that the document below answers.
The question must:
- be answerable from the document alone
- name no fact that the document does not state
- differ from every other question in the set
Difficulty: {difficulty}
Type: {question_type}
Document:
{document}
"""Verification is what makes the dataset safe. The checks are separate assertions. First, is the answer supported by the document?
def test_the_generated_answer_is_supported_by_the_document(question, answer, document):
claims = split_into_claims(answer)
for claim in claims:
assert claim in document, f"unsupported claim: {claim}"Next, is the question a duplicate?
def test_the_generated_question_is_not_a_duplicate(question, existing_questions):
embedding = embed(question)
for other in existing_questions:
assert cosine(embedding, embed(other)) < 0.95, (
f"near-duplicate of: {other}"
)The duplicate check changes the dataset the most. It needs an embedding, not a plain string compare. "How long is the return window?" and "What is the return period?" are the same question.
Then comes the human sample. It measures the verifier itself:
def test_the_verifier_agrees_with_a_human_on_a_sample(verified, human_reviewed):
disagreements = [c for c in verified if c.id in human_reviewed
and c.verified != human_reviewed[c.id]]
rate = len(disagreements) / len(human_reviewed)
report.add("verifier_false_rate", rate)
assert rate < 0.05, f"the verifier disagrees with humans {rate:.1%} of the time"This last test keeps the whole approach honest. The verifier is a model, and a model that checks answers can be wrong too. So you measure its error rate. You do not assume it.
The DSPy optimizer then improves the generator against that same metric:
import dspy
class WriteQuestion(dspy.Signature):
document: str = dspy.InputField()
question: str = dspy.OutputField()
def verifier_metric(example, pred, trace=None):
return verify(pred.question, example.document) # True or False
program = dspy.ChainOfThought(WriteQuestion)
trainset = [dspy.Example(document=d).with_inputs("document") for d in verified_documents]
optimizer = dspy.BootstrapFewShot(metric=verifier_metric, max_bootstrapped_demos=8)
compiled = optimizer.compile(program, trainset=trainset)Train the optimizer on the verified examples only. That is the point. It learns what a good question looks like from the questions that survived, not from the ones thrown away.
বাংলা ব্যাখ্যা
তৈরির prompt-টাই ডিজাইন, আর সেটা ধরন আর কঠিনতা চাইতে বলে:
QUESTION_PROGRAM = """Write one question that the document below answers.
The question must:
- be answerable from the document alone
- name no fact that the document does not state
- differ from every other question in the set
Difficulty: {difficulty}
Type: {question_type}
Document:
{document}
"""verification যেখানে dataset নিরাপদ হয়, আর তিনটা যাচাই আলাদা assertion:
def test_the_generated_answer_is_supported_by_the_document(question, answer, document):
claims = split_into_claims(answer)
for claim in claims:
assert claim in document, f"unsupported claim: {claim}"def test_the_generated_question_is_not_a_duplicate(question, existing_questions):
embedding = embed(question)
for other in existing_questions:
assert cosine(embedding, embed(other)) < 0.95, (
f"near-duplicate of: {other}"
)duplicate চেকটাই dataset-কে সবচেয়ে বেশি বদলায়, আর এতে string তুলনা নয় embedding লাগে, কারণ "How long is the return window?" আর "What is the return period?" একই প্রশ্ন।
আর মানুষের নমুনা, যেটা verifier-কেই মাপে:
def test_the_verifier_agrees_with_a_human_on_a_sample(verified, human_reviewed):
disagreements = [c for c in verified if c.id in human_reviewed
and c.verified != human_reviewed[c.id]]
rate = len(disagreements) / len(human_reviewed)
report.add("verifier_false_rate", rate)
assert rate < 0.05, f"the verifier disagrees with humans {rate:.1%} of the time"শেষ টেস্টটাই পুরো পদ্ধতিটাকে সৎ করে। verifier একটা model, আর উত্তর যাচাই করা model-ও ভুল হতে পারে, তাই তার error rate অনুমান না করে মাপতে হবে।
তারপর DSPy optimizer সেই একই metric-এর বিপরীতে generator-কে উন্নত করে:
import dspy
class WriteQuestion(dspy.Signature):
document: str = dspy.InputField()
question: str = dspy.OutputField()
def verifier_metric(example, pred, trace=None):
return verify(pred.question, example.document) # True or False
program = dspy.ChainOfThought(WriteQuestion)
trainset = [dspy.Example(document=d).with_inputs("document") for d in verified_documents]
optimizer = dspy.BootstrapFewShot(metric=verifier_metric, max_bootstrapped_demos=8)
compiled = optimizer.compile(program, trainset=trainset)শুধু যাচাই পাস করা উদাহরণে optimizer train করাটাই পয়েন্ট: optimizer শেখে ভালো প্রশ্ন কেমন হয়, আর যেগুলো ছুঁড়ে ফেলা হয়েছে সেগুলো থেকে নয়।
Common mistakes
Mistake 1: no verification step. An unverified synthetic dataset trains the model to be confidently wrong and then tests you on it.
Mistake 2: one model writes the question and answers it. Self-consistent and wrong at the same time is the worst combination.
Mistake 3: no duplicate check. A large generated set is often a few dozen questions repeated.
Mistake 4: no human sample. The verifier is a model too, and its error rate has to be measured.
Mistake 5: using the generated set as the only eval. A dataset written by a model tests for the failures that model has.
Mistake 6: training the optimizer on unverified examples. It learns from the questions you were going to throw away.
Mistake 7: regenerating the set on every run. The eval must be fixed, or two runs cannot be compared.
সহজ English
Mistake 1: no verification step. An unverified synthetic dataset teaches the model to be confidently wrong. Then it tests you on that.
Mistake 2: one model writes the question and answers it. A pair that is self-consistent and wrong at once is the worst mix.
Mistake 3: no duplicate check. A large generated set is often a few dozen questions repeated.
Mistake 4: no human sample. The verifier is a model too. You have to measure its error rate.
Mistake 5: using the generated set as the only eval. A dataset written by a model tests for the failures that model has.
Mistake 6: training the optimizer on unverified examples. It learns from the questions you were going to throw away.
Mistake 7: regenerating the set on every run. The eval must stay fixed. Otherwise you cannot compare two runs.
বাংলা ব্যাখ্যা
Mistake 1: verification ধাপ নেই। যাচাই না করা synthetic dataset model-কে আত্মবিশ্বাসী ভুল হতে শেখায়, আর তারপর সেটা দিয়ে আপনাকেই টেস্ট করে।
Mistake 2: একই model প্রশ্ন লেখে আর উত্তরও দেয়। self-consistent আর ভুল একসাথে হওয়াটাই সবচেয়ে খারাপ সমন্বয়।
Mistake 3: duplicate চেক নেই। বড় তৈরি set প্রায়ই কয়েক ডজন প্রশ্নের পুনরাবৃত্তি।
Mistake 4: মানুষের নমুনা নেই। verifier-ও একটা model, আর তার error rate মাপতে হবে।
Mistake 5: তৈরি set-কেই একমাত্র eval হিসেবে ব্যবহার করা। model-এর লেখা dataset ওই model-এর ব্যর্থতাগুলোর জন্যই টেস্ট করে।
Mistake 6: যাচাই না করা উদাহরণে optimizer train করা। যে প্রশ্নগুলো ফেলে দেওয়ার কথা ছিল, optimizer সেগুলো থেকেই শেখে।
Mistake 7: প্রতিটা run-এ set আবার তৈরি করা। eval set নির্দিষ্ট থাকতে হবে, নইলে দুটো run তুলনা করা যায় না।
What a tester can check
Tutorial advice
This advice is the tutorial's own. It does not come from the sources.
- Commit the generated dataset. It is an asset, and regenerating it every time means every eval measures a different thing.
- Keep the generation prompt, the verifier prompt, and the models in version control next to the dataset. The dataset is meaningless without them.
- Log the verifier's decision for every case, not just the verdict. When a case is wrong later you need to know why it was accepted.
- Keep the difficulty label. The hard cases are the ones worth generating more of, and they are the ones you keep when you prune.
- Sample the generated set by hand every few months. The knowledge base changes and the questions drift out of date.
- Never let the synthetic set replace your hand-written one. Keep both, and compare the scores: a gap between them is a finding about the coverage.
সহজ English
Tutorial advice
This advice is the tutorial's own. It does not come from the sources.
- Commit the generated dataset. It is an asset, and regenerating it every time means every eval measures a different thing.
- Keep the generation prompt, the verifier prompt, and the models in version control next to the dataset. The dataset is meaningless without them.
- Log the verifier's decision for every case, not just the verdict. When a case is wrong later you need to know why it was accepted.
- Keep the difficulty label. The hard cases are the ones worth generating more of, and they are the ones you keep when you prune.
- Sample the generated set by hand every few months. The knowledge base changes and the questions drift out of date.
- Never let the synthetic set replace your hand-written one. Keep both, and compare the scores: a gap between them is a finding about the coverage.
বাংলা ব্যাখ্যা
Tutorial advice
এই পরামর্শ টিউটোরিয়ালের নিজস্ব। এটি সোর্স থেকে আসেনি।
- তৈরি dataset commit করুন। এটা একটা সম্পদ, আর প্রতিবার আবার তৈরি করলে প্রতিটা eval ভিন্ন কিছু মাপে।
- generation prompt, verifier prompt আর model dataset-এর পাশে version control-এ রাখুন। এগুলো ছাড়া dataset অর্থহীন।
- প্রতিটা কেসের verifier-এর সিদ্ধান্ত log করুন, শুধু verdict নয়। পরে কোনো কেস ভুল প্রমাণিত হলে কেন accept হয়েছিল তা জানা দরকার।
- কঠিনতার label রাখুন। কঠিন কেসগুলোই বেশি তৈরি করার যোগ্য, আর prune করার সময় ওগুলোই রাখা হয়।
- কয়েক মাস পরপর তৈরি set থেকে নমুনা হাতে পড়ুন। knowledge base বদলায় আর প্রশ্নগুলো পুরোনো হয়ে যায়।
- তৈরি set কখনো আপনার হাতে লেখা set-এর বদলে দেবেন না। দুটোই রাখুন, আর স্কোর তুলনা করুন: দুটোর মধ্যে ফারাক মানে coverage-র একটা finding।
Key words
| Word | What it means |
|---|---|
| Synthetic data | Test data you generate rather than collect |
| Golden dataset | The fixed, versioned set of cases the eval runs against |
| Verification | Checking that a generated answer is supported by its source document |
| Answerable | Whether the document actually contains the answer to the question |
| Duplicate check | Rejecting a generated question that is too close to an existing one |
| DSPy | A framework that compiles and optimises prompt programs against a metric |
| Optimizer | The component that improves a prompt program using examples and a metric |
| Scenario tag | A label on a generated test scenario naming the quality it tests and the component it stresses |