Backtestspydanticrun-20261009-110034#13692

Issue #13692 Merged fix PR #13699 Run 9 Oct 2026 · run-20261009-110034 pydantic/pydantic

model_dump_json crashes on a SecretStr field with an unvalidated None default (internal AttributeError instead of null/warning)

Worse

The merged fix was judged better than our patch.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Patch A fixes the root cause with a one-line change. Setting `when_used='unless-none'` on the `_SecretField` plain serializer means pydantic-core never passes `None` to `_serialize_secret_field`, so it falls through to default handling and produces `{"f":null}`. That is the issue's preferred outcome 1, matching how `Optional[SecretStr]` and `int = None` behave. Valid secrets serialize exactly as before, `model_dump()` is unchanged, and the risk is minimal. Its test reproduces the reported failure and checks both python and JSON output, so it would fail before the fix and pass after.

Patch B takes the second route: it raises `PydanticSerializationUnexpectedValue` so core falls back and emits a warning. It also extends this to `Secret`, `ByteSize`, `EncodedBytes`/`EncodedStr` and `Pattern`. That approach is defensible, but it has costs:
- It changes python-mode behavior. `model_dump()` on a `SecretStr = None` model previously returned `None` silently; now it emits a warning. The issue explicitly calls that path fine, and B's own test has to suppress warnings there.
- It alters several unrelated serializers, such as `ByteSize` moving from `int` to an isinstance-checked `_serialize`, which changes JSON-schema default handling. These changes can affect union serializer selection and existing users' outputs, which adds risk well beyond the issue's ask.
- Its main test asserts a `UserWarning` while also including `n: int = None`, which may itself produce the warning. That weakens the test as evidence for the secret-specific behavior.

The extra hardening earns no credit and adds risk. Patch A delivers the cleaner, consistent `null` outcome with a targeted, correct change.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

10m 40s

Wall-clock time in the container

Full test suite

No new failures

12,405 passed · 501 skipped · 12,906 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

89vs 12 merged

Changed lines, ours vs merged

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed the bug exists on the latest version of pydantic (2.13.4) and on current main.

Python Version

3.13.14

Pydantic Version

pydantic 2.13.4 / pydantic-core (latest from pip at time of report)

Description

A model field annotated SecretStr with an unvalidated None default crashes serialization to JSON. Per models docs, field defaults are only validated when validate_default=True, so constructing such a model without arguments is legal — and every other scalar annotation serializes the unvalidated default as null. For SecretStr, however, model_dump_json() raises instead.

Note this is specific to the JSON path and to the non-nullable annotation:

  • model_dump() (python mode) returns None fine;
  • Optional[SecretStr] = None dumps to JSON correctly ({"s":null});
  • a control field n: int = None also dumps fine — so only the combination "non-nullable SecretStr + None default + JSON serializer" hits the broken path.

Example Code

import warnings
from typing import Optional
from pydantic import BaseModel, SecretStr

class M(BaseModel):
    n: int = None          # same pattern, control group
    s: SecretStr = None    # crashes

with warnings.catch_warnings(record=True) as w:
    warnings.simplefilter("always")
    print(M().model_dump())          # {'n': None, 's': None}  -- OK
    try:
        print(M().model_dump_json())
    except Exception as e:
        print(type(e).__name__, "|", e)
    print("warnings emitted:", len(w))

class O(BaseModel):
    s: Optional[SecretStr] = None

print(O().model_dump_json())         # {"s":null} -- OK

Actual behavior

{'n': None, 's': None}
PydanticSerializationError | Error calling function '_serialize_secret_field': AttributeError: 'NoneType' object has no attribute 'get_secret_value'
warnings emitted: 0
{"s":null}

The exception escapes even though the value mismatch is exactly the situation the serialization layer normally degrades gracefully on (serialize as-is / warn). No UserWarning about the mismatched default is emitted either.

Looking at _serialize_secret_field, it calls .get_secret_value() unconditionally when the serializer runs with when_used='always'; there is no guard for value is None on a non-optional annotation, while core handles None defaults for other types before user serializers are reached.

Expected behavior

Either of the consistent-with-everything-else outcomes:

  1. serialize the unvalidated default as null: {"n":null,"s":null} (matching how int = None, str = None etc. behave), or
  2. at minimum emit the standard serialization-mismatch UserWarning instead of raising an internal AttributeError wrapped into PydanticSerializationError.

The current traceback points users at pydantic internals rather than their own model definition.

The two patches

Our patch+83 −6
pydantic/_internal/_generate_schema.pyindex 3766cfee..1b7812c1 100644@@ -1825,8 +1825,16 @@ class GenerateSchema:         from . import _validators          metadata = {'pydantic_js_functions': [lambda _1, _2: {'type': 'string', 'format': 'regex'}]}++        def ser_pattern(pattern: Any) -> str | bytes:+            if not isinstance(pattern, re.Pattern):+                raise PydanticSerializationUnexpectedValue(+                    f"Expected `{re.Pattern}` but got `{type(pattern)}` with value `'{pattern}'` - serialized value may not be as expected."+                )+            return pattern.pattern+         ser = core_schema.plain_serializer_function_ser_schema(-            attrgetter('pattern'), when_used='json', return_schema=core_schema.str_schema()+            ser_pattern, when_used='json', return_schema=core_schema.str_schema()         )         if pattern_type is typing.Pattern or pattern_type is re.Pattern:             # bare typepydantic/json_schema.pyindex ebcd0f52..d917cb70 100644@@ -1258,7 +1258,8 @@ class GenerateJsonSchema:                     # such validation during JSON Schema generation so we don't support                     # this pattern for now.                     # (One example is when using `foo: ByteSize = '1MB'`, which validates and-                    # serializes as an int. In this case, `ser_func` is `int` and `int('1MB')` fails).+                    # serializes as an int. In this case, `ser_func` is `ByteSize._serialize`, which+                    # rejects the non-int `'1MB'`).                     self.emit_warning(                         'non-serializable-default',                         f'Unable to serialize value {default!r} with the plain serializer; excluding default from JSON schema',pydantic/types.pyindex 50f94cf4..2a44cfd8 100644@@ -29,7 +29,13 @@ from uuid import UUID  import annotated_types from annotated_types import BaseMetadata, MaxLen, MinLen-from pydantic_core import CoreSchema, PydanticCustomError, SchemaSerializer, core_schema+from pydantic_core import (+    CoreSchema,+    PydanticCustomError,+    PydanticSerializationUnexpectedValue,+    SchemaSerializer,+    core_schema,+) from typing_extensions import (  # noqa: UP035 (for `get_args` and `get_origin`)     Protocol,     TypeAliasType,@@ -1574,7 +1580,15 @@ class _SecretBase(Generic[SecretType]):         raise NotImplementedError  +def _unexpected_value_error(expected: Any, value: Any) -> PydanticSerializationUnexpectedValue:+    return PydanticSerializationUnexpectedValue(+        f"Expected `{expected}` but got `{type(value)}` with value `'{value}'` - serialized value may not be as expected."+    )++ def _serialize_secret(value: Secret[SecretType], info: core_schema.SerializationInfo) -> str | Secret[SecretType]:+    if not isinstance(value, Secret):+        raise _unexpected_value_error(Secret, value)     if info.mode == 'json':         return str(value)     else:@@ -1753,6 +1767,8 @@ def _secret_display(value: SecretType) -> str:  # type: ignore def _serialize_secret_field(     value: _SecretField[SecretType], info: core_schema.SerializationInfo ) -> str | _SecretField[SecretType]:+    if not isinstance(value, _SecretField):+        raise _unexpected_value_error(_SecretField, value)     if info.mode == 'json':         # we want the output to always be string without the `b'` prefix for bytes,         # hence we just use `secret_display`@@ -2150,10 +2166,16 @@ class ByteSize(int):                 custom_error_message='could not parse value and unit from byte string',             ),             serialization=core_schema.plain_serializer_function_ser_schema(-                int, return_schema=core_schema.int_schema(ge=0)+                cls._serialize, return_schema=core_schema.int_schema(ge=0)             ),         ) +    @classmethod+    def _serialize(cls, value: Any) -> int:+        if not isinstance(value, int):+            raise _unexpected_value_error(cls, value)+        return int(value)+     @classmethod     def _validate(cls, input_value: Any, /, _: core_schema.ValidationInfo) -> ByteSize:         try:@@ -2594,9 +2616,14 @@ class EncodedBytes:         return core_schema.with_info_after_validator_function(             function=self.decode,             schema=schema,-            serialization=core_schema.plain_serializer_function_ser_schema(function=self.encode),+            serialization=core_schema.plain_serializer_function_ser_schema(function=self._serialize),         ) +    def _serialize(self, value: Any) -> bytes:+        if not isinstance(value, (bytes, bytearray, memoryview)):+            raise _unexpected_value_error(bytes, value)+        return self.encode(value)+     def decode(self, data: bytes, _: core_schema.ValidationInfo) -> bytes:         """Decode the data using the specified encoder. @@ -2693,9 +2720,14 @@ class EncodedStr:         return core_schema.with_info_after_validator_function(             function=self.decode_str,             schema=schema,-            serialization=core_schema.plain_serializer_function_ser_schema(function=self.encode_str),+            serialization=core_schema.plain_serializer_function_ser_schema(function=self._serialize_str),         ) +    def _serialize_str(self, value: Any) -> str:+        if not isinstance(value, str):+            raise _unexpected_value_error(str, value)+        return self.encode_str(value)+     def decode_str(self, data: str, _: core_schema.ValidationInfo) -> str:         """Decode the data using the specified encoder. tests/test_types.pyindex d168c449..1d0c74c3 100644@@ -1237,6 +1237,42 @@ def test_secret_bytes_min_max_length():     assert Foobar(password=value).password.get_secret_value() == value  +@pytest.mark.parametrize(+    'field_type',+    [+        pytest.param(SecretStr, id='SecretStr'),+        pytest.param(SecretBytes, id='SecretBytes'),+        pytest.param(Secret[int], id='Secret[int]'),+        pytest.param(ByteSize, id='ByteSize'),+        pytest.param(Base64Str, id='Base64Str'),+        pytest.param(Base64Bytes, id='Base64Bytes'),+        pytest.param(re.Pattern, id='Pattern'),+        pytest.param(re.Pattern[str], id='Pattern[str]'),+    ],+)+def test_unvalidated_none_default_serialization(field_type) -> None:+    """https://github.com/pydantic/pydantic/issues/13692"""++    class Model(BaseModel):+        n: int = None+        x: field_type = None++    with pytest.warns(UserWarning, match='PydanticSerializationUnexpectedValue'):+        assert Model().model_dump_json() == '{"n":null,"x":null}'++    with warnings.catch_warnings():+        warnings.simplefilter('ignore')+        assert Model().model_dump() == {'n': None, 'x': None}+++def test_base64_bytes_serializes_bytes_like_without_warning() -> None:+    ta = TypeAdapter(Base64Bytes)+    with warnings.catch_warnings():+        warnings.simplefilter('error')+        assert ta.dump_python(bytearray(b'ab'), mode='json') == 'YWI='+        assert ta.dump_python(memoryview(b'ab'), mode='json') == 'YWI='++ def test_generic_without_params():     class Model(BaseModel):         generic_list: list 
Merged fix · PR #13699+11 −1
pydantic/types.pyindex 50f94cf43..a12b19100 100644@@ -1795,7 +1795,7 @@ class _SecretField(_SecretBase[SecretType]):                 serialization=core_schema.plain_serializer_function_ser_schema(                     _serialize_secret_field,                     info_arg=True,-                    when_used='always',+                    when_used='unless-none',                 ),             ) tests/test_types.pyindex d168c4490..5d54df58f 100644@@ -2424,3 +2424,13 @@ def test_custom_serializer_override_secret_str() -> None:      u = User(name='sam', password='hi')     assert u.model_dump()['password'] == 'secret: **********'+++def test_secret_str_none() -> None:+    """https://github.com/pydantic/pydantic/issues/13692"""++    class Model(BaseModel):+        f: SecretStr = None++    assert Model().model_dump() == {'f': None}+    assert Model().model_dump_json() == '{"f":null}'