How to beginner · 3 min read

AI flashcard generation

Quick answer
Use a large language model like gpt-4o via the OpenAI SDK to generate flashcards by prompting it to create question-answer pairs from educational text. This involves sending the source content as input and parsing the model's structured output into flashcards.

PREREQUISITES

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

Setup

Install the openai Python package and set your 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 flashcards from a given educational text by prompting gpt-4o to output question-answer pairs in JSON format.

python
import os
from openai import OpenAI

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

source_text = "Photosynthesis is the process by which green plants convert sunlight into chemical energy. It occurs mainly in the leaves and involves chlorophyll."

prompt = f"Generate 3 flashcards as JSON with 'question' and 'answer' fields from this text:\n{source_text}\nOutput format: [{'{'}\"question\": string, \"answer\": string{'}'}]"

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

flashcards_json = response.choices[0].message.content
print(flashcards_json)
output
[{"question": "What is photosynthesis?", "answer": "Photosynthesis is the process by which green plants convert sunlight into chemical energy."}, {"question": "Where does photosynthesis mainly occur?", "answer": "It occurs mainly in the leaves."}, {"question": "What pigment is involved in photosynthesis?", "answer": "Chlorophyll is involved in photosynthesis."}]

Common variations

  • Use gpt-4o-mini for faster, cheaper flashcard generation with slightly less detail.
  • Implement asynchronous calls with asyncio and await for scalable batch processing.
  • Stream output tokens for real-time flashcard generation display.
  • Customize prompt templates to generate flashcards for different subjects or formats.
python
import asyncio
import os
from openai import OpenAI

async def generate_flashcards_async(text: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    prompt = f"Generate 3 flashcards as JSON with 'question' and 'answer' fields from this text:\n{text}\nOutput format: [{'{'}\"question\": string, \"answer\": string{'}'}]"
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

async def main():
    source_text = "Water boils at 100 degrees Celsius at sea level."
    flashcards = await generate_flashcards_async(source_text)
    print(flashcards)

asyncio.run(main())
output
[{"question": "At what temperature does water boil at sea level?", "answer": "Water boils at 100 degrees Celsius at sea level."}, {"question": "What is the boiling point of water?", "answer": "100 degrees Celsius at sea level."}, {"question": "Does water boil at different temperatures at different altitudes?", "answer": "Yes, boiling point changes with altitude."}]

Troubleshooting

  • If you get an authentication error, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the output is not valid JSON, refine your prompt to explicitly request JSON format and validate the model's response.
  • For rate limit errors, implement exponential backoff retries or reduce request frequency.
  • Use max_tokens parameter to control response length if output is truncated.

Key Takeaways

  • Use gpt-4o with the OpenAI SDK to generate structured flashcards from text.
  • Explicitly prompt for JSON output to simplify parsing flashcards programmatically.
  • Async and streaming calls enable scalable and interactive flashcard generation.
  • Always validate API keys and handle common errors like rate limits and malformed output.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗