Looking to automate your course enrollment process with AI? In this deep, hands-on guide, you’ll build an AI-powered course enrollment workflow using Python, FastAPI, and OpenAI’s GPT-4 API. This tutorial is designed for developers and technical teams at schools, colleges, and EdTech startups who want to streamline student onboarding, eligibility checks, and personalized recommendations.
For a broader look at the future of AI workflow automation in education, see our AI Workflow Automation for Education: Top Platforms and Case Studies for 2026 guide.
Prerequisites
- Tools & Libraries:
- Python 3.11+
- FastAPI 0.110+
- Uvicorn 0.29+
- OpenAI Python SDK 1.22+
- SQLite (or PostgreSQL, optional for production)
- HTTP client (e.g., curl, Postman)
- Accounts & API Keys:
- OpenAI API key (GPT-4 model access)
- Knowledge:
- Basic Python and REST API development
- Familiarity with JSON and HTTP requests
- Understanding of course enrollment processes
- System: Linux/macOS/Windows with Python installed
-
Set Up Your Project Environment
-
Create a new project directory:
mkdir ai-course-enrollment-workflow && cd ai-course-enrollment-workflow
-
Initialize a Python virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install required dependencies:
pip install fastapi uvicorn openai sqlite-utils
-
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="sk-..." # Replace with your actual key
Tip: For a comparison of AI workflow tools for education, see Best AI Workflow Tools for Education: 2026 Comparison for Schools, Colleges & EdTech Startups.
-
Create a new project directory:
-
Design the Course Enrollment Workflow
-
Define the workflow stages:
- Student submits enrollment request
- AI checks eligibility (prerequisites, schedule conflicts)
- AI recommends alternative courses if ineligible
- Enrollment is approved or declined
-
Create a simple database schema for students and courses:
sqlite-utils create-database enrollments.db sqlite-utils create-table enrollments.db students id integer primary key name text completed_courses text sqlite-utils create-table enrollments.db courses id integer primary key name text prerequisites text capacity integer enrolled integerThis creates two tables:
studentsandcourses. You can view them with:sqlite-utils tables enrollments.db
-
Insert sample data:
sqlite-utils insert enrollments.db students '[{"id":1,"name":"Alice","completed_courses":"Math 101,CS 101"}]' sqlite-utils insert enrollments.db courses '[{"id":1,"name":"AI Fundamentals","prerequisites":"Math 101","capacity":2,"enrolled":1}]'
Screenshot description: Terminal showing creation of
studentsandcoursestables, and sample data insertion. -
Define the workflow stages:
-
Build the FastAPI Backend
-
Create
main.pyand set up FastAPI:from fastapi import FastAPI, HTTPException from pydantic import BaseModel import sqlite3 import os app = FastAPI() DB_PATH = "enrollments.db" class EnrollmentRequest(BaseModel): student_id: int course_id: int -
Add a helper to fetch student and course info:
def get_student(student_id): with sqlite3.connect(DB_PATH) as conn: cur = conn.cursor() cur.execute("SELECT id, name, completed_courses FROM students WHERE id=?", (student_id,)) row = cur.fetchone() if row: return {"id": row[0], "name": row[1], "completed_courses": row[2].split(",")} return None def get_course(course_id): with sqlite3.connect(DB_PATH) as conn: cur = conn.cursor() cur.execute("SELECT id, name, prerequisites, capacity, enrolled FROM courses WHERE id=?", (course_id,)) row = cur.fetchone() if row: return { "id": row[0], "name": row[1], "prerequisites": row[2].split(",") if row[2] else [], "capacity": row[3], "enrolled": row[4] } return None -
Test your API locally:
uvicorn main:app --reload
Open
http://127.0.0.1:8000/docsin your browser to view the auto-generated Swagger UI.
-
Create
-
Integrate the OpenAI API for Smart Eligibility Checks
-
Install OpenAI SDK (if not already):
pip install openai
-
Add the AI eligibility logic to your API endpoint:
import openai @app.post("/enroll") async def enroll(request: EnrollmentRequest): student = get_student(request.student_id) course = get_course(request.course_id) if not student or not course: raise HTTPException(status_code=404, detail="Student or course not found") # Prepare prompt for GPT-4 prompt = f""" Student {student['name']} has completed: {', '.join(student['completed_courses'])}. They want to enroll in {course['name']}, which requires: {', '.join(course['prerequisites'])}. Course capacity: {course['capacity']}, currently enrolled: {course['enrolled']}. List any missing prerequisites, and say if the student can enroll. If not, recommend up to 2 alternative courses from this list: AI Fundamentals, Data Science 101, Advanced Python. """ response = openai.ChatCompletion.create( model="gpt-4", messages=[{"role": "system", "content": "You are an education assistant."}, {"role": "user", "content": prompt}], max_tokens=200 ) ai_reply = response['choices'][0]['message']['content'] # Simulate enrollment if eligible (simple check for demo) can_enroll = "can enroll" in ai_reply.lower() and "missing prerequisites" not in ai_reply.lower() if can_enroll and course['enrolled'] < course['capacity']: # Update database with sqlite3.connect(DB_PATH) as conn: cur = conn.cursor() cur.execute("UPDATE courses SET enrolled = enrolled + 1 WHERE id=?", (course['id'],)) conn.commit() return {"status": "enrolled", "ai_reply": ai_reply} else: return {"status": "not enrolled", "ai_reply": ai_reply} -
Test the AI-powered endpoint:
curl -X POST "http://127.0.0.1:8000/enroll" -H "Content-Type: application/json" -d '{"student_id":1,"course_id":1}'Expected output: JSON with
statusand a detailedai_replyfrom GPT-4.
Screenshot description: Swagger UI showing the
/enrollendpoint, and a sample JSON response with AI recommendations. -
Install OpenAI SDK (if not already):
-
Personalize Course Recommendations With AI
-
Enhance your prompt for more tailored suggestions:
prompt = f""" Student {student['name']} has completed: {', '.join(student['completed_courses'])}. They want to enroll in {course['name']}, which requires: {', '.join(course['prerequisites'])}. Course capacity: {course['capacity']}, currently enrolled: {course['enrolled']}. Based on their background, recommend up to 2 alternative courses that are available and relevant, if they cannot enroll. """ -
Optionally, store AI recommendations in the database for future analytics:
def log_ai_recommendation(student_id, course_id, ai_reply): with sqlite3.connect(DB_PATH) as conn: cur = conn.cursor() cur.execute(""" CREATE TABLE IF NOT EXISTS ai_logs ( id INTEGER PRIMARY KEY AUTOINCREMENT, student_id INTEGER, course_id INTEGER, ai_reply TEXT ) """) cur.execute("INSERT INTO ai_logs (student_id, course_id, ai_reply) VALUES (?, ?, ?)", (student_id, course_id, ai_reply)) conn.commit()Add
log_ai_recommendation(request.student_id, request.course_id, ai_reply)after the AI response in your endpoint. -
Test with a student missing prerequisites to see AI recommendations:
sqlite-utils insert enrollments.db students '[{"id":2,"name":"Bob","completed_courses":"English 101"}]' curl -X POST "http://127.0.0.1:8000/enroll" -H "Content-Type: application/json" -d '{"student_id":2,"course_id":1}'Expected:
status: not enrolledand AI suggests alternative courses.
Screenshot description: Terminal showing a failed enrollment with AI-generated personalized recommendations.
-
Enhance your prompt for more tailored suggestions:
-
Automate Notifications (Optional)
-
Add a notification function (e.g., email or webhook):
import requests def send_notification(student_email, message): # Replace with your email or webhook service webhook_url = "https://your-notification-service.com/api/send" data = {"to": student_email, "message": message} requests.post(webhook_url, json=data) -
Call
send_notification()in your/enrollendpoint after enrollment decision. -
Test notifications by simulating enrollments:
sqlite-utils add-column enrollments.db students email text sqlite-utils update enrollments.db students 1 email="alice@example.com"
Screenshot description: Notification payload being sent after enrollment status changes.
-
Add a notification function (e.g., email or webhook):
Common Issues & Troubleshooting
-
OpenAI API errors: Ensure your
OPENAI_API_KEYis set and has GPT-4 access. Check for typos and sufficient quota. - Database locked or permission errors: Make sure no other processes are holding the SQLite file. Use PostgreSQL for production.
- AI not recommending alternatives: Tweak your GPT-4 prompt for clarity. Test with different student/course data.
- FastAPI not starting: Check for syntax errors and ensure all dependencies are installed in your virtual environment.
Next Steps
- Enhance security: Add authentication and role-based access to your API.
- Improve AI context: Integrate student interests, GPA, or previous feedback for even smarter recommendations.
- Scale up: Move to a production database (e.g., PostgreSQL) and deploy with Docker or a cloud platform.
- Expand workflow automation: Integrate with LMS, payment gateways, or attendance tracking.
For more advanced automations across cloud platforms, check out Building AI Workflow Automations Across Multi-Cloud Environments in 2026: A Step-by-Step Guide.
To explore how leading institutions are transforming education with AI workflows, see our AI Workflow Automation for Education: Top Platforms and Case Studies for 2026 article.
You’ve just built a robust, AI-powered course enrollment workflow—ready to scale and adapt for the future of education!