Skip to content

Conversation

@dsfaccini
Copy link
Contributor

FilePart.has_content() will always return True, even if it's an empty file.

@dataclass(repr=False)
class FilePart:
    """A file response from a model."""

    content: Annotated[BinaryContent, pydantic.AfterValidator(BinaryImage.narrow_type)]
    """The file content of the response."""

    ...

    def has_content(self) -> bool:
        """Return `True` if the file content is non-empty."""
        # `self.content` is a `BinaryContent` object
        return bool(self.content)  # pragma: no cover

check using

from pydantic_ai.messages import BinaryContent, FilePart

empty_filepart = FilePart(
    content=BinaryContent(data=b'', media_type='image/jpeg')
)
nonempty_filepart = FilePart(
    content=BinaryContent(data=b'abc', media_type='image/jpeg')
)

print(f'{empty_filepart.has_content()=}')        # BUG: True
print(f'{nonempty_filepart.has_content()=}')  # True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant