How to Require a Non-Empty String in pydantic with min_length
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
from pydantic import BaseModel, Field
class <Model>(BaseModel):
<field>: str = Field(min_length=<number>)
| Part | What it means |
|---|---|
Field(min_length=<number>) | The string must have at least that many characters. 1 rejects "" |
ValidationError | What 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.
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)

Step 2: Run It
uv run python user_name.py
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

Ada passed. The empty name failed with string_too_short, and the error names the field and the value it got.
Common Mistakes
| Mistake | What happens | Do 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 model | Every place that creates the model has to remember the check | Put the rule in the field, where pydantic checks it every time |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A name of only spaces gets through | min_length counts the spaces | Add 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
- pydantic Field — every constraint a field can have.
- string_too_short — the error, with an example.
