How to Use a Path Field in a Pydantic Model
Settings often hold file locations: where the database lives, where to write a report. Typed as plain str, they arrive as text you still have to turn into a path. Typed as pathlib.Path, Pydantic does the conversion for you. This guide gives a settings model a database path, with real output from Pydantic 2.14.4.
Syntax
from pathlib import Path
from pydantic import BaseModel
class <Model>(BaseModel):
<field>: Path = Path("<default path>")
| Part | What it means |
|---|---|
<field>: Path | The field holds a pathlib.Path. Text you pass is turned into one |
= Path("<default path>") | The value when none is given |
Pydantic's standard library types page, on paths: "Path instances are validated as is. Strings are accepted and passed to the type constructor."
Prerequisites
- Pydantic 2 in a uv project. This guide uses Pydantic 2.14.4, added with
uv add pydantic, on Python 3.15.2.
Step 1: Write the Script
from pathlib import Path
from pydantic import BaseModel
class Settings(BaseModel):
database: Path = Path("shop.db")
print(repr(Settings().database))
settings = Settings(database="data/orders.db")
print(repr(settings.database))
print(settings.database.name, settings.database.parent)

database: Path = Path("shop.db")types the field and gives it a default.Settings()uses the default.Settings(database="data/orders.db")passes text..nameand.parentarePathattributes: they only work if the value really is aPath.
Step 2: Run It
uv run python path_field.py
PosixPath('shop.db')
PosixPath('data/orders.db')
orders.db data

The default came back as a path, and so did the text: Pydantic turned "data/orders.db" into a Path, whose .name is orders.db and .parent is data. On macOS and Linux a Path prints as PosixPath; on Windows it's a WindowsPath.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing a number, Settings(database=42) | A validation error: Input is not a valid path for <class 'pathlib.Path'> [type=path_type, input_value=42, input_type=int] | Pass text or a Path |
| Passing an empty string | No error: it becomes PosixPath('.'), the current folder | Check for empty text before you build the settings |
Expecting Path to check that the file exists | It doesn't: any path is accepted | Use Pydantic's FilePath for a file that must already exist |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Path does not point to a file [type=path_not_file, ...] | The field is a FilePath and the file isn't there | Create the file, or use Path if it may not exist yet |
FAQ
What's the difference between Path and FilePath?
Path accepts any path. FilePath also checks that the file exists: with missing.db it raised Path does not point to a file [type=path_not_file, input_value='missing.db', input_type=str].
Is the path relative to the script?
A relative path like data/orders.db is relative to the folder the program runs from, as with any Path.
Conclusion
Typed as Path, the database field turned the default and the text "data/orders.db" into Path objects, so .name and .parent worked straight away.
Additional Resources
- Pydantic: Paths — how path fields are validated.
- Pydantic: FilePath — paths that must point to an existing file.
