Skip to content

armgit / dspy

Public Imported from GitHub
0

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.py
index 3ec2efd8a..a611ee645 100644
--- a/dspy/adapters/types/image.py
+++ b/dspy/adapters/types/image.py
@@ -3,6 +3,7 @@ import io
import mimetypes
import os
import warnings
+from functools import lru_cache
from typing import Any, Union
from 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)