{"id":"0caeb30052bb3769","repo":"pydantic/pydantic","slug":"ip-any-interface","errorCode":"ip_any_interface","errorMessage":"value is not a valid IPv4 or IPv6 interface","messagePattern":"value is not a valid IPv4 or IPv6 interface","errorType":"validation","errorClass":"PydanticCustomError","httpStatus":null,"severity":"error","filePath":"pydantic/networks.py","lineNumber":1240,"sourceCode":"        def _validate(cls, input_value: Any, /) -> IPvAnyAddressType:\n            return cls(input_value)  # type: ignore[return-value]\n\n    class IPvAnyInterface:\n        \"\"\"Validate an IPv4 or IPv6 interface.\"\"\"\n\n        __slots__ = ()\n\n        def __new__(cls, value: NetworkType) -> IPvAnyInterfaceType:\n            \"\"\"Validate an IPv4 or IPv6 interface.\"\"\"\n            try:\n                return IPv4Interface(value)\n            except ValueError:\n                pass\n\n            try:\n                return IPv6Interface(value)\n            except ValueError:\n                raise PydanticCustomError('ip_any_interface', 'value is not a valid IPv4 or IPv6 interface')\n\n        @classmethod\n        def __get_pydantic_json_schema__(\n            cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler\n        ) -> JsonSchemaValue:\n            field_schema = {}\n            field_schema.update(type='string', format='ipvanyinterface')\n            return field_schema\n\n        @classmethod\n        def __get_pydantic_core_schema__(\n            cls,\n            _source: type[Any],\n            _handler: GetCoreSchemaHandler,\n        ) -> core_schema.CoreSchema:\n            return core_schema.no_info_plain_validator_function(\n                cls._validate, serialization=core_schema.to_string_ser_schema()\n            )","sourceCodeStart":1222,"sourceCodeEnd":1258,"githubUrl":"https://github.com/pydantic/pydantic/blob/2e5f0e2b4218de31709f1cf9c5bc61ea97a68835/pydantic/networks.py#L1222-L1258","documentation":"IPvAnyInterface tries IPv4Interface(value) then IPv6Interface(value); on both failing it raises PydanticCustomError code 'ip_any_interface'. An 'interface' is an address plus a prefix length (e.g. 192.168.1.1/24), identifying a host on a subnet.","triggerScenarios":"Passing a plain address without prefix ('192.168.1.1'), a hostname, a URL, or a malformed CIDR to a field typed IPvAnyInterface.","commonSituations":"Confusing interface notation with a bare address or a network; omitting the /prefix; user input that is a hostname rather than an IP+mask.","solutions":["Provide address plus prefix: '192.168.1.1/24' or '2001:db8::1/64'.","If only a bare address is expected, switch the field to IPvAnyAddress.","If you want the network only, use IPvAnyNetwork."],"exampleFix":"# before\nclass M(BaseModel):\n    iface: IPvAnyInterface\nM(iface='10.0.0.1')\n# after\nM(iface='10.0.0.1/24')","handlingStrategy":"validation","validationCode":"import ipaddress\ndef parse_ip_any_interface(value):\n    try:\n        return ipaddress.ip_interface(value)\n    except ValueError:\n        raise ValueError(f'{value!r} is not a valid IPv4 or IPv6 interface')","typeGuard":"import ipaddress\ndef is_ip_interface(value) -> bool:\n    try:\n        ipaddress.ip_interface(value)\n        return True\n    except ValueError:\n        return False","tryCatchPattern":"from pydantic import ValidationError\ntry:\n    M(iface=user_input)\nexcept ValidationError as e:\n    if any(err['type'] == 'ip_any_interface' for err in e.errors()):\n        ...","preventionTips":["Provide address plus prefix (e.g. 10.0.0.1/24) for interface fields.","Use ipaddress.ip_interface() to pre-validate and produce clear errors.","Choose IPvAnyAddress vs IPvAnyInterface vs IPvAnyNetwork deliberately."],"tags":["ip","networks","validation","ipv4","ipv6","interface"],"analyzedSha":"2e5f0e2b4218de31709f1cf9c5bc61ea97a68835","analyzedAt":"2026-08-04T19:54:21.281Z","schemaVersion":2}