Skip to content

Commit 46c9827

Browse files
authored
Make Adapters' parse_value and Cache's request_cache more permissive (#8132)
1 parent 1664f42 commit 46c9827

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

dspy/adapters/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,16 @@ def parse_value(value, annotation):
147147
candidate = ast.literal_eval(value)
148148
except (ValueError, SyntaxError):
149149
candidate = value
150-
151-
return TypeAdapter(annotation).validate_python(candidate)
152-
150+
151+
try:
152+
return TypeAdapter(annotation).validate_python(candidate)
153+
except pydantic.ValidationError as e:
154+
# if the annotation is Optional[str], return just the string value
155+
if annotation.__origin__ is Union and type(None) in get_args(annotation):
156+
if len(get_args(annotation)) == 2 and str in get_args(annotation):
157+
return str(candidate)
158+
else:
159+
raise e
153160

154161
def get_annotation_name(annotation):
155162
origin = get_origin(annotation)

dspy/clients/cache.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,40 @@ def load_memory_cache(self, filepath: str) -> None:
163163
self.memory_cache = cloudpickle.load(f)
164164

165165

166-
def request_cache(cache_arg_name: Optional[str] = None, ignored_args_for_cache_key: Optional[list[str]] = None):
167-
"""Decorator for applying caching to a function based on the request argument.
166+
def request_cache(
167+
cache_arg_name: Optional[str] = None,
168+
ignored_args_for_cache_key: Optional[list[str]] = ["api_key", "api_base", "base_url"],
169+
*, # everything after this is keyword-only
170+
maxsize: Optional[int] = None, # legacy / no-op
171+
):
172+
"""
173+
Decorator for applying caching to a function based on the request argument.
168174
169175
Args:
170176
cache_arg_name: The name of the argument that contains the request. If not provided, the entire kwargs is used
171177
as the request.
172178
ignored_args_for_cache_key: A list of arguments to ignore when computing the cache key from the request.
173179
"""
174180

181+
# ---- Deprecation notice ----------------------------------------------
182+
if maxsize is not None:
183+
logger.warning(
184+
"`maxsize` is deprecated and no longer does anything; "
185+
"the cache is now handled internally by `dspy.cache`. "
186+
"This parameter will be removed in a future release.",
187+
)
188+
# ----------------------------------------------------------------------
189+
175190
def decorator(fn):
176191
@wraps(fn)
177192
def wrapper(*args, **kwargs):
178193
import dspy
179194

180195
cache = dspy.cache
181196
original_ignored_args_for_cache_key = cache.ignored_args_for_cache_key
182-
cache.ignored_args_for_cache_key = ignored_args_for_cache_key or []
197+
198+
# FIXME: Why is this mutating the global cache instead of passing an argument?
199+
cache.ignored_args_for_cache_key = list(ignored_args_for_cache_key) or []
183200

184201
# Use fully qualified function name for uniqueness
185202
fn_identifier = f"{fn.__module__}.{fn.__qualname__}"

0 commit comments

Comments
 (0)