Backtestspydanticrun-20261007-130130#13692

Issue #13692 Merged fix PR #13699 Run 7 Oct 2026 · run-20261007-130130 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 A = our agent, Patch B = the merged fix.

Patch B fixes the root cause with one change. It sets `when_used='unless-none'` on the SecretStr/SecretBytes plain serializer, so `_serialize_secret_field` is never called with `None`. pydantic-core then serializes the unvalidated default as `null` in both python and JSON modes. This is the issue's preferred outcome 1, and it matches how the control field `n: int = None` behaves: null with no warning, since the issue reports zero warnings from `model_dump()` with that field present. Patch B's test fails before the fix (AttributeError wrapped in PydanticSerializationError) and passes after it.

Patch A guards the serializer with `isinstance` checks and raises `PydanticSerializationUnexpectedValue`. pydantic-core catches this, falls back to inference and emits a warning, which corresponds to outcome 2. That does resolve the crash, but it changes behavior elsewhere:
- `model_dump()` in python mode previously returned `None` silently for these fields and now emits a UserWarning. Patch A's own tests assert this.
- The result is inconsistent with the `int = None` control, which does not warn.
- It also rewrites serializers for re.Pattern, ByteSize, Base64Bytes/Base64Str and generic Secret, none of which the issue mentions. These rewrites add new warnings and new code paths, for example ByteSize now raises for non-int values, which raises the regression risk without being needed for the reported bug.

Patch A's tests are more extensive, but much of what they lock in is new warning behavior rather than the requested fix. On correctness, consistency and scope, Patch B is the cleaner and safer fix.

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

11m 31s

Wall-clock time in the container

Full test suite

No new failures

12,414 passed · 501 skipped · 12,915 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

108vs 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+105 −3
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/types.pyindex 50f94cf4..7617efc5 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,20 @@ class _SecretBase(Generic[SecretType]):         raise NotImplementedError  +def _unexpected_value_error(+    expected: str, value: Any, *, include_value: bool = True+) -> PydanticSerializationUnexpectedValue:+    # `include_value=False` keeps (potentially secret) values out of the warning message+    value_info = f' with value `{value!r}`' if include_value else ''+    return PydanticSerializationUnexpectedValue(+        f'Expected `{expected}` but got `{type(value).__name__}`{value_info} - 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):+        # e.g. an unvalidated default: let pydantic-core fall back to inference and emit a warning+        raise _unexpected_value_error('Secret', value, include_value=False)     if info.mode == 'json':         return str(value)     else:@@ -1753,6 +1772,9 @@ 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):+        # e.g. an unvalidated default: let pydantic-core fall back to inference and emit a warning+        raise _unexpected_value_error('SecretStr | SecretBytes', value, include_value=False)     if info.mode == 'json':         # we want the output to always be string without the `b'` prefix for bytes,         # hence we just use `secret_display`@@ -2067,6 +2089,13 @@ class PaymentCardNumber(str): # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BYTE SIZE TYPE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  +def _serialize_byte_size(value: Any) -> int:+    if not isinstance(value, int):+        # e.g. an unvalidated default: let pydantic-core fall back to inference and emit a warning+        raise _unexpected_value_error('ByteSize', value)+    return int(value)++ class ByteSize(int):     """Converts a string representing a number of bytes with units (such as `'1KB'` or `'11.5MiB'`) into an integer. @@ -2150,7 +2179,7 @@ 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)+                _serialize_byte_size, return_schema=core_schema.int_schema(ge=0)             ),         ) @@ -2617,6 +2646,9 @@ class EncodedBytes:         Returns:             The encoded data.         """+        if not isinstance(value, (bytes, bytearray, memoryview)):+            # e.g. an unvalidated default: let pydantic-core fall back to inference and emit a warning+            raise _unexpected_value_error('bytes', value)         return self.encoder.encode(value)      def __hash__(self) -> int:@@ -2716,6 +2748,9 @@ class EncodedStr:         Returns:             The encoded data.         """+        if not isinstance(value, str):+            # e.g. an unvalidated default: let pydantic-core fall back to inference and emit a warning+            raise _unexpected_value_error('str', value)         return self.encoder.encode(value.encode()).decode()  # noqa: UP008      def __hash__(self) -> int:tests/test_types.pyindex d168c449..01d6b7a9 100644@@ -1237,6 +1237,65 @@ def test_secret_bytes_min_max_length():     assert Foobar(password=value).password.get_secret_value() == value  +@pytest.mark.parametrize(+    'field_type,expected',+    [+        (SecretStr, 'Expected `SecretStr | SecretBytes` but got `NoneType` - '),+        (SecretBytes, 'Expected `SecretStr | SecretBytes` but got `NoneType` - '),+        (Secret[int], 'Expected `Secret` but got `NoneType` - '),+        (ByteSize, 'Expected `ByteSize` but got `NoneType` with value `None` - '),+        (Base64Bytes, 'Expected `bytes` but got `NoneType` with value `None` - '),+        (Base64Str, 'Expected `str` but got `NoneType` with value `None` - '),+    ],+)+@pytest.mark.parametrize('mode', ['python', 'json'])+def test_unvalidated_none_default_serialization(field_type, expected, mode) -> None:+    """https://github.com/pydantic/pydantic/issues/13692"""++    class Model(BaseModel):+        n: int = None+        value: field_type = None++    expected_warning = f'{expected}serialized value may not be as expected.'+    with pytest.warns(UserWarning, match=re.escape(expected_warning)):+        if mode == 'json':+            assert Model().model_dump_json() == '{"n":null,"value":null}'+        else:+            assert Model().model_dump() == {'n': None, 'value': None}+++@pytest.mark.parametrize('field_type', [SecretStr, SecretBytes, Secret[str]])+def test_secret_non_secret_value_serialization(field_type) -> None:+    class Model(BaseModel):+        value: field_type++    m = Model.model_construct(value='plain')+    with pytest.warns(UserWarning, match='serialized value may not be as expected') as record:+        assert m.model_dump_json() == '{"value":"plain"}'+    with pytest.warns(UserWarning, match='serialized value may not be as expected'):+        assert m.model_dump() == {'value': 'plain'}+    # the (non-secret) value must not leak into the warning message+    assert all('plain' not in str(w.message) for w in record)+++def test_encoded_bytes_bytes_like_serialization() -> None:+    class Model(BaseModel):+        value: Base64Bytes++    assert Model.model_construct(value=bytearray(b'ab')).model_dump_json() == '{"value":"YWI="}'+    assert Model.model_construct(value=memoryview(b'ab')).model_dump_json() == '{"value":"YWI="}'+++@pytest.mark.parametrize('field_type', [re.Pattern, re.Pattern[str]], ids=['bare', 'str'])+def test_pattern_unvalidated_none_default_serialization(field_type) -> None:+    class Model(BaseModel):+        value: field_type = None++    with pytest.warns(UserWarning, match='serialized value may not be as expected'):+        assert Model().model_dump_json() == '{"value":null}'+    assert Model().model_dump() == {'value': None}++ 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}'