Skip to content

Conversation

@dsfaccini
Copy link
Owner

@dsfaccini dsfaccini commented Nov 14, 2025

In pydantic_ai_slim/pydantic_ai/messages.py, FilePart.has_content() will return True always, 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."""
        return bool(self.content)  # pragma: no cover

checked 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.

4 participants