Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Make Adapters' parse_value and Cache's request_cache more permissive (#8132)

Omar Khattab46c982702f1a
diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.py
index 4a3a48011..3f8f00fad 100644
--- a/dspy/adapters/utils.py
+++ b/dspy/adapters/utils.py
@@ -147,9 +147,16 @@ def parse_value(value, annotation):
candidate = ast.literal_eval(value)
except (ValueError, SyntaxError):
candidate = value
-
- return TypeAdapter(annotation).validate_python(candidate)
-
+
+ try:
+ return TypeAdapter(annotation).validate_python(candidate)
+ except pydantic.ValidationError as e:
+ # if the annotation is Optional[str], return just the string value
+ if annotation.__origin__ is Union and type(None) in get_args(annotation):
+ if len(get_args(annotation)) == 2 and str in get_args(annotation):
+ return str(candidate)
+ else:
+ raise e
def get_annotation_name(annotation):
origin = get_origin(annotation)
diff --git a/dspy/clients/cache.py b/dspy/clients/cache.py
index c73d986ff..6ac81240e 100644
--- a/dspy/clients/cache.py
+++ b/dspy/clients/cache.py
@@ -163,8 +163,14 @@ class Cache:
self.memory_cache = cloudpickle.load(f)
-def request_cache(cache_arg_name: Optional[str] = None, ignored_args_for_cache_key: Optional[list[str]] = None):
- """Decorator for applying caching to a function based on the request argument.
+def request_cache(
+ cache_arg_name: Optional[str] = None,
+ ignored_args_for_cache_key: Optional[list[str]] = ["api_key", "api_base", "base_url"],
+ *, # everything after this is keyword-only
+ maxsize: Optional[int] = None, # legacy / no-op
+):
+ """
+ Decorator for applying caching to a function based on the request argument.
Args:
cache_arg_name: The name of the argument that contains the request. If not provided, the entire kwargs is used
@@ -172,6 +178,15 @@ def request_cache(cache_arg_name: Optional[str] = None, ignored_args_for_cache_k
ignored_args_for_cache_key: A list of arguments to ignore when computing the cache key from the request.
"""
+ # ---- Deprecation notice ----------------------------------------------
+ if maxsize is not None:
+ logger.warning(
+ "`maxsize` is deprecated and no longer does anything; "
+ "the cache is now handled internally by `dspy.cache`. "
+ "This parameter will be removed in a future release.",
+ )
+ # ----------------------------------------------------------------------
+
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
@@ -179,7 +194,9 @@ def request_cache(cache_arg_name: Optional[str] = None, ignored_args_for_cache_k
cache = dspy.cache
original_ignored_args_for_cache_key = cache.ignored_args_for_cache_key
- cache.ignored_args_for_cache_key = ignored_args_for_cache_key or []
+
+ # FIXME: Why is this mutating the global cache instead of passing an argument?
+ cache.ignored_args_for_cache_key = list(ignored_args_for_cache_key) or []
# Use fully qualified function name for uniqueness
fn_identifier = f"{fn.__module__}.{fn.__qualname__}"