Repository: pydantic/pydantic-ai
File: pydantic_ai_slim/pydantic_ai/messages.py (Python)

Code before:
    _media_type: str | None = field(default=None, repr=False)
    """Optional override for the media type of the file, in case it cannot be inferred from the URL."""

    force_download: bool = False
    """If the model supports it:

    * If True, the file is downloaded and the data is sent to the model as bytes.
    * If False, the URL is sent directly to the model and no download is performed.
    """

    vendor_metadata: dict[str, Any] | None = None
    """Vendor-specific metadata for the file.

    Supported by:
    - `GoogleModel`: `VideoUrl.vendor_metadata` is used as `video_metadata`: https://ai.google.dev/gemini-api/docs/video-understanding#customize-video-processing
    """

    @abstractmethod
    def _infer_media_type_from_url(self) -> str:
        """Return the media type of the file, based on the url."""

    @property
    def media_type(self) -> str:
        """Return the media type of the file, based on the url or the provided `_media_type`."""
        if self._media_type is not None:
            return self._media_type
        else:
            return self._infer_media_type_from_url()

    @property
    @abstractmethod
    def format(self) -> str:
        """The file format."""

    __repr__ = _utils.dataclasses_no_defaults_repr


@dataclass(repr=False)
class VideoUrl(FileUrl):
    """A URL to a video."""

    url: str
    """The URL of the video."""

    kind: Literal['video-url'] = 'video-url'
    """Type identifier, this is available on all parts as a discriminator."""

    def _infer_media_type_from_url(self) -> VideoMediaType:
        """Return the media type of the video, based on the url."""
        if self.url.endswith('.mkv'):
            return 'video/x-matroska'

Review comment (suggestion):
Note that there is no check that _media_type conforms to the string literals (e.g. `VideoMediaType`) in the subclasses.

Code after:
    force_download: bool = False
    """If the model supports it:

    * If True, the file is downloaded and the data is sent to the model as bytes.
    * If False, the URL is sent directly to the model and no download is performed.
    """

    vendor_metadata: dict[str, Any] | None = None
    """Vendor-specific metadata for the file.

    Supported by:
    - `GoogleModel`: `VideoUrl.vendor_metadata` is used as `video_metadata`: https://ai.google.dev/gemini-api/docs/video-understanding#customize-video-processing
    """

    _media_type: str | None = field(init=False, repr=False)

    def __init__(
        self,
        url: str,
        force_download: bool = False,
        vendor_metadata: dict[str, Any] | None = None,
        media_type: str | None = None,
    ) -> None:
        self.url = url
        self.vendor_metadata = vendor_metadata
        self.force_download = force_download
        self._media_type = media_type

    @abstractmethod
    def _infer_media_type(self) -> str:
        """Return the media type of the file, based on the url."""

    @property
    def media_type(self) -> str:
        """Return the media type of the file, based on the url or the provided `_media_type`."""
        return self._media_type or self._infer_media_type()

    @property
    @abstractmethod
    def format(self) -> str:
        """The file format."""

    __repr__ = _utils.dataclasses_no_defaults_repr


@dataclass(init=False, repr=False)
class VideoUrl(FileUrl):
    """A URL to a video."""

    url: str
    """The URL of the video."""