How to Beginner · 3 min read

How to generate quiz questions with AI

Quick answer
Use a large language model like gpt-4o via the OpenAI API to generate quiz questions by prompting it with a topic and desired question format. Send a prompt such as "Generate 5 multiple-choice questions about photosynthesis" and parse the AI's response for quiz content.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your OpenAI API key as an environment variable for secure access.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example shows how to generate 5 multiple-choice quiz questions about photosynthesis using gpt-4o. The prompt instructs the model to create questions with 4 options and the correct answer indicated.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

prompt = (
    "Generate 5 multiple-choice quiz questions about photosynthesis. "
    "Each question should have 4 options labeled A-D, and indicate the correct answer."
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

quiz_text = response.choices[0].message.content
print(quiz_text)
output
1. What is the primary pigment involved in photosynthesis?
A) Chlorophyll
B) Hemoglobin
C) Melanin
D) Carotene
Answer: A

2. Which organelle is the site of photosynthesis in plant cells?
A) Mitochondria
B) Chloroplast
C) Nucleus
D) Ribosome
Answer: B

3. What gas is taken in by plants during photosynthesis?
A) Oxygen
B) Carbon dioxide
C) Nitrogen
D) Hydrogen
Answer: B

4. What is the main product of photosynthesis?
A) Glucose
B) Oxygen
C) Water
D) ATP
Answer: A

5. Photosynthesis primarily occurs in which part of the plant?
A) Roots
B) Stem
C) Leaves
D) Flowers
Answer: C

Common variations

You can generate quiz questions asynchronously, stream responses for real-time display, or use different models like gpt-4o-mini for faster, cheaper results. Adjust the prompt to create true/false, fill-in-the-blank, or short answer questions.

python
import asyncio
from openai import OpenAI

async def generate_quiz_async():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    prompt = "Generate 3 true/false quiz questions about the solar system."
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    print(response.choices[0].message.content)

asyncio.run(generate_quiz_async())
output
1. The Sun is a star. True
2. Jupiter is the smallest planet in the solar system. False
3. Venus is closer to the Sun than Earth. True

Troubleshooting

  • If you get an authentication error, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the output is incomplete, increase max_tokens in the API call.
  • For unexpected formats, refine your prompt to be more explicit about the question style and answer format.

Key Takeaways

  • Use the OpenAI Python SDK with gpt-4o to generate quiz questions by crafting clear prompts.
  • Adjust question types and formats by modifying the prompt instructions to suit your educational needs.
  • Leverage async or streaming calls for interactive quiz generation experiences.
  • Always secure your API key via environment variables and handle errors by checking authentication and token limits.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗