Skip to content

Unexpected behaviour when combining pattern and MISSING #12971

@JensHeinrich

Description

@JensHeinrich

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When looking at the generated schema for fields allowing the MISSING Sentinel the pattern is lost.
This holds true for assignment of the Field(pattern=...) as well as when using Annotated unless the MISSING sentinel is in the outer union.

This is especially weird as description and title are properly handled in this combination

Example Code

from typing import Annotated

from pydantic import BaseModel, Field
from pydantic.experimental.missing_sentinel import MISSING


def test_missing_schema_pattern_minimal_assignment():
    class Model(BaseModel):
        a: str | MISSING = Field(title='title', description='Something', pattern='^[a-z]+$')

    assert Model.model_json_schema()['properties'] == {
        'a': {
            'description': 'Something',
            'pattern': '^[a-z]+$',
            'title': 'title',
            'type': 'string',
        }
    }


def test_missing_schema_pattern_minimal_annotated_in_union():
    class Model(BaseModel):
        a: Annotated[str, Field(title='title', description='Something', pattern='^[a-z]+$')] | MISSING

    assert Model.model_json_schema()['properties'] == {
        'a': {
            'description': 'Something',
            'pattern': '^[a-z]+$',
            'title': 'title',
            'type': 'string',
        }
    }


def test_missing_schema_pattern_minimal_annotated_union():
    class Model(BaseModel):
        a: Annotated[str | MISSING, Field(title='title', description='Something', pattern='^[a-z]+$')]

    assert Model.model_json_schema()['properties'] == {
        'a': {
            'description': 'Something',
            'pattern': '^[a-z]+$',
            'title': 'title',
            'type': 'string',
        }
    }

Python, Pydantic & OS Version

pydantic version: 2.13.0b2
        pydantic-core version: 2.42.0
          pydantic-core build: profile=debug pgo=false
               python version: 3.13.7 (main, Mar  3 2026, 12:19:54) [GCC 15.2.0]
                     platform: Linux-6.17.0-19-generic-x86_64-with-glibc2.42
             related packages: pyright-1.1.405 pydantic-extra-types-2.10.6 mypy-1.18.2 typing_extensions-4.14.1 pydantic-settings-2.7.1
                       commit: 704a79d50

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions