0Pricing
MLOps Academy · درس

التحقق من الطلبات باستخدام Pydantic

ارفض المدخلات غير السليمة قبل وصولها إلى النموذج.

التحقق من الطلبات باستخدام Pydantic درس مجاني في MLOps Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MLOps Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MLOps Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Garbage In, Garbage Out

If a client sends the wrong fields or types, your model gets nonsense. You want to reject bad input before it ever reaches the model. 🛡️

Meet Pydantic

FastAPI uses Pydantic to validate requests. You describe the shape of the data with a Python class, and validation happens for free.

Define a Schema

You subclass BaseModel and list the fields with their types. This class becomes the contract for your /predict request body.

from pydantic import BaseModel

class IrisInput(BaseModel):
    sepal_length: float
    petal_width: float

Use It in the Route

You type-annotate the endpoint argument with your model. FastAPI parses, validates, and hands you a clean object.

@app.post("/predict")
def predict(data: IrisInput):
    ...

Access the Fields

Inside the function the validated data is a normal object. You read each value with simple dot access.

x = [data.sepal_length, data.petal_width]
pred = model.predict([x])

Automatic 422 Errors

If a field is missing or has the wrong type, FastAPI returns a 422 response with a clear message. You write zero error-handling code.

Add Value Constraints

You can enforce ranges with Field. Here a measurement must be greater than zero, so negatives are rejected automatically.

from pydantic import Field

sepal_length: float = Field(gt=0)

Validate a List of Rows

To score many samples at once, you accept a list of your model. Pydantic validates every item in the batch for you.

@app.post("/predict")
def predict(rows: list[IrisInput]):
    ...

Document Fields

Add descriptions and examples to fields, and they show up in the /docs page so callers know exactly what to send. 📘

sepal_length: float = Field(description="cm", examples=[5.1])

Shape the Response Too

You can declare a response_model so the output is validated and documented just like the input, keeping your API contract tight.

@app.post("/predict", response_model=Prediction)
def predict(data: IrisInput):
    ...

Why This Matters

Strong input validation is your first line of defense in production. It blocks malformed requests so the model only ever sees clean, expected data. ✅

Quick Check

A client sends a request missing a required field. What does FastAPI do thanks to Pydantic?

Recap

You defined a BaseModel schema, typed your route with it, added field constraints, and let FastAPI auto-reject bad input with clear 422 errors. Clean data in! 🙌

الأسئلة الشائعة

هل درس «التحقق من الطلبات باستخدام Pydantic» مجاني؟

نعم — نص درس «التحقق من الطلبات باستخدام Pydantic» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MLOps Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MLOps Academy 4 دروس في المجموع.

ماذا ستتعلم في «التحقق من الطلبات باستخدام Pydantic»؟

ارفض المدخلات غير السليمة قبل وصولها إلى النموذج. تتمرن على MLOps Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ MLOps Academy؟

لا تُشترط خبرة سابقة. MLOps Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «التحقق من الطلبات باستخدام Pydantic»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس MLOps Academy هذا؟

نعم. كل درس في MLOps Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. نقطة النهاية /predict الأولى
  2. التحقق من الطلبات باستخدام Pydantic
  3. تحميل النموذج مرة واحدة عند بدء التشغيل
  4. إضافة فحص الجاهزية /health
← العودة إلى MLOps Academy