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%.
@@ -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':
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)
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
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).
- 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
- GitHub issue: #9257
- Fix PR: https://github.com/pydantic/pydantic/pull/9279
- First fixed release: 2.7.1
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.33
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…”
“Looks like it's only a problem when we have use_enum_values in the config.”
Failure Signature (Search String)
- Error: RuntimeError: Unable to apply constraint strict to schema function-after
Error Message
Stack trace
Error Message
-------------
Error: RuntimeError: Unable to apply constraint strict to schema function-after
Stack trace
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
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
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.
When NOT to Use This Fix
- This fix should not be used if strict constraints are not required for enum values.
Verify Fix
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
| Version | Status |
|---|---|
| 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.