Backtestspydanticrun-20261013-104308#13692

Issue #13692 Merged fix PR #13699 Run 13 Oct 2026 · run-20261013-104308 pydantic/pydantic

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

Better

Our patch was judged better than the merged fix.

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

Both patches fix the reported crash the same way: they change the `_SecretField` serializer from `when_used='always'` to `when_used='unless-none'`. With that change, pydantic-core passes an unvalidated `None` default straight through as `null`, matching how `int = None` behaves. So `SecretStr`/`SecretBytes` with a `None` default now dump to `{"s":null}` in both patches, and set values still serialize as `**********` because `unless-none` only skips the serializer for `None`.

Patch A applies the same fix to other serializers that would crash in exactly the same way on an unvalidated `None` default. These are `Secret[T]` (`_serialize_secret`), `ByteSize` (`int(None)`), `EncodedBytes`/`EncodedStr` (encoding `None`), and `Pattern` (`attrgetter('pattern')` on `None` in JSON mode, now `json-unless-none`). These edits are low-risk: they only change behavior for `None` values, which previously raised. They also match the 'consistent with everything else' expectation in the issue, though this extra scope is mostly neutral.

The main difference is testing. Patch A has a parametrized test covering all affected types in `model_dump`, `model_dump(mode='json')` and `model_dump_json`, run with warnings promoted to errors. It also has a regression test confirming that set secret values still mask correctly. Patch B adds one test covering only `SecretStr`, with a placeholder `"""<link>"""` docstring, and does not check that non-None serialization is unchanged.

Patch B is a correct minimal fix, but it leaves the identical crash in `Secret[T]` and its tests are much thinner. Patch A is better on test strength without adding meaningful risk.

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

6m 58s

Wall-clock time in the container

Full test suite

No new failures

1,407 passed · 40 skipped · 1,447 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

69vs 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+63 −6
pydantic/_internal/_generate_schema.pyindex 3766cfee..abf72741 100644@@ -1826,7 +1826,7 @@ class GenerateSchema:          metadata = {'pydantic_js_functions': [lambda _1, _2: {'type': 'string', 'format': 'regex'}]}         ser = core_schema.plain_serializer_function_ser_schema(-            attrgetter('pattern'), when_used='json', return_schema=core_schema.str_schema()+            attrgetter('pattern'), when_used='json-unless-none', return_schema=core_schema.str_schema()         )         if pattern_type is typing.Pattern or pattern_type is re.Pattern:             # bare typepydantic/types.pyindex 50f94cf4..98deb5ff 100644@@ -1731,7 +1731,7 @@ class Secret(_SecretBase[SecretType]):             serialization=core_schema.plain_serializer_function_ser_schema(                 _serialize_secret,                 info_arg=True,-                when_used='always',+                when_used='unless-none',             ),         ) @@ -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',                 ),             ) @@ -2150,7 +2150,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)+                int, return_schema=core_schema.int_schema(ge=0), when_used='unless-none'             ),         ) @@ -2594,7 +2594,9 @@ 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.encode, when_used='unless-none'+            ),         )      def decode(self, data: bytes, _: core_schema.ValidationInfo) -> bytes:@@ -2693,7 +2695,9 @@ 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.encode_str, when_used='unless-none'+            ),         )      def decode_str(self, data: str, _: core_schema.ValidationInfo) -> str:tests/test_types.pyindex d168c449..02d205e1 100644@@ -1131,6 +1131,59 @@ def test_secret_str_min_max_length():     assert Foobar(password=value).password.get_secret_value() == value  +@pytest.mark.parametrize(+    'field_type',+    [+        SecretStr,+        SecretBytes,+        Secret[int],+        Secret[str],+        ByteSize,+        Base64Str,+        Base64Bytes,+        Base64UrlStr,+        Base64UrlBytes,+        re.Pattern,+    ],+    ids=[+        'SecretStr',+        'SecretBytes',+        'Secret[int]',+        'Secret[str]',+        'ByteSize',+        'Base64Str',+        'Base64Bytes',+        'Base64UrlStr',+        'Base64UrlBytes',+        'Pattern',+    ],+)+def test_unvalidated_none_default_serialization(field_type) -> None:+    """An unvalidated `None` default is serialized as `None`/`null` without errors or warnings, like `int = None`."""++    class Model(BaseModel):+        n: int = None+        value: field_type = None++    m = Model()+    with warnings.catch_warnings():+        warnings.simplefilter('error')+        assert m.model_dump() == {'n': None, 'value': None}+        assert m.model_dump(mode='json') == {'n': None, 'value': None}+        assert m.model_dump_json() == '{"n":null,"value":null}'+++def test_secret_none_default_does_not_change_set_value_serialization() -> None:+    class Model(BaseModel):+        s: SecretStr = None+        b: SecretBytes = None+        i: Secret[int] = None++    m = Model(s='password', b=b'password', i=1)+    assert m.model_dump_json() == '{"s":"**********","b":"**********","i":"**********"}'+    assert m.model_dump() == {'s': m.s, 'b': m.b, 'i': m.i}++ def test_secretbytes_json():     class Foobar(BaseModel):         password: SecretBytes 
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}'