Skip to main content
POST
/
predict
Single Prediction
curl --request POST \
  --url https://api.example.com/predict \
  --header 'Content-Type: application/json' \
  --data '
{
  "student_country": "<string>",
  "days_on_platform": 123,
  "minutes_watched": 123,
  "courses_started": 123,
  "practice_exams_started": 123,
  "practice_exams_passed": 123,
  "minutes_spent_on_exams": 123
}
'
{
  "predicted_purchase_probability": 123,
  "predicted_purchase": 123
}

Predict Purchase Probability

Predicts whether a student will make a purchase based on their engagement metrics.

Endpoint

POST /predict

Request Body

Send a JSON object with the student’s engagement features.
student_country
string
required
Student’s country code or name. Must be between 2 and 64 characters.Validation: min_length=2, max_length=64
days_on_platform
integer
required
Number of days since the student joined the platform.Validation: >= 0
minutes_watched
number
required
Total minutes of video content watched by the student.Validation: >= 0.0
courses_started
integer
required
Number of courses the student has started.Validation: >= 0
practice_exams_started
integer
required
Number of practice exams the student has begun.Validation: >= 0
practice_exams_passed
integer
required
Number of practice exams the student has successfully passed.Validation: >= 0Business Rule: Cannot exceed practice_exams_started
minutes_spent_on_exams
number
required
Total minutes spent taking practice exams.Validation: >= 0.0

Response

Returns the prediction result with probability and binary classification.
predicted_purchase_probability
number
required
Probability score between 0.0 and 1.0 indicating likelihood of purchase.Higher values indicate higher purchase intent.
predicted_purchase
integer
required
Binary prediction: 1 for predicted purchase, 0 for no purchase.Determined by comparing probability against the trained threshold.

Status Codes

  • 200 OK - Prediction successful
  • 422 Unprocessable Entity - Validation error (e.g., practice_exams_passed > practice_exams_started)
  • 503 Service Unavailable - Model not loaded

Example Request

cURL
curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -H "accept: application/json" \
  -d '{
    "student_country": "United States",
    "days_on_platform": 45,
    "minutes_watched": 320.5,
    "courses_started": 3,
    "practice_exams_started": 5,
    "practice_exams_passed": 3,
    "minutes_spent_on_exams": 87.2
  }'

Example Response

200 OK
{
  "predicted_purchase_probability": 0.7834,
  "predicted_purchase": 1
}

Error Responses

Validation Error

422 Unprocessable Entity
{
  "detail": "practice_exams_passed cannot exceed practice_exams_started."
}

Service Unavailable

503 Service Unavailable
{
  "detail": "Model is not loaded."
}

Implementation Details

Defined in src/api.py:284-289 Request Model: PredictRequest (src/api.py:27-34)
class PredictRequest(BaseModel):
    student_country: str = Field(..., min_length=2, max_length=64)
    days_on_platform: int = Field(..., ge=0)
    minutes_watched: float = Field(..., ge=0)
    courses_started: int = Field(..., ge=0)
    practice_exams_started: int = Field(..., ge=0)
    practice_exams_passed: int = Field(..., ge=0)
    minutes_spent_on_exams: float = Field(..., ge=0)
Response Model: PredictResponse (src/api.py:37-39)
class PredictResponse(BaseModel):
    predicted_purchase_probability: float
    predicted_purchase: int

Feature Engineering

The endpoint automatically applies feature engineering transformations defined in src/features.py:
  • Engagement Score: Weighted combination of minutes watched, days on platform, and courses started
  • Exam Success Rate: practice_exams_passed / (practice_exams_started + epsilon)
  • Efficiency: minutes_watched / (days_on_platform + epsilon)
These engineered features are used by the model for prediction.

Monitoring

Each prediction is:
  1. Logged to artifacts/prediction_log.jsonl with timestamp and features
  2. Tracked for drift monitoring (feature distributions and prediction rates)
  3. Counted in the global monitoring state for /monitoring/drift endpoint

Build docs developers (and LLMs) love