Skip to main content

How to Use a Path Field in a Pydantic Model

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
from pathlib import Path

from pydantic import BaseModel


class <Model>(BaseModel):
<field>: Path = Path("<default path>")
PartWhat it means
<field>: PathThe 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​

path_field.py
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)

The path_field.py script in a macOS-style code window: a Settings model with a database field typed Path and defaulting to shop.db, printed once with the default and once after passing the text data/orders.db, then the path's name and 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.
  • .name and .parent are Path attributes: they only work if the value really is a Path.

Step 2: Run It​

Terminal
uv run python path_field.py
Output
PosixPath('shop.db')
PosixPath('data/orders.db')
orders.db data

The output in a terminal: PosixPath('shop.db'), PosixPath('data/orders.db'), then orders.db and 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​

MistakeWhat happensDo 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 stringNo error: it becomes PosixPath('.'), the current folderCheck for empty text before you build the settings
Expecting Path to check that the file existsIt doesn't: any path is acceptedUse Pydantic's FilePath for a file that must already exist

Troubleshooting​

SymptomLikely causeFix
Path does not point to a file [type=path_not_file, ...]The field is a FilePath and the file isn't thereCreate 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​