Skip to main content

How to Require a Non-Empty String in pydantic with min_length

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

In pydantic, a field typed str is required, but an empty string "" still counts as a string, so it passes. When a name or a setting must have a value, add Field(min_length=1). This guide rejects an empty name, with real output from pydantic 2.14.4.

Syntax​

Syntax
from pydantic import BaseModel, Field


class <Model>(BaseModel):
<field>: str = Field(min_length=<number>)
PartWhat it means
Field(min_length=<number>)The string must have at least that many characters. 1 rejects ""
ValidationErrorWhat pydantic raises when a value breaks the rule

The pydantic Field reference: "min_length : int | None Default: _Unset Minimum length for iterables."

Prerequisites​

  • pydantic in your project, for example with uv add pydantic. This guide uses pydantic 2.14.4 on Python 3.15.2.

Step 1: Write the Script​

The model's name must have at least one character. The script creates one valid user, then tries an empty name and prints the error.

user_name.py
from pydantic import BaseModel, Field, ValidationError


class User(BaseModel):
name: str = Field(min_length=1)


print(User(name="Ada"))

try:
User(name="")
except ValidationError as error:
print(error)

The user_name.py script in a macOS-style code window: a User model with name: str = Field(min_length=1), print(User(name="Ada")), then User(name="") inside try with except ValidationError printing the error.

Step 2: Run It​

Terminal
uv run python user_name.py
Output
name='Ada'
1 validation error for User
name
String should have at least 1 character [type=string_too_short, input_value='', input_type=str]
For further information visit https://errors.pydantic.dev/2.14/v/string_too_short

The script's output in a terminal: name='Ada', then 1 validation error for User, name, String should have at least 1 character, with type string_too_short and the input value of an empty string.

Ada passed. The empty name failed with string_too_short, and the error names the field and the value it got.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting min_length=1 to reject spaces" " has one character, so User(name=" ") passed as name=' 'Strip whitespace first with str_strip_whitespace, below
Checking for empty strings by hand after creating the modelEvery place that creates the model has to remember the checkPut the rule in the field, where pydantic checks it every time

Troubleshooting​

SymptomLikely causeFix
A name of only spaces gets throughmin_length counts the spacesAdd model_config = ConfigDict(str_strip_whitespace=True): " " then failed with String should have at least 1 character

FAQ​

Is there another way to reject spaces?​

A pattern: Field(min_length=1, pattern=r"\S") requires at least one character that isn't a space, and " " failed with String should match pattern '\S'.

Does min_length work for lists too?​

Yes. The reference describes it as the "Minimum length for iterables", which covers strings, lists and other collections.

Conclusion​

name: str = Field(min_length=1) let Ada through and stopped "" with String should have at least 1 character. Add str_strip_whitespace when spaces alone shouldn't count either.

Additional Resources​