Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
PR suggestions centering on readability
  • Loading branch information
jamesbraza committed Feb 27, 2026
commit 9cccb006beda293cb46a8e98b649164de5b324f2
19 changes: 10 additions & 9 deletions src/paperqa/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ class ParsedMedia(BaseModel):
@model_validator(mode="after")
def _check_data_xor_url(self) -> Self:
if not self.data and not self.url:
Comment thread
jamesbraza marked this conversation as resolved.
raise ValueError("At least one of 'data' (non-empty) or 'url' is required.")
raise ValueError("Setting one of 'data' (non-empty) or 'url' is required.")
if self.data and self.url:
raise ValueError(
"Set 'data' or 'url', not both. Having both creates ambiguous"
Expand Down Expand Up @@ -657,22 +657,23 @@ def __eq__(self, other) -> bool:
# We only compare bytes or URLs, we consider mixes incompatible.
# If you need compatibility, resolve the URL to bytes or generate a URL
return False
common = (
self.index == other.index
and self.text == other.text
and self._get_info_hashable() == other._get_info_hashable()
)
if (
self.index != other.index
or self.text != other.text
or self._get_info_hashable() != other._get_info_hashable()
):
return False
if self.data:
return common and self.data == other.data
return common and self.url == other.url
return self.data == other.data
return self.url == other.url

def to_image_url(self) -> str:
"""Get a URL suitable for LLM image content.

Returns a signed/public HTTP URL when available, otherwise falls back
to an RFC 2397 base64 data URL built from the raw bytes.
"""
if self.url and not self.data:
if self.url:
return self.url
image_type = cast(str, self.info.get("suffix", "png")).removeprefix(".")
if image_type == "jpg": # SEE: https://stackoverflow.com/a/54488403
Expand Down
4 changes: 2 additions & 2 deletions tests/test_paperqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1914,10 +1914,10 @@ def test_media_to_image_url(subtests: SubTests) -> None:


def test_parsed_media_data_or_url() -> None:
with pytest.raises(ValidationError, match="At least one of"):
with pytest.raises(ValidationError, match="one of"):
ParsedMedia(index=0, data=b"")

with pytest.raises(ValidationError, match="At least one of"):
with pytest.raises(ValidationError, match="one of"):
ParsedMedia(index=0)

with pytest.raises(ValidationError, match="not both"):
Expand Down