Jump to solution
Verify

The Fix

pip install pydantic==2.9.1

Based on closed pydantic/pydantic issue #8783 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -534,7 +534,6 @@ Specifically, the following config options are relevant: * [`title`][pydantic.config.ConfigDict.title] * [`json_schema_extra`][pydantic.config.ConfigDict.json_schema_extra] -* [`schema_generator`][pydantic.config.ConfigDict.schema_generator] * [`json_schema_mode_override`][pydantic.config.ConfigDict.json_schema_mode_override] * [`field_title_generator`][pydantic.config.ConfigDict.field_title_generator]
repro.py
from datetime import date, datetime from functools import partial from typing import Any, cast, override, TYPE_CHECKING from pydantic import ConfigDict, GenerateSchema, TypeAdapter from pydantic.dataclasses import dataclass from pydantic_core import core_schema from pydantic._internal._std_types_schema import InnerSchemaValidator class ApiSchema(GenerateSchema): @override def match_type(obj: object): print(obj) return super().match_type(obj) @api_dataclass class DateTimeOrNone: none: None # works date_none: date | None # only works for date datetime_none: datetime | None # only works for datetime time_none: time | None # only works for time fail_date: date # doesn't works fail_datetime: datetime # doesn't works fail_time: time # doesn't works # Example of overriding '_get_prepare_pydantic_annotations_for_known_type' and '_union_schema' (separate file, use same imports) def test_date_or_None(): test_none_dict: dict[str, str] = {"dn": ""} assert None is TypeAdapter(DateTimeOrNone).validate_python(test_none_dict).dn class ApiSchema(GenerateSchema): @override def _get_prepare_pydantic_annotations_for_known_type( self, obj: Any, annotations: tuple[Any, ...] ) -> tuple[Any, list[Any]] | None: result = None if obj is date: result = core_schema.no_info_plain_validator_function( lambda x: datetime.strptime(x, "%Y%m%d") ) if result is not None: return obj, [InnerSchemaValidator(result)] return cast( tuple[object, list[object]] | None, super()._get_prepare_pydantic_annotations_for_known_type(obj, annotations), ) @override def _union_schema(self, union_type: Any) -> core_schema.CoreSchema: schema = super()._union_schema(union_type) if schema["type"] == "nullable": schema["schema"] = core_schema.union_schema([ schema["schema"], core_schema.no_info_before_validator_function( lambda x: None if x == "" else x, core_schema.none_schema() ), ]) return schema @dataclass(config=ConfigDict(schema_generator=ApiSchema)) class DateTimeOrNone: dn: date | None = None
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\npip install pydantic==2.9.1\nWhen NOT to use: This fix should not be used if the application relies on the previous behavior of `match_type`.\n\n

Why This Fix Works in Production

  • Trigger: `GenerateSchema.match_type` does not work for union types or `datetime` types
  • Mechanism: The `match_type` method in `GenerateSchema` does not evaluate union types and datetime types correctly
  • Why the fix works: Addresses issues with the `GenerateSchema.match_type` method not working for union and datetime types, marking it as experimental in the documentation. (first fixed release: 2.9.1).

Why This Breaks in Prod

  • The `match_type` method in `GenerateSchema` does not evaluate union types and datetime types correctly
  • Production symptom (often without a traceback): `GenerateSchema.match_type` does not work for union types or `datetime` types

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“@sydney-runkle additionally, None does not work correctly.”
@KotlinIsland · 2024-03-18 · source
“See https://github.com/pydantic/pydantic/pull/10303 - I don't think we plan on supporting GenerateSchema in this way in the short term, so going to close this as not…”
@sydney-runkle · 2024-09-05 · source
“Hi @sydney-runkle”
Issue thread · 2024-02-21 · repro detail · source
“Hi @Loch64, Thanks for raising this issue”
@sydney-runkle · 2024-02-20 · source

Failure Signature (Search String)

  • `GenerateSchema.match_type` does not work for union types or `datetime` types
  • `match_type` does not completely evaluate the whole union on a class that inherits `GenerateSchema`.
Copy-friendly signature
signature.txt
Failure Signature ----------------- `GenerateSchema.match_type` does not work for union types or `datetime` types `match_type` does not completely evaluate the whole union on a class that inherits `GenerateSchema`.

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- `GenerateSchema.match_type` does not work for union types or `datetime` types `match_type` does not completely evaluate the whole union on a class that inherits `GenerateSchema`.

Minimal Reproduction

repro.py
from datetime import date, datetime from functools import partial from typing import Any, cast, override, TYPE_CHECKING from pydantic import ConfigDict, GenerateSchema, TypeAdapter from pydantic.dataclasses import dataclass from pydantic_core import core_schema from pydantic._internal._std_types_schema import InnerSchemaValidator class ApiSchema(GenerateSchema): @override def match_type(obj: object): print(obj) return super().match_type(obj) @api_dataclass class DateTimeOrNone: none: None # works date_none: date | None # only works for date datetime_none: datetime | None # only works for datetime time_none: time | None # only works for time fail_date: date # doesn't works fail_datetime: datetime # doesn't works fail_time: time # doesn't works # Example of overriding '_get_prepare_pydantic_annotations_for_known_type' and '_union_schema' (separate file, use same imports) def test_date_or_None(): test_none_dict: dict[str, str] = {"dn": ""} assert None is TypeAdapter(DateTimeOrNone).validate_python(test_none_dict).dn class ApiSchema(GenerateSchema): @override def _get_prepare_pydantic_annotations_for_known_type( self, obj: Any, annotations: tuple[Any, ...] ) -> tuple[Any, list[Any]] | None: result = None if obj is date: result = core_schema.no_info_plain_validator_function( lambda x: datetime.strptime(x, "%Y%m%d") ) if result is not None: return obj, [InnerSchemaValidator(result)] return cast( tuple[object, list[object]] | None, super()._get_prepare_pydantic_annotations_for_known_type(obj, annotations), ) @override def _union_schema(self, union_type: Any) -> core_schema.CoreSchema: schema = super()._union_schema(union_type) if schema["type"] == "nullable": schema["schema"] = core_schema.union_schema([ schema["schema"], core_schema.no_info_before_validator_function( lambda x: None if x == "" else x, core_schema.none_schema() ), ]) return schema @dataclass(config=ConfigDict(schema_generator=ApiSchema)) class DateTimeOrNone: dn: date | None = None

Environment

  • Pydantic: 2

What Broke

Users experience incorrect schema generation for union and datetime types, leading to validation errors.

Why It Broke

The `match_type` method in `GenerateSchema` does not evaluate union types and datetime types correctly

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==2.9.1

When NOT to use: This fix should not be used if the application relies on the previous behavior of `match_type`.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Fix reference: https://github.com/pydantic/pydantic/pull/10303

First fixed release: 2.9.1

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be used if the application relies on the previous behavior of `match_type`.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
  • Upgrade behind a canary and run integration tests against the canary before 100% rollout.

Version Compatibility Table

VersionStatus
2.9.1 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.