DSPy: The framework for programming—not prompting—language models
Python
Cache Image.format for better throughput (#8842)
TomuHirata50185ca97687
diff --git a/dspy/adapters/types/image.py b/dspy/adapters/types/image.pyindex 3ec2efd8a..a611ee645 100644--- a/dspy/adapters/types/image.py+++ b/dspy/adapters/types/image.py@@ -3,6 +3,7 @@ import ioimport mimetypesimport osimport warnings+from functools import lru_cachefrom typing import Any, Unionfrom urllib.parse import urlparse@@ -65,6 +66,7 @@ class Image(Type):# Delegate the rest of initialization to pydantic's BaseModel.super().__init__(**data)+ @lru_cache(maxsize=32)def format(self) -> list[dict[str, Any]] | str:try:image_url = encode_image(self.url)