#13886·pydantic

Add support for string constraints for `SecretStr` type

Author: erdnaxeliCreated Sep 28, 2026Updated Sep 29, 2026
Labelsfeature request

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I want to use SecretStr to hide a secret, but I also want to annotate it with StringConstraints to add more constraints. It does not work.

Example Code

python
from typing import Annotated

from pydantic import BaseModel, SecretStr, StringConstraints


class Settings(BaseModel):
    password_hash: Annotated[SecretStr, StringConstraints(ascii_only=True)]


Settings(password_hash="hash")

Python, Pydantic & OS Version

pydantic version: 2.13.5
        pydantic-core version: 2.46.5
          pydantic-core build: profile=release pgo=false
               python version: 3.13.1 (main, Jan 14 2025, 22:47:38) [Clang 19.1.6 ]
                     platform: Linux-7.2.7-200.fc44.x86_64-x86_64-with-glibc2.43
             related packages: pydantic-settings-2.15.0 typing_extensions-4.16.0 pydantic-extra-types-2.11.1 email-validator-2.3.0 fastapi-0.141.1
                       commit: unknown