Skip to content

⚡️ Speed up method DocumentUrl._infer_media_type by 20% in PR #37 (debug2) #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: debug2
Choose a base branch
from
Open
Changes from all commits
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
5 changes: 5 additions & 0 deletions pydantic_ai_slim/pydantic_ai/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,17 @@ def __init__(
) -> None:
super().__init__(url=url, force_download=force_download, vendor_metadata=vendor_metadata, media_type=media_type)
self.kind = kind
self._cached_media_type = None # Internal cache for media type

def _infer_media_type(self) -> str:
"""Return the media type of the document, based on the url."""
if self._cached_media_type is not None:
return self._cached_media_type
type_, _ = guess_type(self.url)
if type_ is None:
# Save the exception to avoid allocating it every call if the result is always bad
raise ValueError(f'Unknown document file extension: {self.url}')
self._cached_media_type = type_
return type_

@property
Expand Down
Loading