Epython Lab
Ir al canal en Telegram
Welcome to Epython Lab, where you can get resources to learn, one-on-one trainings on machine learning, business analytics, and Python, and solutions for business problems. Buy ads: https://telega.io/c/epythonlab
Mostrar más6 140
Suscriptores
Sin datos24 horas
-117 días
-5130 días
Archivo de publicaciones
6 139
While learning FastAPI, I came across another question: 𝐖𝐡𝐲 𝐝𝐨 𝐈 𝐧𝐞𝐞𝐝 𝐏𝐲𝐝𝐚𝐧𝐭𝐢𝐜 𝐰𝐡𝐞𝐧 𝐏𝐲𝐭𝐡𝐨𝐧 𝐚𝐥𝐫𝐞𝐚𝐝𝐲 𝐡𝐚𝐬 𝐭𝐲𝐩𝐞 𝐡𝐢𝐧𝐭𝐬?
For example, I can write:
𝑑𝑒𝑓 𝑐𝑟𝑒𝑎𝑡𝑒_𝑢𝑠𝑒𝑟(𝑛𝑎𝑚𝑒: 𝑠𝑡𝑟, 𝑎𝑔𝑒: 𝑖𝑛𝑡):
...
Python already tells me that name should be a string and age should be an integer.
So what does 𝐏𝐲𝐝𝐚𝐧𝐭𝐢c add?
The difference became clear when working with API request data.
An API receives data from outside the application, and I need more than just type hints. I need to validate that the data has the expected structure and values.
That’s where Pydantic becomes useful.
With FastAPI, I can define a model:
𝑐𝑙𝑎𝑠𝑠 𝑈𝑠𝑒𝑟𝐶𝑟𝑒𝑎𝑡𝑒(𝐵𝑎𝑠𝑒𝑀𝑜𝑑𝑒𝑙):
𝑛𝑎𝑚𝑒: 𝑠𝑡𝑟 𝑎𝑔𝑒: 𝑖𝑛𝑡
FastAPI can then use that model to validate incoming request data automatically.
So my takeaway is:
𝐏𝐲𝐭𝐡𝐨𝐧 𝐭𝐲𝐩𝐞 𝐡𝐢𝐧𝐭𝐬 𝐝𝐞𝐬𝐜𝐫𝐢𝐛𝐞 𝐰𝐡𝐚𝐭 𝐈 𝐞𝐱𝐩𝐞𝐜𝐭.
𝐏𝐲𝐝𝐚𝐧𝐭𝐢𝐜 𝐡𝐞𝐥𝐩𝐬 𝐯𝐚𝐥𝐢𝐝𝐚𝐭𝐞 𝐰𝐡𝐚𝐭 𝐈 𝐚𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐫𝐞𝐜𝐞𝐢𝐯𝐞.
This is another small concept that became much clearer once I started building a real FastAPI application.
𝐖𝐡𝐚𝐭 𝐰𝐚𝐬 𝐲𝐨𝐮𝐫 𝐟𝐢𝐫𝐬𝐭 𝐢𝐦𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧 𝐨𝐟 𝐏𝐲𝐝𝐚𝐧𝐭𝐢𝐜 𝐰𝐡𝐞𝐧 𝐲𝐨𝐮 𝐬𝐭𝐚𝐫𝐭𝐞𝐝 𝐮𝐬𝐢𝐧𝐠 𝐅𝐚𝐬𝐭𝐀𝐏𝐈?
🔗 𝐆𝐢𝐭𝐇𝐮𝐛: https://github.com/epythonlab2/ai-document-api
🎥 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 𝐜𝐨𝐮𝐫𝐬𝐞: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM
6 139
The FastAPI Course Github (AI Document API) Project is just updated
Add database connection setup and document model definition
- Implement database connection using SQLAlchemy and environment variables
- Create Document model with fields for title, description, category, and priority
- Update .gitignore to include .env file
- Add required dependencies in pyproject.toml and uv.lock
🔗 𝐆𝐢𝐭𝐇𝐮𝐛: https://github.com/epythonlab2/ai-document-api
🎥 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 𝐜𝐨𝐮𝐫𝐬𝐞: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM
6 139
While moving from other web frameworks to 𝐅𝐚𝐬𝐭𝐀𝐏𝐈, one question came to my mind:
𝐖𝐡𝐲 𝐝𝐨 𝐈 𝐧𝐞𝐞𝐝 𝐀𝐏𝐈𝐑𝐨𝐮𝐭𝐞𝐫 𝐰𝐡𝐞𝐧 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 𝐚𝐥𝐫𝐞𝐚𝐝𝐲 𝐥𝐞𝐭𝐬 𝐦𝐞 𝐝𝐞𝐟𝐢𝐧𝐞 𝐫𝐨𝐮𝐭𝐞𝐬 𝐝𝐢𝐫𝐞𝐜𝐭𝐥𝐲?
For a small project, this is enough:
@𝒂𝒑𝒑.𝒈𝒆𝒕("/𝒅𝒐𝒄𝒖𝒎𝒆𝒏𝒕𝒔")
𝒂𝒔𝒚𝒏𝒄 𝒅𝒆𝒇 𝒈𝒆𝒕_𝒅𝒐𝒄𝒖𝒎𝒆𝒏𝒕𝒔():
...
So why introduce APIRouter?
The answer became clearer when I started thinking about a real application.
As the project grows, I may have authentication, users, documents, search, AI processing, and many more endpoints.
Keeping all of these routes in one place can quickly become difficult to maintain.
That’s where APIRouter becomes useful.
It allows me to organize routes by feature and keep the application structure clean.
So my takeaway is simple:
𝐅𝐚𝐬𝐭𝐀𝐏𝐈 𝐥𝐞𝐭𝐬 𝐦𝐞 𝐝𝐞𝐟𝐢𝐧𝐞 𝐭𝐡𝐞 𝐫𝐨𝐮𝐭𝐞𝐬.
𝐀𝐏𝐈𝐑𝐨𝐮𝐭𝐞𝐫 𝐡𝐞𝐥𝐩𝐬 𝐦𝐞 𝐨𝐫𝐠𝐚𝐧𝐢𝐳𝐞 𝐭𝐡𝐞𝐦.
It’s not required to make FastAPI work. It becomes useful when I want the application to stay organized and maintainable as it grows.
This was one of those small FastAPI concepts that made much more sense once I started looking at the bigger picture.
𝐇𝐚𝐯𝐞 𝐲𝐨𝐮 𝐡𝐚𝐝 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐪𝐮𝐞𝐬𝐭𝐢𝐨𝐧 𝐰𝐡𝐞𝐧 𝐦𝐨𝐯𝐢𝐧𝐠 𝐭𝐨 𝐅𝐚𝐬𝐭𝐀𝐏𝐈?
🔗 𝐆𝐢𝐭𝐇𝐮𝐛: https://github.com/epythonlab2/ai-document-api
🎥 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 𝐜𝐨𝐮𝐫𝐬𝐞: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM
#FastAPI #Python #APIRouter #BackendDevelopment #SoftwareArchitecture #APIDevelopment #PythonDeveloper
6 139
This is the github starter for the project based FastAPI course tutorial
https://github.com/epythonlab2/ai-document-api
6 139
When building a FastAPI application, I pay close attention to how data is validated before it reaches the business logic.
That is where Pydantic becomes particularly useful.
Pydantic lets us define the structure and rules for the data our application accepts, instead of scattering validation checks throughout the codebase.
I use it to define and enforce the data contract at the application boundary.
For example:
𝑐𝑙𝑎𝑠𝑠 𝑃𝑟𝑜𝑐𝑒𝑠𝑠𝑖𝑛𝑔𝐶𝑜𝑛𝑓𝑖𝑔(𝐵𝑎𝑠𝑒𝑀𝑜𝑑𝑒𝑙):
𝑐ℎ𝑢𝑛𝑘_𝑠𝑖𝑧𝑒: 𝑖𝑛𝑡 = 𝐹𝑖𝑒𝑙𝑑(𝑔𝑡=0)
𝑜𝑣𝑒𝑟𝑙𝑎𝑝: 𝑖𝑛𝑡 = 𝐹𝑖𝑒𝑙𝑑(𝑔𝑒=0)
@𝑚𝑜𝑑𝑒𝑙_𝑣𝑎𝑙𝑖𝑑𝑎𝑡𝑜𝑟(𝑚𝑜𝑑𝑒="𝑎𝑓𝑡𝑒𝑟")
𝑑𝑒𝑓 𝑣𝑎𝑙𝑖𝑑𝑎𝑡𝑒_𝑐𝑜𝑛𝑓𝑖𝑔(𝑠𝑒𝑙𝑓):
𝑖𝑓 𝑠𝑒𝑙𝑓.𝑜𝑣𝑒𝑟𝑙𝑎𝑝 >= 𝑠𝑒𝑙𝑓.𝑐ℎ𝑢𝑛𝑘_𝑠𝑖𝑧𝑒:
𝑟𝑎𝑖𝑠𝑒 𝑉𝑎𝑙𝑢𝑒𝐸𝑟𝑟𝑜𝑟("𝑜𝑣𝑒𝑟𝑙𝑎𝑝 𝑚𝑢𝑠𝑡 𝑏𝑒 𝑠𝑚𝑎𝑙𝑙𝑒𝑟 𝑡ℎ𝑎𝑛 𝑐ℎ𝑢𝑛𝑘_𝑠𝑖𝑧𝑒")
𝑟𝑒𝑡𝑢𝑟𝑛 𝑠𝑒𝑙𝑓
Both fields can be individually valid, while their combination is not.
That is the difference between:
Field validation → Is this value valid?
Model validation → Is this combination valid?
With Pydantic, 𝙁𝙞𝙚𝙡𝙙(), 𝙛𝙞𝙚𝙡𝙙_𝙫𝙖𝙡𝙞𝙙𝙖𝙩𝙤𝙧(), 𝙖𝙣𝙙 𝙢𝙤𝙙𝙚𝙡_𝙫𝙖𝙡𝙞𝙙𝙖𝙩𝙤𝙧() let us keep these rules close to the schema.
The result is a cleaner boundary:
𝙍𝙚𝙦𝙪𝙚𝙨𝙩 → 𝙑𝙖𝙡𝙞𝙙𝙖𝙩𝙞𝙤𝙣 → 𝘽𝙪𝙨𝙞𝙣𝙚𝙨𝙨 𝙇𝙤𝙜𝙞𝙘 → 𝘿𝙖𝙩𝙖𝙗𝙖𝙨𝙚 / 𝘼𝙄
For production AI applications, this matters. Document metadata, processing parameters, search filters, and structured AI outputs all need predictable contracts.
Good schemas do more than describe data. They protect the rest of the system.
You can explore more: https://www.youtube.com/playlist?list=PLQNCas8_eikM
#Python #Pydantic #PydanticV2 #FastAPI #BackendEngineering #AIEngineering
6 139
The hardest part of building AI applications isn't writing the prompt or calling the model. In the last two weeks, I learned that keeping the backend from turning into spaghetti code once you move past the tutorial phase.
When you're wiring up an AI document pipeline in FastAPI, a few things quickly become non-negotiable:
• Payload Guardrails: If your Pydantic schemas aren't catching malformed JSON, missing nested fields, or bad Enums at the door, your AI service will fail unpredictably downstream.
• Route Isolation: Mixing your raw API endpoints with validation logic and business rules makes refactoring a nightmare by week three.
• The Persistence Gap: Transitioning from mock in-memory data structures to a real relational database and a vector store for RAG is where most clean prototypes start to break down.
If you're building production backends for AI and ML features, where do you usually draw the line between keeping things simple and over-engineering your architecture?
You can learn about FastAPI: https://www.youtube.com/playlist?list=PLQNCas8_eikM
#FastAPI #Python #BackendEngineering #SoftwareArchitecture #APIs #Pydantic #ArtificialIntelligence #MachineLearning #RAG
6 139
FastAPI Episode 6: Pydantic Validation Requests and Response Models
https://youtu.be/G5cKA88-6vc
6 139
FastAPI Full Course Episode 5: FastAPI Parameters & Request Bodies(Path, Query & Pydantic) https://www.youtube.com/watch?v=-tkww4I4Vfg&t=638s
6 139
Set Up FastAPI Development Environment with uv & VS Code | FastAPI Full Course(Episode 4)
https://www.youtube.com/watch?v=G60LwkySnwQ
6 139
Your model can look excellent and still be wrong.
One of the first things I check when evaluating an ML dataset is data leakage. 🔍
Data leakage happens when information that would not actually be available at prediction time gets into the training data.
For example:
🏥 Healthcare
You are predicting whether a patient will be admitted, but your dataset includes a field recorded after admission.
💳 Fraud detection
You are predicting fraud, but one of the features is created after the transaction has already been investigated.
📦 Customer churn
You are predicting who will leave, but the training data contains information that only becomes available after the customer leaves.
The result?
Your model may show:
📈 98% accuracy
📈 Excellent validation results
📈 Great performance during testing
Then you put it into production...
And the performance drops.
The problem was not necessarily the model.
The model had access to information it would never have in the real world.
That is why I don't look at model performance alone.
I also ask:
🔎 Where did each feature come from?
⏱️ When was it created?
🎯 Would this information actually be available when making the prediction?
A high score is not always a good score.
Sometimes, it is a warning sign.
Check out data quality issues
https://youtube.com/playlist?list=PL0nX4ZoMtjYHTtowSzzB2gVH2AuuoF9WW&si=EhLKvJCVlYQXknOs
Also checkout data quality checker tool https://datasetdoctor.fastapicloud.dev
#MachineLearning #DataScience #AI #DataLeakage #MLOps #Python
6 139
𝐁𝐞𝐟𝐨𝐫𝐞 𝐜𝐡𝐚𝐧𝐠𝐢𝐧𝐠 𝐲𝐨𝐮𝐫 𝐌𝐋 𝐦𝐨𝐝𝐞𝐥, 𝐜𝐡𝐞𝐜𝐤 𝐲𝐨𝐮𝐫 𝐝𝐚𝐭𝐚.
When a model performs badly, the first thing we often do is try a different algorithm.
Sometimes that works.
But before doing that, I usually look at the dataset. 🔍
I check things like:
🔹 Missing values
🔹 Duplicate records
🔹 Outliers
🔹 Wrong data types
🔹 Class imbalance
🔹 Data leakage
🔹 High-cardinality columns
🔹 Features with little useful information
There is no point spending hours tuning a model if the dataset itself has problems. ⚠️
A simple workflow I prefer is:
📥 𝑹𝒂𝒘 𝑫𝒂𝒕𝒂
↓
🔎 𝑪𝒉𝒆𝒄𝒌 𝑸𝒖𝒂𝒍𝒊𝒕𝒚
↓
🧹 𝑪𝒍𝒆𝒂𝒏
↓
📊 𝑨𝒏𝒂𝒍𝒚𝒛𝒆
↓
🤖 𝑻𝒓𝒂𝒊𝒏
↓
📈 𝑴𝒐𝒏𝒊𝒕𝒐𝒓
Data quality is not just something to deal with before machine learning. It affects every step that comes after it.
So when a model is not performing as expected, don't immediately change the model.
🔍 Take another look at the data first. https://lnkd.in/d7MW42N8
Use Data quality checker tool: https://datasetdoctor.fastapicloud.dev
#MachineLearning #DataScience #AI #DataQuality #MLOps #Python
6 139
𝐅𝐚𝐬𝐭𝐀𝐏𝐈 𝐯𝐬 𝐑𝐄𝐒𝐓 𝐀𝐏𝐈 — What’s the Difference?
One thing I see quite often when people start building APIs with Python is confusion between FastAPI and REST API.
In reality, they are not the same thing.
𝐑𝐄𝐒𝐓 𝐀𝐏𝐈 is an architectural approach for designing APIs around resources, HTTP methods, stateless communication, and standard HTTP responses.
𝐅𝐚𝐬𝐭𝐀𝐏𝐈 is a Python web framework that helps you build APIs.
For example, in an AI application, I might have:
GET /documents
𝙶𝙴𝚃 /𝚍𝚘𝚌𝚞𝚖𝚎𝚗𝚝𝚜
𝙿𝙾𝚂𝚃 /𝚍𝚘𝚌𝚞𝚖𝚎𝚗𝚝𝚜
𝙶𝙴𝚃 /𝚍𝚘𝚌𝚞𝚖𝚎𝚗𝚝𝚜/{𝚒𝚍}
𝙿𝚄𝚃 /𝚍𝚘𝚌𝚞𝚖𝚎𝚗𝚝𝚜/{𝚒𝚍}
𝙳𝙴𝙻𝙴𝚃𝙴 /𝚍𝚘𝚌𝚞𝚖𝚎𝚗𝚝𝚜/{𝚒𝚍}
These endpoints can follow 𝐑𝐄𝐒𝐓 principles.
𝐅𝐚𝐬𝐭𝐀𝐏𝐈 is the tool I use to implement them in Python.
So, a simple way to remember it:
𝐑𝐄𝐒𝐓 = how the API is designed
𝐅𝐚𝐬𝐭𝐀𝐏𝐈 = the framework used to build it
𝐅𝐚𝐬𝐭𝐀𝐏𝐈 also gives us useful features such as request validation, automatic API documentation, dependency injection, and strong support for asynchronous applications.
Understanding this distinction makes it much easier to understand 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 and, more importantly, to design APIs properly.
𝐅𝐚𝐬𝐭𝐀𝐏𝐈 Fundamentals: Build Your First AI API | Python FastAPI Course (An Overview of API): https://youtu.be/vvP9GIWSews
#FastAPI #Python #RESTAPI #APIDevelopment #AI #MachineLearning #BackendDevelopment
6 139
FastAPI Fundamentals: Build Your First AI API | Python FastAPI Course (Episode 2 - Overview of API)
https://youtu.be/vvP9GIWSews
6 139
When I build an AI application, choosing the backend framework is an important decision.
There are several good options, but I usually look at 𝐅𝐚𝐬𝐭𝐀𝐏𝐈, 𝐃𝐣𝐚𝐧𝐠𝐨, and 𝐅𝐥𝐚𝐬𝐤 first.
The choice really depends on what I'm building.
✔️ 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 makes a lot of sense when the application is mainly an AI/API backend. Since most AI tools I use are already in Python, I can keep the whole stack in one ecosystem, from LLMs and embeddings to document processing, RAG, databases, and the API itself.
✔️ 𝐃𝐣𝐚𝐧𝐠𝐨 is a strong choice when the AI functionality is part of a larger web application. Its built-in ORM, authentication, admin panel, and other features can save a lot of development time.
✔️ 𝐅𝐥𝐚𝐬𝐤 is still a great option when I want something simple, lightweight, and flexible, especially for smaller services or prototypes.
For an AI application, I also need to think beyond the framework:
✔️ Authentication
✔️ Database and data persistence
✔️ Document processing
✔️ Embeddings and vector search
✔️ RAG
✔️ Background tasks
✔️ Testing
✔️ Docker
✔️ Monitoring
✔️ Deployment
There isn't one framework that is "best" for every AI application. For the type of production AI backends I'm building, 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 is often a practical choice because it provides a clean API layer while keeping everything close to the Python AI ecosystem.
The framework is only one piece of the puzzle.
Good architecture matters more than the framework you choose.
What do you normally use for AI applications: 𝐅𝐚𝐬𝐭𝐀𝐏𝐈, 𝐃𝐣𝐚𝐧𝐠𝐨, 𝐅𝐥𝐚𝐬𝐤, or something else?
Here is the roadmap to build an AI application with FastAPI: https://www.youtube.com/watch?v=0SLLG2Z_Htw
#FastAPI #Python #AIEngineering #GenerativeAI #RAG #BackendDevelopment #MachineLearning #Django #Flask #SoftwareArchitecture
6 139
FastAPI From Zero: Build a Production AI API | Episode 1 - Course Overview
https://www.youtube.com/watch?v=0SLLG2Z_Htw
6 139
When I build an AI agent, I do not start by asking, Which model should I use? I start by designing the system around the model.
The model provides reasoning and language capabilities. The surrounding architecture determines whether the agent is reliable, controllable, and production ready.
This is the approach I follow:
𝟏. 𝐌𝐨𝐝𝐞𝐥: I select the model based on reasoning capability, task complexity, latency, cost, and context requirements.
𝟐. 𝐓𝐨𝐨𝐥𝐬: I give the agent well-defined tools with strict schemas, validation, permissions, and predictable outputs.
𝟑. 𝐂𝐨𝐧𝐭𝐞𝐱𝐭: I carefully control the information provided to the model through retrieval, memory, conversation state, and structured context.
𝟒. 𝐎𝐫𝐜𝐡𝐞𝐬𝐭𝐫𝐚𝐭𝐢𝐨𝐧: I define how the agent reasons, when it can call tools, when it should retry, when it should ask for clarification, and when it must stop.
𝟓. 𝐆𝐮𝐚𝐫𝐝𝐫𝐚𝐢𝐥𝐬: I validate inputs, tool calls, and outputs. For sensitive or high-impact operations, I add additional verification.
𝟔. 𝐎𝐛𝐬𝐞𝐫𝐯𝐚𝐛𝐢𝐥𝐢𝐭𝐲: I monitor tool calls, model responses, latency, failures, token usage, and agent execution paths.
𝟕. 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧: I test the complete system against realistic scenarios, edge cases, adversarial inputs, and expected failure modes.
▶️ I walk through how to build this kind of agent from scratch here:
https://www.youtube.com/watch?v=AgconCK-l4g
#AIEngineering #AIAgents #GenerativeAI #LLM #MachineLearning #Python #LangChain #LangGraph #MLOps #SoftwareEngineering
