{"record":{"id":"0bff9ffc7e517e35","repo":"PrefectHQ/fastmcp","slug":"pattern-pattern-r-is-not-supported-by-pydantic-s","errorCode":null,"errorMessage":"Pattern {pattern!r} is not supported by Pydantic's regex engine and will not be enforced.","messagePattern":"Pattern (.+?) is not supported by Pydantic's regex engine and will not be enforced\\.","errorType":"console","errorClass":"UserWarning","httpStatus":null,"severity":"warning","filePath":"fastmcp_slim/fastmcp/utilities/json_schema_type.py","lineNumber":301,"sourceCode":"            \"max_length\": schema.get(\"maxLength\"),\n            \"pattern\": schema.get(\"pattern\"),\n        }.items()\n        if v is not None\n    }\n\n    if not constraints:\n        return str\n\n    annotated: Any = Annotated[str, StringConstraints(**constraints)]\n\n    if \"pattern\" in constraints:\n        try:\n            TypeAdapter(annotated)\n        except _PydanticSchemaError as exc:\n            if \"regex\" not in str(exc).lower():\n                raise\n            pattern = constraints.pop(\"pattern\")\n            warnings.warn(\n                f\"Pattern {pattern!r} is not supported by Pydantic's regex engine \"\n                f\"and will not be enforced.\",\n                UserWarning,\n                stacklevel=2,\n            )\n            pattern_field = Field(json_schema_extra={\"x-unsupported-pattern\": pattern})\n            if constraints:\n                annotated = Annotated[\n                    str, StringConstraints(**constraints), pattern_field\n                ]  # type: ignore[valid-type]\n            else:\n                annotated = Annotated[str, pattern_field]  # type: ignore[valid-type]\n\n    return annotated\n\n\ndef _create_numeric_type(\n    base: type[int | float], schema: Mapping[str, Any]","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/utilities/json_schema_type.py#L283-L319","documentation":"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.","triggerScenarios":"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.","commonSituations":"Schemas authored for JavaScript/ECMAScript regex (lookahead/lookbehind) consumed by Python; hand-written patterns with unsupported syntax; cross-language schema reuse.","solutions":["Rewrite the pattern using Python `re`-compatible syntax so it compiles and is enforced.","If the pattern cannot be supported, validate it manually in your code and document the gap (the schema keeps `x-unsupported-pattern`).","Run with `-W error::UserWarning` in CI to catch unsupported patterns before production.","Normalize incoming external schemas (pre-strip unsupported regex features) before feeding them to the type converter."],"exampleFix":"// before\n{\"type\": \"string\", \"pattern\": \"(?<=@)example\\\\.com$\"}  # lookbehind unsupported\n// after\n{\"type\": \"string\", \"pattern\": \"^[^@]+@example\\\\.com$\"}  # rewrite without lookbehind","handlingStrategy":"validation","validationCode":"import re\nschema = {\"type\": \"string\", \"pattern\": p}\ntry:\n    re.compile(p)\nexcept re.error:\n    # rewrite or flag the pattern before schema conversion\n    raise ValueError(f\"pattern not compatible with Python re: {p}\")\n","typeGuard":null,"tryCatchPattern":"import warnings\nwith warnings.catch_warnings(record=True) as caught:\n    warnings.simplefilter(\"always\")\n    typ = build_type_from_schema(schema)\nif any(\"not supported by Pydantic's regex engine\" in str(w.message) for w in caught):\n    logging.warning(\"pattern dropped from schema; validate manually\")","preventionTips":["Pre-compile patterns with Python re before publishing schemas","Avoid ECMAScript-only regex features (lookbehind, named group quirks)","Inspect generated JSON schema for x-unsupported-pattern markers in CI"],"tags":["python","json-schema","regex","pydantic","validation"],"backgroundTag":"unsupported-regex-pattern","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}