Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Models and Types

Field Types in Pydantic

A tour of the types Pydantic understands: numbers, strings, bytes, dates and times, Decimal, UUID, Path, Enum and more, with parsing rules and runnable examples.

The power of Pydantic comes from the types you can use in annotations. Besides int and str, it understands dates, decimals, UUIDs, paths, enums, and many more, and it knows how to parse each one from strings and JSON. In this lesson you will see the common scalar types and the parsing rules you need to know.

Numbers

int, float, bool and Decimal are the numeric types. Use Decimal for money, because floats cannot represent amounts such as 0.1 exactly. Pydantic parses numeric strings for Decimal without ever passing through a float, so no precision is lost.

Python Numbers and money

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that Decimal is written to JSON as a string ("0.10") so no client can lose precision by parsing it as a float.

Strings and bytes

str accepts only text. bytes accepts bytes and also strings, which it encodes as UTF-8. Whitespace handling and length limits are covered in Field Constraints.

Dates and times

date, datetime, time and timedelta come from the standard datetime module. Pydantic accepts ISO 8601 strings, and for datetime also Unix timestamps (ints and floats).

Python Dates from strings and numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A timezone-aware datetime keeps its offset. A string without an offset gives a naive datetime. If you need to insist on one or the other, use AwareDatetime or NaiveDatetime from pydantic; you will see special types in Validating Special Types.

Store and exchange timestamps in UTC with an explicit offset (Z or +00:00). Convert to a local zone only for display.

UUID, Path and friends

Python UUID, Path, IP address

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Other standard types that work out of the box: IPv6Address, IPv4Network, re.Pattern, bytes, complex, range... and in the typing module Any, Callable, NewType, TypedDict, NamedTuple and Protocol (for static typing).

Enums

Use an Enum when a field has a fixed set of named choices. Pydantic accepts either the member itself or its value, and rejects anything else with an error that lists the allowed values.

Python Enum fields

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

By default model_dump() keeps enum members. Pass mode="json" (or use model_dump_json) to get the plain values.

Using Any and object

Annotating a field as Any turns checking off for it: any value is accepted unchanged. It is useful for truly free-form data, but it also hides mistakes, so use it sparingly. Prefer a precise type or a union (Unions and Literals).

The type system at a glance

Numbers

int, float
Decimal Exact money
bool yes, no, 1, 0
1 / 4

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is Decimal better than float for prices?

    Floats are binary approximations (0.1 is not exact). Decimal keeps decimal digits exactly, and Pydantic parses strings into Decimal without a float step.
  2. 2. A datetime field receives the string '2030-06-15T19:30:00' (no offset). What do you get?

    No offset means a naive datetime. Use AwareDatetime if you want to require an offset.
  3. 3. An Enum field with values 'admin', 'editor', 'viewer' receives 'boss'. What happens?

    Enum fields accept a member or its value only. Other input is an error.
  4. 4. What does model_dump(mode='json') do with an Enum member?

    mode='json' produces JSON-compatible values, so the enum is replaced by its value.

Summary

  • Pydantic parses numbers, strings, bytes, dates, times, durations, UUIDs, paths, IP addresses and enums from strings and JSON.
  • Use Decimal for money; it is serialized as a string to protect precision.
  • Dates accept ISO 8601 strings (and timestamps for datetime); an offset-free string gives a naive datetime.
  • Enum fields accept a member or its value; mode="json" dumps the value.
  • Any turns validation off for a field; use it sparingly.

Next: Optional Fields and Defaults