ValidationError
pydantic.error_wrappers.ValidationError
Stack trace
pydantic.error_wrappers.ValidationError: 1 validation error for InstructorModel field_name field required (type=value_error.missing) During handling of the above exception, the Instructor library failed to parse the input data according to the Pydantic model.
Why it happens
Instructor uses Pydantic models to validate input data structures strictly. This error occurs when the data passed to the model is missing required fields, has incorrect field names, or contains data types that do not match the model's schema. This mismatch causes Pydantic to raise a ValidationError.
Detection
Catch ValidationError exceptions around Instructor model instantiations and log the input data to identify missing or mismatched fields before the application crashes.
Causes & fixes
Input dictionary keys do not match the Pydantic model field names exactly (case-sensitive).
Ensure that all keys in the input data dictionary match the Pydantic model's field names exactly, including case.
Required fields defined in the Pydantic model are missing from the input data.
Add all required fields to the input data before passing it to the Instructor model.
Input data fields have incorrect data types that do not conform to the Pydantic model schema.
Convert or validate input data types to match the expected types defined in the Pydantic model.
Using an outdated or incompatible version of the Instructor library or Pydantic causing schema mismatches.
Upgrade Instructor and Pydantic to compatible versions as per the library documentation.
Code: broken vs fixed
from instructor import InstructorModel
input_data = {'text': 'Example text'} # Missing required fields
model = InstructorModel(**input_data) # This line raises ValidationError import os
from instructor import InstructorModel
input_data = {
'text': 'Example text',
'embedding_dim': 768 # Added required field
}
model = InstructorModel(**input_data) # Fixed: all required fields present
print('Model created successfully:', model) Workaround
Wrap the model instantiation in try/except ValidationError, then log or inspect the raw input data to manually fix missing or mismatched fields before retrying.
Prevention
Use strict type checking and schema validation tools during development to ensure input data always matches the Instructor Pydantic model schema before runtime.