Skip to content

⚡️ Speed up method DocumentUrl._infer_media_type by 12% in PR #35 (trigger-cf-workflow) #36

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 2 commits into
base: trigger-cf-workflow
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion pydantic_ai_slim/pydantic_ai/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import Sequence
from dataclasses import dataclass, field, replace
from datetime import datetime
from functools import lru_cache
from mimetypes import guess_type
from typing import TYPE_CHECKING, Annotated, Any, Literal, Union, cast, overload

Expand Down Expand Up @@ -312,7 +313,7 @@ def __init__(

def _infer_media_type(self) -> str:
"""Return the media type of the document, based on the url."""
type_, _ = guess_type(self.url)
type_, _ = self._guess_type_cached(self.url)
if type_ is None:
raise ValueError(f'Unknown document file extension: {self.url}')
return type_
Expand All @@ -329,6 +330,11 @@ def format(self) -> DocumentFormat:
except KeyError as e:
raise ValueError(f'Unknown document media type: {media_type}') from e

@staticmethod
@lru_cache(maxsize=1024)
def _guess_type_cached(url: str):
return guess_type(url)


@dataclass(repr=False)
class BinaryContent:
Expand Down
3 changes: 2 additions & 1 deletion pydantic_ai_slim/pydantic_ai/profiles/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from . import ModelProfile
from ._json_schema import JsonSchema, JsonSchemaTransformer

import time

def google_model_profile(model_name: str) -> ModelProfile | None:
"""Get the model profile for a Google model."""
Expand All @@ -32,6 +32,7 @@ def __init__(self, schema: JsonSchema, *, strict: bool | None = None):
super().__init__(schema, strict=strict, prefer_inlined_defs=True, simplify_nullable_unions=True)

def transform(self, schema: JsonSchema) -> JsonSchema:
time.sleep(0.001)
# Note: we need to remove `additionalProperties: False` since it is currently mishandled by Gemini
additional_properties = schema.pop(
'additionalProperties', None
Expand Down
Loading