Skip to main content
Version: 1.48

XGBoost model missing feature names

When an XGBoost model is trained on a DataFrame, inferencing the model deployed in Deeploy might give an error similar to:

Exception: Failed to call predictive model: {'statusCode': 500, 'timestamp': '2025-05-22T07:39:18.640Z', 'path': '/workspaces/180feca5-e854-4e08-8a2b-613d23e7a053/deployments/b17fe3d6-fe4e-47f8-bce8-d00783be1bad/predict', 'message': 'An error has occurred while calling the model endpoint, server returned HTTP 500: Internal Server Error training data did not have the following fields: feature name 1, feature name 2, feature name 3'}

To solve this error, there are two options:

Option 1

Make sure to convert your DataFrame into a Numpy Array and train the model using Numpy Array and DMatrix.

# Import libraries
from xgboost import DMatrix, train
import pandas as pd
import os

# Create df and specify X and y
df = pd.DataFrame({
'A': [1, 4, 7],
'B': [2, 5, 8],
'C': [3, 6, 9],
'target': [0, 1, 1]
})

X = df[['A', 'B', 'C']]
y = df['target']

# Convert X and y to Numpy Arrays
X_array = X.to_numpy()
y_array = y.to_numpy()

# Create DMatrix for optimal memory efficiency and training speed
data_dmatrix = DMatrix(data=X_array, label=y_array)

# Set XGBoost parameters
params = {
'objective': 'binary:logistic',
'learning_rate': 0.1,
'random_state': 42
}

# Train the model
model = train(params, data_dmatrix)

# Create 'model' folder and 'model.json' file
model_dir = "./model"
JSON_FILE = "model.json"
# Save model object
model_file = os.path.join((model_dir), JSON_FILE)
model.save_model(model_file)

Option 2

In case you already trained your model on a DataFrame, remove the content of the featureNames key from an existing model.json file using the following code snippet.

# Open the JSON file, load it, modify it, and save it back
with open('model.json', 'r') as f:
d = json.load(f)

# Modify the 'featureNames' key
d['featureNames'] = []

# Save the updated dictionary back to the JSON file
with open('model_no_feature_names.json', 'w') as f:
json.dump(d, f, indent=4)