Skip to main content
This module defines the input and output schemas for the credit risk prediction API using Pydantic for data validation.

Input Schemas

CreditRiskInput

Defines the structure of input data for credit risk prediction. Field names match the original dataset columns.
class CreditRiskInput(BaseModel)
Age
int
required
Age of the applicant in years. Must be greater than 0.
Sex
SexEnum
required
Sex of the applicant. Valid values: male, female
Job
JobEnum
required
Job skill level of the applicant. See JobEnum for valid values.
Housing
HousingEnum
required
Housing type of the applicant. Valid values: own, rent, free
Saving accounts
SavingAccountsEnum
required
Status of the applicant’s savings account. See SavingAccountsEnum for valid values.
Checking account
CheckingAccountEnum
required
Status of the applicant’s checking account. See CheckingAccountEnum for valid values.
Credit amount
float
required
Amount of credit requested. Must be greater than 0.
Duration
int
required
Duration of the credit in months. Must be greater than 0.
Purpose
PurposeEnum
required
Purpose of the credit. See PurposeEnum for valid values.
The schema uses field aliases to support spaces in field names (e.g., “Saving accounts”, “Checking account”, “Credit amount”). Both the aliased and underscore versions are accepted.
{
  "Age": 35,
  "Sex": "male",
  "Job": "unskilled and resident",
  "Housing": "free",
  "Saving accounts": "NA",
  "Checking account": "NA",
  "Credit amount": 9055,
  "Duration": 36,
  "Purpose": "education"
}

Output Schemas

CreditRiskOutput

Defines the structure of the API response for credit risk prediction.
class CreditRiskOutput(BaseModel)
prediction
str
required
Credit risk prediction: either “good” or “bad”
probability
float
required
Probability that the risk is “good” (range: 0.0 to 1.0)
{
  "prediction": "good",
  "probability": 0.87
}

Enums

SexEnum

Defines valid values for the applicant’s sex.
class SexEnum(str, Enum):
    male = "male"
    female = "female"
ValueDescription
maleMale applicant
femaleFemale applicant

JobEnum

Defines valid job skill levels for the applicant.
class JobEnum(str, Enum):
    level_0 = "unskilled and non-resident"
    level_1 = "unskilled and resident"
    level_2 = "skilled"
    level_3 = "highly skilled"
ValueDescription
unskilled and non-residentLevel 0 - Unskilled and non-resident
unskilled and residentLevel 1 - Unskilled and resident
skilledLevel 2 - Skilled worker
highly skilledLevel 3 - Highly skilled worker
The Job field has a custom validator that converts JobEnum values to their numeric level (0-3) when processing.

HousingEnum

Defines valid housing types for the applicant.
class HousingEnum(str, Enum):
    own = "own"
    rent = "rent"
    free = "free"
ValueDescription
ownOwns housing
rentRents housing
freeFree housing

SavingAccountsEnum

Defines valid savings account statuses.
class SavingAccountsEnum(str, Enum):
    na = "NA"
    little = "little"
    moderate = "moderate"
    quite_rich = "quite rich"
    rich = "rich"
ValueDescription
NANo savings account information
littleLittle savings
moderateModerate savings
quite richQuite rich savings
richRich savings

CheckingAccountEnum

Defines valid checking account statuses.
class CheckingAccountEnum(str, Enum):
    na = "NA"
    little = "little"
    moderate = "moderate"
    rich = "rich"
ValueDescription
NANo checking account information
littleLittle balance
moderateModerate balance
richRich balance

PurposeEnum

Defines valid purposes for the credit request.
class PurposeEnum(str, Enum):
    car = "car"
    furniture_equipment = "furniture/equipment"
    radio_tv = "radio/TV"
    domestic_appliances = "domestic appliances"
    repairs = "repairs"
    education = "education"
    business = "business"
    vacation_others = "vacation/others"
ValueDescription
carPurchase a car
furniture/equipmentPurchase furniture or equipment
radio/TVPurchase radio or TV
domestic appliancesPurchase domestic appliances
repairsHome or property repairs
educationEducation expenses
businessBusiness purposes
vacation/othersVacation or other purposes

Validation

All schemas include Pydantic validation:
  • Type validation: Ensures all fields match their declared types
  • Range validation: Age, Credit amount, and Duration must be greater than 0
  • Enum validation: Categorical fields must use valid enum values
  • Probability validation: Output probability must be between 0.0 and 1.0
from server.schemas import CreditRiskInput
from pydantic import ValidationError

try:
    # This will raise a validation error (Age must be > 0)
    invalid_input = CreditRiskInput(
        Age=-5,
        Sex="male",
        Job="skilled",
        Housing="own",
        Saving_accounts="little",
        Checking_account="moderate",
        Credit_amount=5000,
        Duration=24,
        Purpose="car"
    )
except ValidationError as e:
    print(e)

Source Code Reference

View the complete implementation at server/schemas.py:1-109

Build docs developers (and LLMs) love