Jump to solution
Verify

The Fix

pip install pydantic==2.7.1

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

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -195,6 +195,12 @@ def apply_known_metadata(annotation: Any, schema: CoreSchema) -> CoreSchema | No allowed_schemas = CONSTRAINTS_TO_ALLOWED_SCHEMAS[constraint] + # if it becomes necessary to handle more than one constraint + # in this recursive case with function-after or function-wrap, we should refactor + if schema_type in {'function-before', 'function-wrap', 'function-after'} and constraint == 'strict':
repro.py
from enum import Enum from typing import Annotated from pydantic import ConfigDict, Strict, BaseModel class SomeEnum(int, Enum): SOME_KEY = 1 class Foo(BaseModel): model_config = ConfigDict(strict=True, use_enum_values=True) foo: Annotated[SomeEnum, Strict(strict=False)] f = Foo(foo=SomeEnum.SOME_KEY) print(f.foo)
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.7.1\nWhen NOT to use: This fix should not be used if strict constraints are not required for enum values.\n\n

Why This Fix Works in Production

  • Trigger: Error: RuntimeError: Unable to apply constraint strict to schema function-after
  • Mechanism: The strict constraint was not correctly applied to the function-after schema when using use_enum_values
  • Why the fix works: Fixes the application of the 'strict' constraint to the 'function-after' schema when using 'use_enum_values'. (first fixed release: 2.7.1).
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • The strict constraint was not correctly applied to the function-after schema when using use_enum_values
  • Surfaces as: Error: RuntimeError: Unable to apply constraint strict to schema function-after

Proof / Evidence

Discussion

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

“@Ryaningli, Ah, thanks for the report. Definitely looks like a bug, likely related to the migration of our enum validation to rust. Will look into…”
@sydney-runkle · 2024-04-16 · source
“Looks like it's only a problem when we have use_enum_values in the config.”
@sydney-runkle · 2024-04-18 · source

Failure Signature (Search String)

  • Error: RuntimeError: Unable to apply constraint strict to schema function-after

Error Message

Stack trace
error.txt
Error Message ------------- Error: RuntimeError: Unable to apply constraint strict to schema function-after
Stack trace
error.txt
Error Message ------------- Traceback (most recent call last):\n File "E:\store\Testing\testing\test8.py", line 10, in <module>\n class Foo(BaseModel):\n File "C:\Users\16699\.virtualenvs\Testing-fdkT3PoQ\Lib\site-packages\pydantic\_internal\_model_construction.py", line 202, in __new__\n complete_model_class(\n File "C:\Users\16699\.virtualenvs\Testing-fdkT3PoQ\Lib\site-packages\pydantic\_internal\_model_construction.py", line 539, in complete_model_class\n schema = cls.__get_pydantic_core_schema__(cls, handler)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "C:\Users\16699\.virtualenvs\Testing-fdkT3PoQ\Lib\site-packages\pydantic\main.py", line 607, in __get_pydantic_core_schema__\n return handler(source)\n ^^^^^^^^^^^^^^^\n File "C:\Users\16699\.virtualenvs\Testing-fdkT3PoQ\Lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 82, in __call__\n schema = self._handler(source_type)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "C:\Users\16699\.virtualenvs\Testing-fdkT3PoQ\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 499, in generate_schema\n schema = self._generate_schema_inner(obj)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "C:\Users\16699\.virtualenvs\Testing-fdkT3PoQ\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 750, in _generate_schema_inner\n return self._model_schema(obj)\n ... (truncated) ...

Minimal Reproduction

repro.py
from enum import Enum from typing import Annotated from pydantic import ConfigDict, Strict, BaseModel class SomeEnum(int, Enum): SOME_KEY = 1 class Foo(BaseModel): model_config = ConfigDict(strict=True, use_enum_values=True) foo: Annotated[SomeEnum, Strict(strict=False)] f = Foo(foo=SomeEnum.SOME_KEY) print(f.foo)

Environment

  • Pydantic: 2

What Broke

Users experienced runtime errors when applying strict constraints to models with enum values.

Why It Broke

The strict constraint was not correctly applied to the function-after schema when using use_enum_values

Fix Options (Details)

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

pip install pydantic==2.7.1

When NOT to use: This fix should not be used if strict constraints are not required for enum values.

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/9279

First fixed release: 2.7.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 strict constraints are not required for enum values.

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.7.1 Fixed

Related Issues

No related fixes found.

Sources

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