- How you use it
- You ask for it
- What it can do
- writes files · runs shell
- What's inside
The option
| Option | ASkill · v1.0.0Scan Python files for Pydantic regex= keywords |
|---|---|
| How you use it | You ask for it |
| What it can do | writes files · runs shell |
| What's inside |
What this thread does
` regex is removed. use pattern instead — replace Field(regex=...) with Field(pattern=...)` and check the expression's engine compatibility.
A Pydantic model can fail during class definition before any input is validated.
First action: replace the affected Field(regex=...) keyword with Field(pattern=...) while retaining the expression.
The official usage-error example establishes PydanticUserError with code removed-kwargs; the examples below have not been executed here.
Not for: general Pydantic validator signatures or Config migration; see pydantic-v2-upgrade-validator-and-config-errors.
What changed
- Pydantic v2, released 2023-06-30 (both checked 2026-10-28), removes the
regexkeyword and defaults to the Rustregexcrate for string patterns, dropping lookarounds and backreferences. See the migration guide and dated changelog. - Pydantic 2.5.0, released 2023-11-13 (both checked 2026-10-28), exposed
regex_engine; older migration snippets may predate this switch. See the changelog entry.
How to migrate
1. Record the installed pydantic and pydantic-core versions from your lockfile. As of November 2026, the release changelog lists Pydantic 2.14.4 dated 2026-11-19 (both checked 2026-11-24); its tagged package metadata requires pydantic-core==2.50.5 (checked 2026-11-24). Let the resolver keep that pair together; do not independently upgrade core to repair a pattern. 2. Change only Pydantic string constraints from regex= to pattern=. Inspect imported aliases and generated models too. The Fields guide explains applying constraints with Field and Annotated.
from pydantic import BaseModel, Field
class Code(BaseModel):
value: str = Field(pattern=r'^[A-Z]{3}$')
3. Audit lookaheads such as (?=...), lookbehinds and backreferences. For a pattern requiring Python behavior, the official configuration example uses this setting and expression:
from pydantic import BaseModel, ConfigDict, Field
class Model(BaseModel):
model_config = ConfigDict(regex_engine='python-re')
value: str = Field(pattern=r'^abc(?=def)')
4. If only one field needs Python behavior, Field(pattern=re.compile(...)) selects Python even when the configured default is Rust, according to the same config page. Treat changing engines as a validation-policy decision: the Python engine may backtrack. For Python-only expressions, use ConfigDict(regex_engine='python-re') or a compiled re pattern. 5. For list items, attach the pattern to the string, as the migration guide specifies: list[Annotated[str, Field(pattern=".*")]], with Annotated imported from typing. A pattern on list[str] does not automatically constrain every item.
What breaks
Diagnostic: regex is removed. use pattern instead
Field raises PydanticUserError with code removed-kwargs when regex has a non-None value. See the Field implementation. This is a schema-definition problem, not invalid request data. Renaming the keyword may reveal a separate SchemaError if the expression uses features the Rust engine cannot compile. A valid model rejecting a value instead reports string_pattern_mismatch, as shown in the official config example.
What to test
- Import every affected model before serving requests; this catches schema construction failures.
- Compare accepted and rejected values across the migration, including empty strings, Unicode, newlines, partial matches and any flags. Use explicit boundaries when the whole string must conform; see string constraints.
- For Python-only expressions, test lookaround success and failure, compiled flags and long hostile inputs. Do not infer equal performance from equal accepted values.
- Exercise every list element, generated JSON Schema and the consuming API. A backend's Python regex behavior is not a guarantee that another schema consumer supports the same expression.
Pitfalls
- Removing the constraint just to make imports succeed silently broadens accepted input.
- Setting
regex_enginewhile leavingregex=still triggers the removed-keyword error. - Applying a model-wide engine switch without reviewing other patterns can change their behavior.
- Assuming a compiled
re.Patternstill uses Rust ignores the documented override.
Community evidence
Tamerlan-hash's issue #7058 shows a password constraint failing on lookaround during schema construction. ning0818's issue #9729 independently shows the same engine limitation while a FastAPI route is being built. These are concrete failure reports, not proof that every surrounding framework forwards model configuration identically.
A prompt to give your agent
Your training data may predate this API. Read <https://docs.pydantic.dev/latest/migration/> and <https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.regex_engine> before editing; do not guess from older snippets. Inspect the installed pydantic/core pair. Replace only regex constraints, identify unsupported expressions, and propose the narrowest documented engine setting. Preserve validation behavior with positive and negative cases, including item constraints and generated schema consumers. Show the diff and test results; label unverified alternatives Untested experiment.
Limits
Checked 2026-10-28. No models, migrations, performance checks or framework integration tests were run. Community evidence establishes the failure mode, not a universal FastAPI fix. The release pair above is a documented reference, not a requirement to upgrade every application. Other schema consumers may reject Python-only patterns.
Started from a post by @Tamerlan-hash, packaged by the editor. Original post
- 01 · YouPaste the line from Try it into your agent.
- 02 · Your agentGets the package and a trial ticket, installs it on a new branch, runs its check.
- 03 · YouApprove a one-line report. Nothing is posted without you.