PrefectHQ/fastmcp · warning · UserWarning

Pattern {pattern!r} is not supported by Pydantic's regex eng

Error message

Pattern {pattern!r} is not supported by Pydantic's regex engine and will not be enforced.

What it means

When building dynamic types from JSON Schema, a string `pattern` constraint is applied via Pydantic. If Pydantic's regex engine cannot compile the pattern, FastMCP removes the constraint (so it is NOT enforced) and emits a `UserWarning`, attaching the original pattern as `x-unsupported-pattern` in the JSON schema.

Source

Thrown at fastmcp_slim/fastmcp/utilities/json_schema_type.py:301

            "max_length": schema.get("maxLength"),
            "pattern": schema.get("pattern"),
        }.items()
        if v is not None
    }

    if not constraints:
        return str

    annotated: Any = Annotated[str, StringConstraints(**constraints)]

    if "pattern" in constraints:
        try:
            TypeAdapter(annotated)
        except _PydanticSchemaError as exc:
            if "regex" not in str(exc).lower():
                raise
            pattern = constraints.pop("pattern")
            warnings.warn(
                f"Pattern {pattern!r} is not supported by Pydantic's regex engine "
                f"and will not be enforced.",
                UserWarning,
                stacklevel=2,
            )
            pattern_field = Field(json_schema_extra={"x-unsupported-pattern": pattern})
            if constraints:
                annotated = Annotated[
                    str, StringConstraints(**constraints), pattern_field
                ]  # type: ignore[valid-type]
            else:
                annotated = Annotated[str, pattern_field]  # type: ignore[valid-type]

    return annotated


def _create_numeric_type(
    base: type[int | float], schema: Mapping[str, Any]

View on GitHub (pinned to 1f02114297)

Solutions

  1. Rewrite the pattern using Python `re`-compatible syntax so it compiles and is enforced.
  2. If the pattern cannot be supported, validate it manually in your code and document the gap (the schema keeps `x-unsupported-pattern`).
  3. Run with `-W error::UserWarning` in CI to catch unsupported patterns before production.
  4. Normalize incoming external schemas (pre-strip unsupported regex features) before feeding them to the type converter.

Example fix

// before
{"type": "string", "pattern": "(?<=@)example\\.com$"}  # lookbehind unsupported
// after
{"type": "string", "pattern": "^[^@]+@example\\.com$"}  # rewrite without lookbehind
Defensive patterns

Strategy: validation

Validate before calling

import re
schema = {"type": "string", "pattern": p}
try:
    re.compile(p)
except re.error:
    # rewrite or flag the pattern before schema conversion
    raise ValueError(f"pattern not compatible with Python re: {p}")

Try / catch

import warnings
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    typ = build_type_from_schema(schema)
if any("not supported by Pydantic's regex engine" in str(w.message) for w in caught):
    logging.warning("pattern dropped from schema; validate manually")

Prevention

When it happens

Trigger: Schemas containing regex dialects Pydantic can't compile (e.g. lookbehinds on engines lacking them, invalid escapes) flowing into `_create_string_type`; conversion of an external OpenAPI/JSON Schema with exotic patterns.

Common situations: Schemas authored for JavaScript/ECMAScript regex (lookahead/lookbehind) consumed by Python; hand-written patterns with unsupported syntax; cross-language schema reuse.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/0bff9ffc7e517e35. Report an issue: GitHub.