Validation JSON schema keeps a numeric temporal `default` under `type: string`
Initial Checks
- I confirm that I'm using Pydantic V2
Description
Set ser_json_temporal to 'seconds' or 'milliseconds', give a temporal field a default, and the validation JSON schema declares the field as an ISO 8601 string while setting its default to a number. Both sit in the same property and contradict each other. ser_json_timedelta='float' does the same for timedelta on its own.
#13711 made _common_temporal_schema() mode aware, so validation schemas now stay {'type': 'string'}. encode_default() didn't get the same treatment. It still dumps the default through the full config in both modes, so the default keeps the serialization format.
This bites anyone publishing model_json_schema() as the request half of an OpenAPI document with epoch timestamps on the wire. The request schema says date-time string and ships default: 1704164645.0. Generated clients and any spec validation step in CI see the mismatch. Serialization mode is consistent, since the declared type is {'type': 'number'} there and the numeric default fits.
What I get on main at 915896d16:
{'default': 1704164645.0, 'format': 'date-time', 'title': 'Starts At', 'type': 'string'}
jsonschema errors: ["1704164645.0 is not of type 'string'"]What I expected: 'default': '2024-01-02T03:04:05', and no errors when the schema's own defaults are checked against it.
One more thing I ran into while digging. If the default is a BaseModel instance whose own config sets ser_json_temporal, encode_default() takes the _type_has_config() branch and that model's own serializer runs, so its default stays numeric even when the $defs entry beside it says string. That looks like a separate fix and I've left it out of the repro.
Example Code
from datetime import datetime
import jsonschema
from pydantic import BaseModel, ConfigDict
class Event(BaseModel):
model_config = ConfigDict(ser_json_temporal='seconds')
starts_at: datetime = datetime(2024, 1, 2, 3, 4, 5)
schema = Event.model_json_schema()
print(schema['properties']['starts_at'])
defaults = {k: v['default'] for k, v in schema['properties'].items()}
errors = list(jsonschema.Draft202012Validator(schema).iter_errors(defaults))
print('jsonschema errors:', [e.message for e in errors] or 'none')Python, Pydantic & OS Version
pydantic version: 2.14.0b2
pydantic-core version: 2.49.0
pydantic-core build: profile=release pgo=false
python version: 3.10.6 (v3.10.6:9c7b4bd164, Aug 1 2022, 17:13:48) [Clang 13.0.0 (clang-1300.0.29.30)]
platform: macOS-26.6.2-arm64-arm-64bit
related packages: typing_extensions-4.16.0
commit: 915896d16I have a fix ready. Happy to open a PR if you assign this to me.
Source: pydantic/pydantic