Jump to solution
Verify

The Fix

Add support for `defer_build` with `@validate_call` decorator to resolve issues with static methods and class methods in Pydantic.

Based on closed pydantic/pydantic issue #11582 · 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
@@ -49,7 +49,18 @@ def wrapper_function(*args, **kwargs): """This is a wrapper around a function that validates the arguments passed to it, and optionally the return value.""" - __slots__ = ('__pydantic_validator__', '__return_pydantic_validator__') + __slots__ = ( + 'function',
repro.py
from __future__ import ( annotations, ) # without this, there will be an error in static type-checking from pydantic import BaseModel, Field, validate_call class ClassA(BaseModel): field_a: int = Field(default=2) # EXPLICIT RETURN ClassA will lead to an error at runtime @classmethod @validate_call def does_not_work_1( cls, field_a: int, ) -> ClassA: return cls(field_a=field_a) # EXPLICIT RETURN ClassA will lead to an error at runtime @staticmethod @validate_call def does_not_work_2( field_a: int, ) -> ClassA: return ClassA(field_a=field_a) # NO ERROR @classmethod @validate_call def work_1( cls, field_a: int, ): return cls(field_a=field_a) # NO ERROR @staticmethod @validate_call def work_2( field_a: int, ): return ClassA(field_a=field_a)
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Apply the official fix\nAdd support for `defer_build` with `@validate_call` decorator to resolve issues with static methods and class methods in Pydantic.\nWhen NOT to use: Do not use this fix if the class structure is not defined at the module level.\n\nOption C — Workaround\nthis by setting [`defer_build`](https://docs.pydantic.dev/dev/api/config/#pydantic.config.ConfigDict.defer_build) to `True`. It currently has no effect for `@validate_call`, but I'm making a PR to make it work in 2.11:\nWhen NOT to use: Do not use this fix if the class structure is not defined at the module level.\n\n

Why This Fix Works in Production

  • Trigger: class ClassA(BaseModel):
  • Mechanism: The validate_call decorator fails with staticmethods/classmethods due to namespace resolution issues
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 validate_call decorator fails with staticmethods/classmethods due to namespace resolution issues
  • Surfaces as: Traceback (most recent call last):

Proof / Evidence

Discussion

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

“I have the same issue as indicated above”
@hqtuan99 · 2025-03-19 · source
“By the time the methods are defined, ClassA does not exist (as is it in the process of having its namespace defined)”
@Viicos · 2025-03-19 · source

Failure Signature (Search String)

  • class ClassA(BaseModel):

Error Message

Stack trace
error.txt
Error Message ------------- Traceback (most recent call last): File "D:\crpatcher\test.py", line 6, in <module> class ClassA(BaseModel): File "D:\crpatcher\test.py", line 11, in ClassA @validate_call ^^^^^^^^^^^^^ File "D:\crpatcher\.venv\Lib\site-packages\pydantic\validate_call_decorator.py", line 113, in validate_call return validate(func) ^^^^^^^^^^^^^^ File "D:\crpatcher\.venv\Lib\site-packages\pydantic\validate_call_decorator.py", line 107, in validate validate_call_wrapper = _validate_call.ValidateCallWrapper( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\crpatcher\.venv\Lib\site-packages\pydantic\_internal\_validate_call.py", line 72, in __init__ schema = gen_schema.clean_schema(gen_schema.generate_schema(function)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\crpatcher\.venv\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 610, in generate_schema schema = self._generate_schema_inner(obj) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\crpatcher\.venv\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 884, in _generate_schema_inner return self.match_type(obj) ^^^^^^^^^^^^^^^^^^^^ File "D:\crpatcher\.venv\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 975, in match_type return self. ... (truncated) ...

Minimal Reproduction

repro.py
from __future__ import ( annotations, ) # without this, there will be an error in static type-checking from pydantic import BaseModel, Field, validate_call class ClassA(BaseModel): field_a: int = Field(default=2) # EXPLICIT RETURN ClassA will lead to an error at runtime @classmethod @validate_call def does_not_work_1( cls, field_a: int, ) -> ClassA: return cls(field_a=field_a) # EXPLICIT RETURN ClassA will lead to an error at runtime @staticmethod @validate_call def does_not_work_2( field_a: int, ) -> ClassA: return ClassA(field_a=field_a) # NO ERROR @classmethod @validate_call def work_1( cls, field_a: int, ): return cls(field_a=field_a) # NO ERROR @staticmethod @validate_call def work_2( field_a: int, ): return ClassA(field_a=field_a)

Environment

  • Pydantic: 2

What Broke

Static methods using validate_call raise errors during schema generation, causing application failures.

Why It Broke

The validate_call decorator fails with staticmethods/classmethods due to namespace resolution issues

Fix Options (Details)

Option A — Apply the official fix

Add support for `defer_build` with `@validate_call` decorator to resolve issues with static methods and class methods in Pydantic.

When NOT to use: Do not use this fix if the class structure is not defined at the module level.

Option C — Workaround Temporary workaround

this by setting [`defer_build`](https://docs.pydantic.dev/dev/api/config/#pydantic.config.ConfigDict.defer_build) to `True`. It currently has no effect for `@validate_call`, but I'm making a PR to make it work in 2.11:

When NOT to use: Do not use this fix if the class structure is not defined at the module level.

Use only if you cannot change versions today. Treat this as a stopgap and remove once upgraded.

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

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

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not use this fix if the class structure is not defined at the module level.

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.

Related Issues

No related fixes found.

Sources

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