<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p" -->

---
title: My Model Worked Perfectly. Then I Tried to Make It Useful.
description: A data analytics practitioner transitioning into data engineering documents wrapping a churn prediction model (81% accuracy, trained on telecom customer data)...
canonical: https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: My Model Worked Perfectly. Then I Tried to Make It Useful. | daily.dev
og:description: A data analytics practitioner transitioning into data engineering documents wrapping a churn prediction model (81% accuracy, trained on telecom customer data)...
og:url: https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p
og:image: https://api.daily.dev/og/posts/TOIiW3a7p.png
og:image:alt: My Model Worked Perfectly. Then I Tried to Make It Useful.
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# My Model Worked Perfectly. Then I Tried to Make It Useful.

**[Towards Data Science](https://daily.dev/sources/tds)** · 12 min read · 1 upvotes · 0 comments

## Summary

A data analytics practitioner transitioning into data engineering documents wrapping a churn prediction model (81% accuracy, trained on telecom customer data) in a FastAPI service so other applications can call it instead of relying on manual Jupyter notebook runs. The piece covers designing an input/output schema boundary, sharing preprocessing logic between training and inference via a shared preprocessing.py, loading the model once at import time rather than per-request, reindexing one-hot encoded columns to match training data, and using Pydantic validation to reject bad input before it reaches the model. It ends acknowledging the service only runs locally, with containerization and AWS deployment planned for a follow-up article.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://towardsdatascience.com/my-model-worked-perfectly-then-i-tried-to-make-it-useful>

## Questions this post answers

### How do you prevent a mismatch between training-time preprocessing and inference-time preprocessing when serving a scikit-learn model through an API?

Share a single preprocessing module between the training script and the live API rather than duplicating logic. When training and inference preprocessing diverge, the model does not error, it silently produces wrong predictions because it receives numbers in a different shape or scale than it was trained on. Loading a shared preprocessing.py from both train.py and the API's model code avoids this class of bug.

_Developers wiring ML models into services can find deployment write-ups like this one through daily.dev._

### Why would a single API request to a machine learning model with one-hot encoded categorical features fail or produce wrong results?

A single request can only ever produce one value per one-hot encoded category, unlike a full training dataset that generates multiple dummy columns across many rows. If the request's resulting columns are not reindexed against the exact column list the model was trained on, with missing columns filled with zero, scikit-learn will either error or silently misalign features and produce an incorrect prediction.

_Teams debugging inference-time quirks in scikit-learn pipelines rely on daily.dev to surface practical fixes like this._

### How should input validation be handled for a FastAPI endpoint serving a machine learning model?

Input validation should act as a hard gate before data ever reaches the model, rejecting malformed requests such as a missing required field or a mismatched category value like lowercase 'female' instead of the expected 'Female'. This returns a clear, specific error rather than letting bad input reach predict_churn(), which could otherwise produce a silently wrong prediction instead of a visible failure.

_daily.dev helps developers building model-serving APIs learn validation patterns like schema-based request gating._

## Similar posts on daily.dev

- [The Machine Learning Practitioner’s Guide to Model Deployment with FastAPI](https://daily.dev/posts/the-machine-learning-practitioner-s-guide-to-model-deployment-with-fastapi-jpemcmmgm) · Machine Learning Mastery · 0 upvotes · 0 comments
- [Your Model Isn't Done Until Someone Else Can Call It](https://daily.dev/posts/your-model-isn-t-done-until-someone-else-can-call-it-fguctr287) · Towards Data Science · 5 upvotes · 0 comments
- [How to use AI to generate type-safe Python code that survives production](https://daily.dev/posts/how-to-use-ai-to-generate-type-safe-python-code-that-survives-production-vasuplwtd) · All Things Open · 1 upvotes · 0 comments
- [Building FinGuard AI: What “Almost Done” Actually Meant](https://daily.dev/posts/building-finguard-ai-what-almost-done-actually-meant-luqv1bpkh) · Medium · 0 upvotes · 0 comments
- [FastAPI Docker Deployment: Preparing ONNX AI Models for AWS Lambda](https://daily.dev/posts/fastapi-docker-deployment-preparing-onnx-ai-models-for-aws-lambda-ojhi93ghc) · PyImageSearch · 5 upvotes · 0 comments

---

Tags: [#machine-learning](https://daily.dev/tags/machine-learning), [#architecture](https://daily.dev/tags/architecture), [#mlops](https://daily.dev/tags/mlops), [#fastapi](https://daily.dev/tags/fastapi), [#scikit](https://daily.dev/tags/scikit)

[View this post on daily.dev](https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"My Model Worked Perfectly. Then I Tried to Make It Useful.","url":"https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p"},"datePublished":"2026-09-03T15:53:32.314Z","dateModified":"2026-10-15T02:22:27.988Z","description":"A data analytics practitioner transitioning into data engineering documents wrapping a churn prediction model (81% accuracy, trained on telecom customer data)...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/aa035db45a4d8afba5ad4704829139db?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/aa035db45a4d8afba5ad4704829139db?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Towards Data Science","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Towards Data Science","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/tds","url":"https://daily.dev/sources/tds"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"machine-learning,architecture,mlops,fastapi,scikit","timeRequired":"PT12M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Towards Data Science","item":"https://daily.dev/sources/tds"},{"@type":"ListItem","position":3,"name":"My Model Worked Perfectly. Then I Tried to Make It Useful."}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/my-model-worked-perfectly-then-i-tried-to-make-it-useful--toiiw3a7p#faq","mainEntity":[{"@type":"Question","name":"How do you prevent a mismatch between training-time preprocessing and inference-time preprocessing when serving a scikit-learn model through an API?","acceptedAnswer":{"@type":"Answer","text":"Share a single preprocessing module between the training script and the live API rather than duplicating logic. When training and inference preprocessing diverge, the model does not error, it silently produces wrong predictions because it receives numbers in a different shape or scale than it was trained on. Loading a shared preprocessing.py from both train.py and the API's model code avoids this class of bug. Developers wiring ML models into services can find deployment write-ups like this one through daily.dev."}},{"@type":"Question","name":"Why would a single API request to a machine learning model with one-hot encoded categorical features fail or produce wrong results?","acceptedAnswer":{"@type":"Answer","text":"A single request can only ever produce one value per one-hot encoded category, unlike a full training dataset that generates multiple dummy columns across many rows. If the request's resulting columns are not reindexed against the exact column list the model was trained on, with missing columns filled with zero, scikit-learn will either error or silently misalign features and produce an incorrect prediction. Teams debugging inference-time quirks in scikit-learn pipelines rely on daily.dev to surface practical fixes like this."}},{"@type":"Question","name":"How should input validation be handled for a FastAPI endpoint serving a machine learning model?","acceptedAnswer":{"@type":"Answer","text":"Input validation should act as a hard gate before data ever reaches the model, rejecting malformed requests such as a missing required field or a mismatched category value like lowercase 'female' instead of the expected 'Female'. This returns a clear, specific error rather than letting bad input reach predict_churn(), which could otherwise produce a silently wrong prediction instead of a visible failure. daily.dev helps developers building model-serving APIs learn validation patterns like schema-based request gating."}}]}
```

