Skip to main content

Fixed: "Value should have at least 1 item after validation, not 0" for an Empty Pydantic SecretStr

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

My settings class rejected an empty API key, as intended, but the message read "Value should have at least 1 item after validation, not 0". Items? It was a key, not a list. The cause was the place I'd put the rule. Here's the whole story, with real output from Pydantic 2.14.4.

What I Was Doing​

I was building the settings of a Claude-powered support agent with pydantic-settings. The Claude API key comes from a .env file, and the settings must refuse to start without one. I typed the field as SecretStr, so the key never shows in logs, and added Field(min_length=1) so an empty value is rejected. When the settings fail, the agent prints each problem on one line: Check your .env file. followed by the field and Pydantic's message.

To test the rule, I left the key empty in .env:

.env (excerpt)
ANTHROPIC_API_KEY=

What I Expected​

A message about the key being too short, as Pydantic gives for an ordinary string with min_length=1: "String should have at least 1 character".

What I Got​

Terminal
uv run support-chat
Output
Check your .env file. ANTHROPIC_API_KEY: Value should have at least 1 item after validation, not 0

A terminal where uv run support-chat stops with Check your .env file. ANTHROPIC_API_KEY: Value should have at least 1 item after validation, not 0.

The empty key was rejected, so the rule worked. But "item" and "after validation" describe a list, and a user reading it wouldn't know what to change.

What I Checked​

1. The same thing in a small model, with the error type. The agent prints only the message, so I printed the type too:

secret_min_length.py
from pydantic import BaseModel, Field, SecretStr, ValidationError


class Settings(BaseModel):
api_key: SecretStr = Field(min_length=1)


try:
Settings(api_key="")
except ValidationError as error:
for problem in error.errors():
print(problem["type"], problem["msg"])
Terminal
uv run python secret_min_length.py
Output
too_short Value should have at least 1 item after validation, not 0

The type is too_short, not string_too_short. Pydantic's list of validation errors shows the difference: string_too_short "is raised when the input value is a string whose length is less than the field's min_length constraint", while too_short "is raised when the value length is less than the field's min_length constraint", with a list as its example. So the length rule wasn't checking the text at all: it ran on the value as a whole, like a collection.

2. What Pydantic's documentation says about SecretStr and constraints. The Types page is direct: built-in types such as str accept constraints, but extra types such as SecretStr "are implemented using the patterns described in the custom types section. Strictness and constraints can't be applied on them."

3. How Pydantic says to constrain a secret. The API documentation of Secret gives the way: "You can enforce constraints on the underlying type through annotations", with the example Secret[Annotated[int, Field(gt=0, strict=True)]].

Why It Happens​

Field(min_length=1) attached to a SecretStr field isn't a string rule, because constraints aren't supported on SecretStr. Pydantic still applied it, as a general length check on the finished value, which reports in "items". The fix is to put the constraint on the text inside the secret.

The Fix​

Use the generic Secret around a constrained str:

src/store_support_agent/config.py (excerpt)
from typing import Annotated

from pydantic import Field, Secret

# A secret text that must not be empty. The constraint goes on the text inside the secret.
ApiKey = Secret[Annotated[str, Field(min_length=1)]]

anthropic_api_key: ApiKey = Field(
validation_alias="ANTHROPIC_API_KEY",
description="Your Claude API key, from the Claude Console.",
)

The fixed field in a macOS-style code window: ApiKey is Secret of Annotated str with Field(min_length=1), and anthropic_api_key is typed as ApiKey with its environment variable name and description.

Annotated[str, Field(min_length=1)] is a string that must have at least one character, and Secret[...] hides it the way SecretStr does. Read the value with .get_secret_value(), as before.

After the Fix​

The agent, with the key still empty:

Terminal
uv run support-chat
Output
Check your .env file. ANTHROPIC_API_KEY: String should have at least 1 character

A terminal where uv run support-chat stops with Check your .env file. ANTHROPIC_API_KEY: String should have at least 1 character.

The small model gives the matching type, and a valid key still prints hidden:

secret_fixed.py
from typing import Annotated

from pydantic import BaseModel, Field, Secret, ValidationError

ApiKey = Secret[Annotated[str, Field(min_length=1)]]


class Settings(BaseModel):
api_key: ApiKey


try:
Settings(api_key="")
except ValidationError as error:
for problem in error.errors():
print(problem["type"], problem["msg"])

print(Settings(api_key="abc123"))
Output
string_too_short String should have at least 1 character
api_key=Secret('**********')

Common Mistakes​

MistakeWhat happensDo this instead
SecretStr with Field(min_length=1)too_short: "Value should have at least 1 item after validation, not 0"Secret[Annotated[str, Field(min_length=1)]]
Dropping the secret type to get the normal messageThe key prints in full in logs and errorsKeep it secret; constrain the inner type

Troubleshooting​

SymptomLikely causeFix
The error type is too_short on a text fieldThe constraint is on a wrapper type, not on strMove it inside, onto str, with Annotated

FAQ​

Does the rule still reject an empty key with SecretStr?​

Yes, the empty key was rejected either way. The fix changes the message to one that names the real problem.

Does Secret[...] behave like SecretStr otherwise?​

For this use, yes: it prints as ********** and returns the text with get_secret_value().

Conclusion​

SecretStr with Field(min_length=1) turned an empty key into a list-style too_short error, because Pydantic doesn't support constraints on SecretStr. Secret[Annotated[str, Field(min_length=1)]] put the rule on the text and gave "String should have at least 1 character".

Additional Resources​