Pydantic URL String Types
raw JSON → 1.0.2 verified Sat May 09 auth: no python
Pydantic URL types based on str class for fast and simple URL validation. Current version 1.0.2, release cadence is low (semi-active maintenance). Provides Pydantic field types like HttpUrl, FtpUrl, etc., but unlike pydantic's own UrlStr, these are simple str subclasses that do not parse URL components.
pip install pydantic-string-url Common errors
error ImportError: cannot import name 'HttpUrl' from 'pydantic' ↓
cause User imported HttpUrl from pydantic instead of pydantic_string_url. pydantic's HttpUrl exists but is different.
fix
Use 'from pydantic_string_url import HttpUrl' for this library's version. Or use 'from pydantic import HttpUrl' for pydantic's parsed URL type.
error pydantic.error_wrappers.ValidationError: 1 validation error for MyModel website invalid or missing URL (type=value_error.url) ↓
cause The URL string does not match the expected pattern (e.g., missing scheme or invalid format).
fix
Ensure URL starts with http://, https://, ftp://, etc. and conforms to standard URL format.
Warnings
breaking Types in this library are str subclasses, not UrlStr; use str() to get plain string if you need to disable subclass behavior. ↓
fix Use str(your_url_field) to get a plain string, or use .__str__() if needed for serialization.
gotcha This library's HttpUrl does NOT parse the URL into components like scheme, host, port. It only validates format. Use pydantic's HttpUrl if you need parsed access. ↓
fix If you need URL parsing (e.g., url.host), use from pydantic import HttpUrl instead of pydantic_string_url.
Imports
- HttpUrl wrong
from pydantic import HttpUrlcorrectfrom pydantic_string_url import HttpUrl - FtpUrl
from pydantic_string_url import FtpUrl
Quickstart
from pydantic import BaseModel
from pydantic_string_url import HttpUrl, FtpUrl
class MyModel(BaseModel):
website: HttpUrl
ftp: FtpUrl
# Valid
m = MyModel(website='https://example.com', ftp='ftp://files.example.com')
print(m.website) # 'https://example.com'
# Invalid - raises ValidationError
# m = MyModel(website='not-a-url', ftp='ftp://example.com')