Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Drop Python 3.9 support (#8456)

TomuHirata1697f15a7b7a
diff --git a/.github/workflows/precommits_check.yml b/.github/workflows/precommits_check.yml
index 20c9c8368..4bc380816 100644
--- a/.github/workflows/precommits_check.yml
+++ b/.github/workflows/precommits_check.yml
@@ -10,7 +10,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
- python-version: "3.9"
+ python-version: "3.10"
cache: "pip"
- name: Check Pull Request Title
uses: Slashgear/action-check-pr-title@main
diff --git a/dspy/adapters/base.py b/dspy/adapters/base.py
index a8cced882..1cdc1edfd 100644
--- a/dspy/adapters/base.py
+++ b/dspy/adapters/base.py
@@ -1,5 +1,5 @@
import logging
-from typing import TYPE_CHECKING, Any, Optional, Type, get_origin
+from typing import TYPE_CHECKING, Any, Type, get_origin
import json_repair
import litellm
@@ -17,7 +17,7 @@ if TYPE_CHECKING:
class Adapter:
- def __init__(self, callbacks: Optional[list[BaseCallback]] = None):
+ def __init__(self, callbacks: list[BaseCallback] | None = None):
self.callbacks = callbacks or []
def __init_subclass__(cls, **kwargs) -> None:
@@ -281,7 +281,7 @@ class Adapter:
self,
signature: Type[Signature],
outputs: dict[str, Any],
- missing_field_message: Optional[str] = None,
+ missing_field_message: str | None = None,
) -> str:
"""Format the assistant message content.
diff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.py
index 7e7859978..ac407e475 100644
--- a/dspy/adapters/chat_adapter.py
+++ b/dspy/adapters/chat_adapter.py
@@ -1,6 +1,6 @@
import re
import textwrap
-from typing import Any, Dict, NamedTuple, Optional, Type
+from typing import Any, Dict, NamedTuple, Type
from litellm import ContextWindowExceededError
from pydantic.fields import FieldInfo
@@ -27,7 +27,7 @@ class FieldInfoWithName(NamedTuple):
class ChatAdapter(Adapter):
- def __init__(self, callbacks: Optional[list[BaseCallback]] = None):
+ def __init__(self, callbacks: list[BaseCallback] | None = None):
super().__init__(callbacks)
def __call__(
diff --git a/dspy/adapters/two_step_adapter.py b/dspy/adapters/two_step_adapter.py
index 64f1b80ed..405cc99e2 100644
--- a/dspy/adapters/two_step_adapter.py
+++ b/dspy/adapters/two_step_adapter.py
@@ -1,4 +1,4 @@
-from typing import Any, Optional, Type
+from typing import Any, Type
import json_repair
@@ -193,7 +193,7 @@ class TwoStepAdapter(Adapter):
self,
signature: Type[Signature],
outputs: dict[str, Any],
- missing_field_message: Optional[str] = None,
+ missing_field_message: str | None = None,
) -> str:
parts = []
diff --git a/dspy/adapters/types/base_type.py b/dspy/adapters/types/base_type.py
index eece3469e..4ac90970d 100644
--- a/dspy/adapters/types/base_type.py
+++ b/dspy/adapters/types/base_type.py
@@ -1,6 +1,6 @@
import json
import re
-from typing import Any, Union, get_args, get_origin
+from typing import Any, get_args, get_origin
import json_repair
import pydantic
@@ -26,7 +26,7 @@ class BaseType(pydantic.BaseModel):
```
"""
- def format(self) -> Union[list[dict[str, Any]], str]:
+ def format(self) -> list[dict[str, Any]] | str:
raise NotImplementedError
@classmethod
diff --git a/dspy/adapters/types/image.py b/dspy/adapters/types/image.py
index 693b407fb..e8b44ffe3 100644
--- a/dspy/adapters/types/image.py
+++ b/dspy/adapters/types/image.py
@@ -28,7 +28,7 @@ class Image(BaseType):
"extra": "forbid",
}
- def format(self) -> Union[list[dict[str, Any]], str]:
+ def format(self) -> list[dict[str, Any]] | str:
try:
image_url = encode_image(self.url)
except Exception as e:
diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.py
index 6bddbee9a..c2ca4990c 100644
--- a/dspy/adapters/types/tool.py
+++ b/dspy/adapters/types/tool.py
@@ -1,6 +1,6 @@
import asyncio
import inspect
-from typing import TYPE_CHECKING, Any, Callable, Optional, Tuple, Type, get_origin, get_type_hints
+from typing import TYPE_CHECKING, Any, Callable, Tuple, Type, get_origin, get_type_hints
from jsonschema import ValidationError, validate
from pydantic import BaseModel, TypeAdapter, create_model
@@ -24,21 +24,21 @@ class Tool(BaseType):
"""
func: Callable
- name: Optional[str] = None
- desc: Optional[str] = None
- args: Optional[dict[str, Any]] = None
- arg_types: Optional[dict[str, Any]] = None
- arg_desc: Optional[dict[str, str]] = None
+ name: str | None = None
+ desc: str | None = None
+ args: dict[str, Any] | None = None
+ arg_types: dict[str, Any] | None = None
+ arg_desc: dict[str, str] | None = None
has_kwargs: bool = False
def __init__(
self,
func: Callable,
- name: Optional[str] = None,
- desc: Optional[str] = None,
- args: Optional[dict[str, Any]] = None,
- arg_types: Optional[dict[str, Any]] = None,
- arg_desc: Optional[dict[str, str]] = None,
+ name: str | None = None,
+ desc: str | None = None,
+ args: dict[str, Any] | None = None,
+ arg_types: dict[str, Any] | None = None,
+ arg_desc: dict[str, str] | None = None,
):
"""Initialize the Tool class.
@@ -71,7 +71,7 @@ class Tool(BaseType):
super().__init__(func=func, name=name, desc=desc, args=args, arg_types=arg_types, arg_desc=arg_desc)
self._parse_function(func, arg_desc)
- def _parse_function(self, func: Callable, arg_desc: Optional[dict[str, str]] = None):
+ def _parse_function(self, func: Callable, arg_desc: dict[str, str] | None = None):
"""Helper method that parses a function to extract the name, description, and args.
This is a helper function that automatically infers the name, description, and args of the tool from the
diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.py
index 64c98cc68..6632d325c 100644
--- a/dspy/adapters/utils.py
+++ b/dspy/adapters/utils.py
@@ -32,7 +32,7 @@ def serialize_for_json(value: Any) -> Any:
return str(value)
-def format_field_value(field_info: FieldInfo, value: Any, assume_text=True) -> Union[str, dict]:
+def format_field_value(field_info: FieldInfo, value: Any, assume_text=True) -> str | dict:
"""
Formats the value of the specified field according to the field's DSPy type (input or output),
annotation (e.g. str, int, etc.), and the type of the value itself.
diff --git a/dspy/adapters/xml_adapter.py b/dspy/adapters/xml_adapter.py
index 8ed0da27f..b75dfe90b 100644
--- a/dspy/adapters/xml_adapter.py
+++ b/dspy/adapters/xml_adapter.py
@@ -1,5 +1,5 @@
import re
-from typing import Any, Dict, Optional, Type
+from typing import Any, Dict, Type
from dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithName
from dspy.adapters.utils import format_field_value
@@ -8,7 +8,7 @@ from dspy.utils.callback import BaseCallback
class XMLAdapter(ChatAdapter):
- def __init__(self, callbacks: Optional[list[BaseCallback]] = None):
+ def __init__(self, callbacks: list[BaseCallback] | None = None):
super().__init__(callbacks)
self.field_pattern = re.compile(r"<(?P<name>\w+)>((?P<content>.*?))</\1>", re.DOTALL)
diff --git a/dspy/clients/__init__.py b/dspy/clients/__init__.py
index 52be745ab..735691e9c 100644
--- a/dspy/clients/__init__.py
+++ b/dspy/clients/__init__.py
@@ -27,11 +27,11 @@ litellm.success_callback = [_litellm_track_cache_hit_callback]
def configure_cache(
- enable_disk_cache: Optional[bool] = True,
- enable_memory_cache: Optional[bool] = True,
- disk_cache_dir: Optional[str] = DISK_CACHE_DIR,
- disk_size_limit_bytes: Optional[int] = DISK_CACHE_LIMIT,
- memory_max_entries: Optional[int] = 1000000,
+ enable_disk_cache: bool | None = True,
+ enable_memory_cache: bool | None = True,
+ disk_cache_dir: str | None = DISK_CACHE_DIR,
+ disk_size_limit_bytes: int | None = DISK_CACHE_LIMIT,
+ memory_max_entries: int | None = 1000000,
enable_litellm_cache: bool = False,
):
"""Configure the cache for DSPy.
diff --git a/dspy/clients/cache.py b/dspy/clients/cache.py
index 20b507351..af3bc47c6 100644
--- a/dspy/clients/cache.py
+++ b/dspy/clients/cache.py
@@ -4,7 +4,7 @@ import logging
import threading
from functools import wraps
from hashlib import sha256
-from typing import Any, Dict, Optional
+from typing import Any, Dict
import cloudpickle
import pydantic
@@ -28,8 +28,8 @@ class Cache:
enable_disk_cache: bool,
enable_memory_cache: bool,
disk_cache_dir: str,
- disk_size_limit_bytes: Optional[int] = 1024 * 1024 * 10,
- memory_max_entries: Optional[int] = 1000000,
+ disk_size_limit_bytes: int | None = 1024 * 1024 * 10,
+ memory_max_entries: int | None = 1000000,
):
"""
Args:
@@ -62,7 +62,7 @@ class Cache:
"""Check if a key is in the cache."""
return key in self.memory_cache or key in self.disk_cache
- def cache_key(self, request: Dict[str, Any], ignored_args_for_cache_key: Optional[list[str]] = None) -> str:
+ def cache_key(self, request: Dict[str, Any], ignored_args_for_cache_key: list[str] | None = None) -> str:
"""
Obtain a unique cache key for the given request dictionary by hashing its JSON
representation. For request fields having types that are known to be JSON-incompatible,
@@ -95,7 +95,7 @@ class Cache:
params = {k: transform_value(v) for k, v in request.items() if k not in ignored_args_for_cache_key}
return sha256(ujson.dumps(params, sort_keys=True).encode()).hexdigest()
- def get(self, request: Dict[str, Any], ignored_args_for_cache_key: Optional[list[str]] = None) -> Any:
+ def get(self, request: Dict[str, Any], ignored_args_for_cache_key: list[str] | None = None) -> Any:
try:
key = self.cache_key(request, ignored_args_for_cache_key)
except Exception:
@@ -124,7 +124,7 @@ class Cache:
self,
request: Dict[str, Any],
value: Any,
- ignored_args_for_cache_key: Optional[list[str]] = None,
+ ignored_args_for_cache_key: list[str] | None = None,
enable_memory_cache: bool = True,
) -> None:
try:
@@ -169,11 +169,11 @@ class Cache:
def request_cache(
- cache_arg_name: Optional[str] = None,
- ignored_args_for_cache_key: Optional[list[str]] = None,
+ cache_arg_name: str | None = None,
+ ignored_args_for_cache_key: list[str] | None = None,
enable_memory_cache: bool = True,
*, # everything after this is keyword-only
- maxsize: Optional[int] = None, # legacy / no-op
+ maxsize: int | None = None, # legacy / no-op
):
"""
Decorator for applying caching to a function based on the request argument.
diff --git a/dspy/clients/databricks.py b/dspy/clients/databricks.py
index 427accfab..fea86fe5c 100644
--- a/dspy/clients/databricks.py
+++ b/dspy/clients/databricks.py
@@ -2,7 +2,7 @@ import logging
import os
import re
import time
-from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
+from typing import TYPE_CHECKING, Any, Dict, List
import requests
import ujson
@@ -50,9 +50,9 @@ class DatabricksProvider(Provider):
@staticmethod
def deploy_finetuned_model(
model: str,
- data_format: Optional[TrainDataFormat] = None,
- databricks_host: Optional[str] = None,
- databricks_token: Optional[str] = None,
+ data_format: TrainDataFormat | None = None,
+ databricks_host: str | None = None,
+ databricks_token: str | None = None,
deploy_timeout: int = 900,
):
workspace_client = _get_workspace_client()
@@ -169,8 +169,8 @@ class DatabricksProvider(Provider):
job: TrainingJobDatabricks,
model: str,
train_data: List[Dict[str, Any]],
- train_data_format: Optional[Union[TrainDataFormat, str]] = "chat",
- train_kwargs: Optional[Dict[str, Any]] = None,
+ train_data_format: TrainDataFormat | str | None = "chat",
+ train_kwargs: Dict[str, Any] | None = None,
) -> str:
if isinstance(train_data_format, str):
if train_data_format == "chat":
diff --git a/dspy/clients/embedding.py b/dspy/clients/embedding.py
index b7701997a..6016a372a 100644
--- a/dspy/clients/embedding.py
+++ b/dspy/clients/embedding.py
@@ -1,4 +1,4 @@
-from typing import Any, Callable, Optional, Union
+from typing import Any, Callable
import litellm
import numpy as np
@@ -76,7 +76,7 @@ class Embedder:
```
"""
- def __init__(self, model: Union[str, Callable], batch_size: int = 200, caching: bool = True, **kwargs: dict[str, Any]):
+ def __init__(self, model: str | Callable, batch_size: int = 200, caching: bool = True, **kwargs: dict[str, Any]):
self.model = model
self.batch_size = batch_size
self.caching = caching
@@ -110,7 +110,7 @@ class Embedder:
else:
return np.array(embeddings, dtype=np.float32)
- def __call__(self, inputs: Union[str, list[str]], batch_size: Optional[int] = None, caching: Optional[bool] = None, **kwargs: dict[str, Any]) -> np.ndarray:
+ def __call__(self, inputs: str | list[str], batch_size: int | None = None, caching: bool | None = None, **kwargs: dict[str, Any]) -> np.ndarray:
"""Compute embeddings for the given inputs.
Args:
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py
index 228d78b63..e79a73168 100644
--- a/dspy/clients/lm.py
+++ b/dspy/clients/lm.py
@@ -2,7 +2,7 @@ import logging
import os
import re
import threading
-from typing import Any, Dict, List, Literal, Optional, cast
+from typing import Any, Dict, List, Literal, cast
import litellm
from anyio.streams.memory import MemoryObjectSendStream
@@ -34,12 +34,12 @@ class LM(BaseLM):
max_tokens: int = 4000,
cache: bool = True,
cache_in_memory: bool = True,
- callbacks: Optional[List[BaseCallback]] = None,
+ callbacks: List[BaseCallback] | None = None,
num_retries: int = 3,
- provider: Optional[Provider] = None,
- finetuning_model: Optional[str] = None,
- launch_kwargs: Optional[dict[str, Any]] = None,
- train_kwargs: Optional[dict[str, Any]] = None,
+ provider: Provider | None = None,
+ finetuning_model: str | None = None,
+ launch_kwargs: dict[str, Any] | None = None,
+ train_kwargs: dict[str, Any] | None = None,
**kwargs,
):
"""
@@ -173,17 +173,17 @@ class LM(BaseLM):
settings.usage_tracker.add_usage(self.model, dict(results.usage))
return results
- def launch(self, launch_kwargs: Optional[Dict[str, Any]] = None):
+ def launch(self, launch_kwargs: Dict[str, Any] | None = None):
self.provider.launch(self, launch_kwargs)
- def kill(self, launch_kwargs: Optional[Dict[str, Any]] = None):
+ def kill(self, launch_kwargs: Dict[str, Any] | None = None):
self.provider.kill(self, launch_kwargs)
def finetune(
self,
train_data: List[Dict[str, Any]],
- train_data_format: Optional[TrainDataFormat],
- train_kwargs: Optional[Dict[str, Any]] = None,
+ train_data_format: TrainDataFormat | None,
+ train_kwargs: Dict[str, Any] | None = None,
) -> TrainingJob:
from dspy import settings as settings
@@ -300,7 +300,7 @@ def _get_stream_completion_fn(
return async_stream_completion
-def litellm_completion(request: Dict[str, Any], num_retries: int, cache: Optional[Dict[str, Any]] = None):
+def litellm_completion(request: Dict[str, Any], num_retries: int, cache: Dict[str, Any] | None = None):
cache = cache or {"no-cache": True, "no-store": True}
stream_completion = _get_stream_completion_fn(request, cache, sync=True)
if stream_completion is None:
@@ -314,7 +314,7 @@ def litellm_completion(request: Dict[str, Any], num_retries: int, cache: Optiona
return stream_completion()
-def litellm_text_completion(request: Dict[str, Any], num_retries: int, cache: Optional[Dict[str, Any]] = None):
+def litellm_text_completion(request: Dict[str, Any], num_retries: int, cache: Dict[str, Any] | None = None):
cache = cache or {"no-cache": True, "no-store": True}
# Extract the provider and model from the model string.
# TODO: Not all the models are in the format of "provider/model"
@@ -340,7 +340,7 @@ def litellm_text_completion(request: Dict[str, Any], num_retries: int, cache: Op
)
-async def alitellm_completion(request: Dict[str, Any], num_retries: int, cache: Optional[Dict[str, Any]] = None):
+async def alitellm_completion(request: Dict[str, Any], num_retries: int, cache: Dict[str, Any] | None = None):
cache = cache or {"no-cache": True, "no-store": True}
stream_completion = _get_stream_completion_fn(request, cache, sync=False)
if stream_completion is None:
@@ -354,7 +354,7 @@ async def alitellm_completion(request: Dict[str, Any], num_retries: int, cache:
return await stream_completion()
-async def alitellm_text_completion(request: Dict[str, Any], num_retries: int, cache: Optional[Dict[str, Any]] = None):
+async def alitellm_text_completion(request: Dict[str, Any], num_retries: int, cache: Dict[str, Any] | None = None):
cache = cache or {"no-cache": True, "no-store": True}
model = request.pop("model").split("/", 1)
provider, model = model[0] if len(model) > 1 else "openai", model[-1]
diff --git a/dspy/clients/lm_local.py b/dspy/clients/lm_local.py
index 79a0d8e02..7a314b4c1 100644
--- a/dspy/clients/lm_local.py
+++ b/dspy/clients/lm_local.py
@@ -6,7 +6,7 @@ import string
import subprocess
import threading
import time
-from typing import TYPE_CHECKING, Any, Dict, List, Optional
+from typing import TYPE_CHECKING, Any, Dict, List
import requests
@@ -26,7 +26,7 @@ class LocalProvider(Provider):
self.TrainingJob = TrainingJob
@staticmethod
- def launch(lm: "LM", launch_kwargs: Optional[Dict[str, Any]] = None):
+ def launch(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):
try:
import sglang # noqa: F401
except ImportError:
@@ -118,7 +118,7 @@ class LocalProvider(Provider):
lm.thread = thread
@staticmethod
- def kill(lm: "LM", launch_kwargs: Optional[Dict[str, Any]] = None):
+ def kill(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):
from sglang.utils import terminate_process
if not hasattr(lm, "process"):
@@ -137,8 +137,8 @@ class LocalProvider(Provider):
job: TrainingJob,
model: str,
train_data: List[Dict[str, Any]],
- train_data_format: Optional[TrainDataFormat],
- train_kwargs: Optional[Dict[str, Any]] = None,
+ train_data_format: TrainDataFormat | None,
+ train_kwargs: Dict[str, Any] | None = None,
) -> str:
if model.startswith("openai/"):
model = model[7:]
@@ -322,7 +322,7 @@ def get_free_port() -> int:
return s.getsockname()[1]
-def wait_for_server(base_url: str, timeout: Optional[int] = None) -> None:
+def wait_for_server(base_url: str, timeout: int | None = None) -> None:
"""
Wait for the server to be ready by polling the /v1/models endpoint.
diff --git a/dspy/clients/lm_local_arbor.py b/dspy/clients/lm_local_arbor.py
index f0c11fe79..70a8b1303 100644
--- a/dspy/clients/lm_local_arbor.py
+++ b/dspy/clients/lm_local_arbor.py
@@ -1,6 +1,6 @@
import time
from datetime import datetime
-from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypedDict, Union
+from typing import TYPE_CHECKING, Any, Dict, List, TypedDict
import openai
import requests
@@ -151,7 +151,7 @@ class ArborReinforceJob(ReinforceJob):
# self.provider_job_id = response.json().get("job_id") # TODO: To be updated
def _run_grpo_step_one_group(
- self, train_group: GRPOGroup, train_data_format: Optional[Union[TrainDataFormat, str]] = None
+ self, train_group: GRPOGroup, train_data_format: TrainDataFormat | str | None = None
):
# TODO: Check that the data follows the intended format
api_base = self.lm.kwargs["api_base"]
@@ -168,7 +168,7 @@ class ArborReinforceJob(ReinforceJob):
current_model = response["current_model"]
self.lm.model = ArborProvider._add_provider_prefix(current_model)
- def step(self, train_data: List[GRPOGroup], train_data_format: Optional[Union[TrainDataFormat, str]]):
+ def step(self, train_data: List[GRPOGroup], train_data_format: TrainDataFormat | str | None):
# Note: TrainDataFormat specifies the format for the inner most dict.
# Because we run GRPO at the group level, train_data will be a list of
# groups, where each group is a list of GRPOChatData. Our teleprompters
@@ -183,7 +183,7 @@ class ArborReinforceJob(ReinforceJob):
for group in train_data:
self._run_grpo_step_one_group(group, train_data_format)
- def save_checkpoint(self, checkpoint_name: str, score: Optional[float] = None):
+ def save_checkpoint(self, checkpoint_name: str, score: float | None = None):
api_base = self.lm.kwargs["api_base"]
url = f"{api_base}fine_tuning/grpo/checkpoint"
headers = {"Content-Type": "application/json"}
@@ -237,7 +237,7 @@ class ArborProvider(Provider):
self.ReinforceJob = ArborReinforceJob
@staticmethod
- def launch(lm: "LM", launch_kwargs: Optional[Dict[str, Any]] = None):
+ def launch(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):
model = ArborProvider._remove_provider_prefix(lm.model)
api_base = lm.kwargs["api_base"]
@@ -253,7 +253,7 @@ class ArborProvider(Provider):
print(f"Inference server for model {model} launched successfully")
@staticmethod
- def kill(lm: "LM", launch_kwargs: Optional[Dict[str, Any]] = None):
+ def kill(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):
api_base = lm.kwargs["api_base"]
response = requests.post(
@@ -296,8 +296,8 @@ class ArborProvider(Provider):
job: ArborTrainingJob,
model: str,
train_data: List[Dict[str, Any]],
- train_data_format: Optional[TrainDataFormat],
- train_kwargs: Optional[Dict[str, Any]] = None,
+ train_data_format: TrainDataFormat | None,
+ train_kwargs: Dict[str, Any] | None = None,
) -> str:
# TODO: We want to re-factor finetune so that it takes in an LM.
# Until then, we use the following to get the api information. The
diff --git a/dspy/clients/openai.py b/dspy/clients/openai.py
index 1f5952ceb..adb5ddeab 100644
--- a/dspy/clients/openai.py
+++ b/dspy/clients/openai.py
@@ -1,7 +1,7 @@
import re
import time
from datetime import datetime
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, List
import openai
@@ -122,8 +122,8 @@ class OpenAIProvider(Provider):
job: TrainingJobOpenAI,
model: str,
train_data: List[Dict[str, Any]],
- train_data_format: Optional[TrainDataFormat],
- train_kwargs: Optional[Dict[str, Any]] = None,
+ train_data_format: TrainDataFormat | None,
+ train_kwargs: Dict[str, Any] | None = None,
) -> str:
model = OpenAIProvider._remove_provider_prefix(model)
@@ -231,7 +231,7 @@ class OpenAIProvider(Provider):
return provider_file.id
@staticmethod
- def _start_remote_training(train_file_id: str, model: str, train_kwargs: Optional[Dict[str, Any]] = None) -> str:
+ def _start_remote_training(train_file_id: str, model: str, train_kwargs: Dict[str, Any] | None = None) -> str:
train_kwargs = train_kwargs or {}
provider_job = openai.fine_tuning.jobs.create(
model=model,
diff --git a/dspy/clients/provider.py b/dspy/clients/provider.py
index f753fbd34..01684bf76 100644
--- a/dspy/clients/provider.py
+++ b/dspy/clients/provider.py
@@ -1,7 +1,7 @@
from abc import abstractmethod
from concurrent.futures import Future
from threading import Thread
-from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
+from typing import TYPE_CHECKING, Any, Dict, List
from dspy.clients.utils_finetune import TrainDataFormat
@@ -12,11 +12,11 @@ if TYPE_CHECKING:
class TrainingJob(Future):
def __init__(
self,
- thread: Optional[Thread] = None,
- model: Optional[str] = None,
- train_data: Optional[List[Dict[str, Any]]] = None,
- train_data_format: Optional[TrainDataFormat] = None,
- train_kwargs: Optional[Dict[str, Any]] = None,
+ thread: Thread | None = None,
+ model: str | None = None,
+ train_data: List[Dict[str, Any]] | None = None,
+ train_data_format: TrainDataFormat | None = None,
+ train_kwargs: Dict[str, Any] | None = None,
):
self.thread = thread
self.model = model
@@ -36,7 +36,7 @@ class TrainingJob(Future):
class ReinforceJob:
- def __init__(self, lm: "LM", train_kwargs: Optional[Dict[str, Any]] = None):
+ def __init__(self, lm: "LM", train_kwargs: Dict[str, Any] | None = None):
self.lm = lm
self.train_kwargs = train_kwargs or {}
self.checkpoints = {}
@@ -47,7 +47,7 @@ class ReinforceJob:
raise NotImplementedError
@abstractmethod
- def step(self, train_data: List[Dict[str, Any]], train_data_format: Optional[Union[TrainDataFormat, str]] = None):
+ def step(self, train_data: List[Dict[str, Any]], train_data_format: TrainDataFormat | str | None = None):
raise NotImplementedError
@abstractmethod
@@ -83,14 +83,14 @@ class Provider:
return False
@staticmethod
- def launch(lm: "LM", launch_kwargs: Optional[Dict[str, Any]] = None):
+ def launch(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):
# Note that "launch" and "kill" methods might be called even if there
# is a launched LM or no launched LM to kill. These methods should be
# resillient to such cases.
pass
@staticmethod
- def kill(lm: "LM", launch_kwargs: Optional[Dict[str, Any]] = None):
+ def kill(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):
# We assume that LM.launch_kwargs dictionary will contain the necessary
# information for a provider to launch and/or kill an LM. This is the
# reeason why the argument here is named launch_kwargs and not
@@ -102,7 +102,7 @@ class Provider:
job: TrainingJob,
model: str,
train_data: List[Dict[str, Any]],
- train_data_format: Optional[Union[TrainDataFormat, str]],
- train_kwargs: Optional[Dict[str, Any]] = None,
+ train_data_format: TrainDataFormat | str | None,
+ train_kwargs: Dict[str, Any] | None = None,
) -> str:
raise NotImplementedError
diff --git a/dspy/clients/utils_finetune.py b/dspy/clients/utils_finetune.py
index 086914c9a..de8be77f3 100644
--- a/dspy/clients/utils_finetune.py
+++ b/dspy/clients/utils_finetune.py
@@ -1,6 +1,6 @@
import os
from enum import Enum
-from typing import Any, Dict, List, Literal, Optional, TypedDict, Union
+from typing import Any, Dict, List, Literal, TypedDict
import ujson
@@ -25,7 +25,7 @@ class TrainDataFormat(str, Enum):
class Message(TypedDict):
- role: Union[Literal["user"], Literal["assistant"], Literal["system"]]
+ role: Literal["user"] | Literal["assistant"] | Literal["system"]
content: str
@@ -116,7 +116,7 @@ def validate_data_format(
raise ValueError(err)
-def find_data_errors_completion(data_dict: Dict[str, str]) -> Optional[str]:
+def find_data_errors_completion(data_dict: Dict[str, str]) -> str | None:
keys = ["prompt", "completion"]
assert isinstance(data_dict, dict)
@@ -132,7 +132,7 @@ def find_data_errors_completion(data_dict: Dict[str, str]) -> Optional[str]:
# Following functions are modified from the OpenAI cookbook:
# https://cookbook.openai.com/examples/chat_finetuning_data_prep
-def find_data_error_chat(messages: Dict[str, Any]) -> Optional[str]:
+def find_data_error_chat(messages: Dict[str, Any]) -> str | None:
assert isinstance(messages, dict)
expected_keys = ["messages"]
@@ -149,7 +149,7 @@ def find_data_error_chat(messages: Dict[str, Any]) -> Optional[str]:
return f"Error in message at index {ind}: {err}"
-def find_data_error_chat_message(message: Dict[str, Any]) -> Optional[str]:
+def find_data_error_chat_message(message: Dict[str, Any]) -> str | None:
assert isinstance(message, dict)
message_keys = sorted(["role", "content"])
diff --git a/dspy/datasets/dataloader.py b/dspy/datasets/dataloader.py
index b96ad68ac..9050dd7aa 100644
--- a/dspy/datasets/dataloader.py
+++ b/dspy/datasets/dataloader.py
@@ -1,6 +1,6 @@
import random
from collections.abc import Mapping
-from typing import TYPE_CHECKING, List, Optional, Tuple, Union
+from typing import TYPE_CHECKING, List, Tuple
import dspy
from dspy.datasets.dataset import Dataset
@@ -18,9 +18,9 @@ class DataLoader(Dataset):
dataset_name: str,
*args,
input_keys: Tuple[str] = (),
- fields: Optional[Tuple[str]] = None,
+ fields: Tuple[str] | None = None,
**kwargs,
- ) -> Union[Mapping[str, List[dspy.Example]], List[dspy.Example]]:
+ ) -> Mapping[str, List[dspy.Example]] | List[dspy.Example]:
if fields and not isinstance(fields, tuple):
raise ValueError("Invalid fields provided. Please provide a tuple of fields.")
@@ -63,7 +63,7 @@ class DataLoader(Dataset):
def from_csv(
self,
file_path: str,
- fields: Optional[List[str]] = None,
+ fields: List[str] | None = None,
input_keys: Tuple[str] = (),
) -> List[dspy.Example]:
from datasets import load_dataset
@@ -78,7 +78,7 @@ class DataLoader(Dataset):
def from_pandas(
self,
df: "pd.DataFrame",
- fields: Optional[List[str]] = None,
+ fields: List[str] | None = None,
input_keys: tuple[str] = (),
) -> list[dspy.Example]:
if fields is None:
@@ -91,7 +91,7 @@ class DataLoader(Dataset):
def from_json(
self,
file_path: str,
- fields: Optional[List[str]] = None,
+ fields: List[str] | None = None,
input_keys: Tuple[str] = (),
) -> List[dspy.Example]:
from datasets import load_dataset
@@ -106,7 +106,7 @@ class DataLoader(Dataset):
def from_parquet(
self,
file_path: str,
- fields: Optional[List[str]] = None,
+ fields: List[str] | None = None,
input_keys: Tuple[str] = (),
) -> List[dspy.Example]:
from datasets import load_dataset
@@ -152,9 +152,9 @@ class DataLoader(Dataset):
def train_test_split(
self,
dataset: List[dspy.Example],
- train_size: Union[int, float] = 0.75,
- test_size: Optional[Union[int, float]] = None,
- random_state: Optional[int] = None,
+ train_size: int | float = 0.75,
+ test_size: int | float | None = None,
+ random_state: int | None = None,
) -> Mapping[str, List[dspy.Example]]:
if random_state is not None:
random.seed(random_state)
diff --git a/dspy/dsp/colbertv2.py b/dspy/dsp/colbertv2.py
index ec5fd908f..90b2850e8 100644
--- a/dspy/dsp/colbertv2.py
+++ b/dspy/dsp/colbertv2.py
@@ -1,4 +1,4 @@
-from typing import Any, List, Optional, Union
+from typing import Any, List
import requests
@@ -14,7 +14,7 @@ class ColBERTv2:
def __init__(
self,
url: str = "http://0.0.0.0",
- port: Optional[Union[str, int]] = None,
+ port: str | int | None = None,
post_requests: bool = False,
):
self.post_requests = post_requests
@@ -25,7 +25,7 @@ class ColBERTv2:
query: str,
k: int = 10,
simplify: bool = False,
- ) -> Union[list[str], list[dotdict]]:
+ ) -> list[str] | list[dotdict]:
if self.post_requests:
topk: list[dict[str, Any]] = colbertv2_post_request(self.url, query, k)
else:
@@ -165,7 +165,7 @@ class ColBERTv2RetrieverLocal:
else:
searcher_results = self.searcher.search(query, k=k)
results = []
- for pid, rank, score in zip(*searcher_results): # noqa: B007
+ for pid, rank, score in zip(*searcher_results, strict=False): # noqa: B007
results.append(dotdict({"long_text": self.searcher.collection[pid], "score": score, "pid": pid}))
return results
@@ -192,7 +192,7 @@ class ColBERTv2RerankerLocal:
def __call__(self, *args: Any, **kwargs: Any) -> Any:
return self.forward(*args, **kwargs)
- def forward(self, query: str, passages: Optional[List[str]] = None):
+ def forward(self, query: str, passages: List[str] | None = None):
assert len(passages) > 0, "Passages should not be empty"
import numpy as np
diff --git a/dspy/dsp/utils/utils.py b/dspy/dsp/utils/utils.py
index 7ff6cd3a1..19ab4dd59 100644
--- a/dspy/dsp/utils/utils.py
+++ b/dspy/dsp/utils/utils.py
@@ -120,7 +120,7 @@ def zipstar(data_list, lazy=False):
if width < 100:
return [[elem[idx] for elem in data_list] for idx in range(width)]
- zipped_data = zip(*data_list)
+ zipped_data = zip(*data_list, strict=False)
return zipped_data if lazy else list(zipped_data)
@@ -128,7 +128,7 @@ def zipstar(data_list, lazy=False):
def zip_first(list1, list2):
length = len(list1) if type(list1) in [tuple, list] else None
- zipped_data = list(zip(list1, list2))
+ zipped_data = list(zip(list1, list2, strict=False))
assert length in [None, len(zipped_data)], "zip_first() failure: length differs!"
diff --git a/dspy/evaluate/evaluate.py b/dspy/evaluate/evaluate.py
index ee05ff7df..0fdec0b6e 100644
--- a/dspy/evaluate/evaluate.py
+++ b/dspy/evaluate/evaluate.py
@@ -1,7 +1,7 @@
import importlib
import logging
import types
-from typing import TYPE_CHECKING, Any, Callable, List, Optional, Tuple, Union
+from typing import TYPE_CHECKING, Any, Callable, List, Tuple
if TYPE_CHECKING:
import pandas as pd
@@ -69,12 +69,12 @@ class Evaluate:
self,
*,
devset: List["dspy.Example"],
- metric: Optional[Callable] = None,
- num_threads: Optional[int] = None,
+ metric: Callable | None = None,
+ num_threads: int | None = None,
display_progress: bool = False,
- display_table: Union[bool, int] = False,
- max_errors: Optional[int] = None,
- provide_traceback: Optional[bool] = None,
+ display_table: bool | int = False,
+ max_errors: int | None = None,
+ provide_traceback: bool | None = None,
failure_score: float = 0.0,
**kwargs,
):
@@ -104,12 +104,12 @@ class Evaluate:
def __call__(
self,
program: "dspy.Module",
- metric: Optional[Callable] = None,
- devset: Optional[List["dspy.Example"]] = None,
- num_threads: Optional[int] = None,
- display_progress: Optional[bool] = None,
- display_table: Optional[Union[bool, int]] = None,
- callback_metadata: Optional[dict[str, Any]] = None,
+ metric: Callable | None = None,
+ devset: List["dspy.Example"] | None = None,
+ num_threads: int | None = None,
+ display_progress: bool | None = None,
+ display_table: bool | int | None = None,
+ callback_metadata: dict[str, Any] | None = None,
) -> EvaluationResult:
"""
Args:
@@ -170,7 +170,7 @@ class Evaluate:
assert len(devset) == len(results)
results = [((dspy.Prediction(), self.failure_score) if r is None else r) for r in results]
- results = [(example, prediction, score) for example, (prediction, score) in zip(devset, results)]
+ results = [(example, prediction, score) for example, (prediction, score) in zip(devset, results, strict=False)]
ncorrect, ntotal = sum(score for *_, score in results), len(devset)
logger.info(f"Average Metric: {ncorrect} / {ntotal} ({round(100 * ncorrect / ntotal, 1)}%)")
@@ -223,7 +223,7 @@ class Evaluate:
return result_df.rename(columns={"correct": metric_name})
- def _display_result_table(self, result_df: "pd.DataFrame", display_table: Union[bool, int], metric_name: str):
+ def _display_result_table(self, result_df: "pd.DataFrame", display_table: bool | int, metric_name: str):
"""
Display the specified result DataFrame in a table format.
diff --git a/dspy/predict/avatar/models.py b/dspy/predict/avatar/models.py
index ca40baa82..6b8fa968d 100644
--- a/dspy/predict/avatar/models.py
+++ b/dspy/predict/avatar/models.py
@@ -1,4 +1,4 @@
-from typing import Any, Optional
+from typing import Any
from pydantic import BaseModel, Field
@@ -6,8 +6,8 @@ from pydantic import BaseModel, Field
class Tool(BaseModel):
tool: Any
name: str
- desc: Optional[str]
- input_type: Optional[str] = None
+ desc: str | None
+ input_type: str | None = None
def __str__(self) -> str:
return f"{self.name}{f'(valid_input: {self.input_type})' if self.input_type else ''}: {self.desc}"
diff --git a/dspy/predict/best_of_n.py b/dspy/predict/best_of_n.py
index f2635dcb6..9209793d0 100644
--- a/dspy/predict/best_of_n.py
+++ b/dspy/predict/best_of_n.py
@@ -1,4 +1,4 @@
-from typing import Callable, Optional
+from typing import Callable
import dspy
from dspy.predict.predict import Module, Prediction
@@ -11,7 +11,7 @@ class BestOfN(Module):
N: int, # noqa: N803
reward_fn: Callable[[dict, Prediction], float],
threshold: float,
- fail_count: Optional[int] = None,
+ fail_count: int | None = None,
):
"""
Runs a module up to `N` times with different temperatures and returns the best prediction
diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.py
index ba24a97d9..ecdd97cd5 100644
--- a/dspy/predict/chain_of_thought.py
+++ b/dspy/predict/chain_of_thought.py
@@ -1,4 +1,4 @@
-from typing import Any, Optional, Type, Union
+from typing import Any, Type
from pydantic.fields import FieldInfo
@@ -11,8 +11,8 @@ from dspy.signatures.signature import Signature, ensure_signature
class ChainOfThought(Module):
def __init__(
self,
- signature: Union[str, Type[Signature]],
- rationale_field: Optional[Union[OutputField, FieldInfo]] = None,
+ signature: str | Type[Signature],
+ rationale_field: FieldInfo | None = None,
rationale_field_type: Type = str,
**config: dict[str, Any],
):
diff --git a/dspy/predict/code_act.py b/dspy/predict/code_act.py
index 8d76383e9..d85b3e993 100644
--- a/dspy/predict/code_act.py
+++ b/dspy/predict/code_act.py
@@ -1,6 +1,6 @@
import inspect
import logging
-from typing import Callable, Optional, Type, Union
+from typing import Callable, Type
import dspy
from dspy.adapters.types.tool import Tool
@@ -16,7 +16,7 @@ class CodeAct(ReAct, ProgramOfThought):
CodeAct is a module that utilizes the Code Interpreter and predefined tools to solve the problem.
"""
- def __init__(self, signature: Union[str, Type[Signature]], tools: list[Callable], max_iters: int = 5, interpreter: Optional[PythonInterpreter] = None):
+ def __init__(self, signature: str | Type[Signature], tools: list[Callable], max_iters: int = 5, interpreter: PythonInterpreter | None = None):
"""
Initializes the CodeAct class with the specified model, temperature, and max tokens.
diff --git a/dspy/predict/parallel.py b/dspy/predict/parallel.py
index 46ec7b8cf..b0de0251f 100644
--- a/dspy/predict/parallel.py
+++ b/dspy/predict/parallel.py
@@ -1,5 +1,5 @@
import threading
-from typing import Any, List, Optional, Tuple
+from typing import Any, List, Tuple
from dspy.dsp.utils.settings import settings
from dspy.primitives.example import Example
@@ -9,11 +9,11 @@ from dspy.utils.parallelizer import ParallelExecutor
class Parallel:
def __init__(
self,
- num_threads: Optional[int] = None,
- max_errors: Optional[int] = None,
+ num_threads: int | None = None,
+ max_errors: int | None = None,
access_examples: bool = True,
return_failed_examples: bool = False,
- provide_traceback: Optional[bool] = None,
+ provide_traceback: bool | None = None,
disable_progress_bar: bool = False,
):
super().__init__()
@@ -30,7 +30,7 @@ class Parallel:
self.failed_examples = []
self.exceptions = []
- def forward(self, exec_pairs: List[Tuple[Any, Example]], num_threads: Optional[int] = None) -> List[Any]:
+ def forward(self, exec_pairs: List[Tuple[Any, Example]], num_threads: int | None = None) -> List[Any]:
num_threads = num_threads if num_threads is not None else self.num_threads
executor = ParallelExecutor(
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.py
index a46b6bd37..bec1e4720 100644
--- a/dspy/predict/predict.py
+++ b/dspy/predict/predict.py
@@ -1,6 +1,6 @@
import logging
import random
-from typing import Optional, Type, Union
+from typing import Type
from pydantic import BaseModel
@@ -18,7 +18,7 @@ logger = logging.getLogger(__name__)
class Predict(Module, Parameter):
- def __init__(self, signature: Union[str, Type[Signature]], callbacks: Optional[list[BaseCallback]] = None, **config):
+ def __init__(self, signature: str | Type[Signature], callbacks: list[BaseCallback] | None = None, **config):
super().__init__(callbacks=callbacks)
self.stage = random.randbytes(8).hex()
self.signature = ensure_signature(signature)
diff --git a/dspy/predict/program_of_thought.py b/dspy/predict/program_of_thought.py
index 66de91323..e55b8d060 100644
--- a/dspy/predict/program_of_thought.py
+++ b/dspy/predict/program_of_thought.py
@@ -1,7 +1,7 @@
import json
import logging
import re
-from typing import Optional, Type, Union
+from typing import Type
import dspy
from dspy.primitives.module import Module
@@ -27,7 +27,7 @@ class ProgramOfThought(Module):
```
"""
- def __init__(self, signature: Union[str, Type[Signature]], max_iters: int = 3, interpreter: Optional[PythonInterpreter] = None):
+ def __init__(self, signature: str | Type[Signature], max_iters: int = 3, interpreter: PythonInterpreter | None = None):
"""
Args:
signature: The signature of the module.
diff --git a/dspy/predict/refine.py b/dspy/predict/refine.py
index 3c917b508..10ec89d11 100644
--- a/dspy/predict/refine.py
+++ b/dspy/predict/refine.py
@@ -1,6 +1,6 @@
import inspect
import textwrap
-from typing import Callable, Optional
+from typing import Callable
import ujson
@@ -45,7 +45,7 @@ class Refine(Module):
N: int, # noqa: N803
reward_fn: Callable[[dict, Prediction], float],
threshold: float,
- fail_count: Optional[int] = None,
+ fail_count: int | None = None,
):
"""
Refines a module by running it up to N times with different temperatures and returns the best prediction.
diff --git a/dspy/primitives/module.py b/dspy/primitives/module.py
index bc3c70f66..e29281650 100644
--- a/dspy/primitives/module.py
+++ b/dspy/primitives/module.py
@@ -1,6 +1,5 @@
import inspect
import logging
-from typing import Optional
import magicattr
@@ -119,10 +118,10 @@ class Module(BaseModule, metaclass=ProgramMeta):
def batch(
self,
examples: list[Example],
- num_threads: Optional[int] = None,
- max_errors: Optional[int] = None,
+ num_threads: int | None = None,
+ max_errors: int | None = None,
return_failed_examples: bool = False,
- provide_traceback: Optional[bool] = None,
+ provide_traceback: bool | None = None,
disable_progress_bar: bool = False,
):
"""
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.py
index d9c960d86..c3ba85e71 100644
--- a/dspy/primitives/python_interpreter.py
+++ b/dspy/primitives/python_interpreter.py
@@ -3,7 +3,7 @@ import os
import subprocess
from os import PathLike
from types import TracebackType
-from typing import Any, Dict, List, Optional, Union
+from typing import Any, Dict, List
class InterpreterError(RuntimeError):
@@ -27,11 +27,11 @@ class PythonInterpreter:
def __init__(
self,
- deno_command: Optional[List[str]] = None,
- enable_read_paths: Optional[List[Union[PathLike, str]]] = None,
- enable_write_paths: Optional[List[Union[PathLike, str]]] = None,
- enable_env_vars: Optional[List[str]] = None,
- enable_network_access: Optional[List[str]] = None,
+ deno_command: List[str] | None = None,
+ enable_read_paths: List[PathLike | str] | None = None,
+ enable_write_paths: List[PathLike | str] | None = None,
+ enable_env_vars: List[str] | None = None,
+ enable_network_access: List[str] | None = None,
sync_files: bool = True,
) -> None:
"""
@@ -167,7 +167,7 @@ class PythonInterpreter:
def execute(
self,
code: str,
- variables: Optional[Dict[str, Any]] = None,
+ variables: Dict[str, Any] | None = None,
) -> Any:
variables = variables or {}
code = self._inject_variables(code, variables)
@@ -222,16 +222,16 @@ class PythonInterpreter:
# All exception fields are ignored and the runtime will automatically re-raise the exception
def __exit__(
self,
- _exc_type: Optional[type[BaseException]],
- _exc_val: Optional[BaseException],
- _exc_tb: Optional[TracebackType],
+ _exc_type: type[BaseException] | None,
+ _exc_val: BaseException | None,
+ _exc_tb: TracebackType | None,
):
self.shutdown()
def __call__(
self,
code: str,
- variables: Optional[Dict[str, Any]] = None,
+ variables: Dict[str, Any] | None = None,
) -> Any:
return self.execute(code, variables)
diff --git a/dspy/retrievers/databricks_rm.py b/dspy/retrievers/databricks_rm.py
index 115aa79eb..bca5d541e 100644
--- a/dspy/retrievers/databricks_rm.py
+++ b/dspy/retrievers/databricks_rm.py
@@ -2,7 +2,7 @@ import json
import os
from dataclasses import dataclass
from importlib.util import find_spec
-from typing import Any, Dict, List, Optional, Union
+from typing import Any, Dict, List
import requests
@@ -84,15 +84,15 @@ class DatabricksRM(dspy.Retrieve):
def __init__(
self,
databricks_index_name: str,
- databricks_endpoint: Optional[str] = None,
- databricks_token: Optional[str] = None,
- databricks_client_id: Optional[str] = None,
- databricks_client_secret: Optional[str] = None,
- columns: Optional[List[str]] = None,
- filters_json: Optional[str] = None,
+ databricks_endpoint: str | None = None,
+ databricks_token: str | None = None,
+ databricks_client_id: str | None = None,
+ databricks_client_secret: str | None = None,
+ columns: List[str] | None = None,
+ filters_json: str | None = None,
k: int = 3,
docs_id_column_name: str = "id",
- docs_uri_column_name: Optional[str] = None,
+ docs_uri_column_name: str | None = None,
text_column_name: str = "text",
use_with_databricks_agent_framework: bool = False,
):
@@ -207,10 +207,10 @@ class DatabricksRM(dspy.Retrieve):
def forward(
self,
- query: Union[str, List[float]],
+ query: str | List[float],
query_type: str = "ANN",
- filters_json: Optional[str] = None,
- ) -> Union[dspy.Prediction, List[Dict[str, Any]]]:
+ filters_json: str | None = None,
+ ) -> dspy.Prediction | List[Dict[str, Any]]:
"""
Retrieve documents from a Databricks Mosaic AI Vector Search Index that are relevant to the
specified query.
@@ -293,7 +293,7 @@ class DatabricksRM(dspy.Retrieve):
if "data_array" in results["result"]:
for _, data_row in enumerate(results["result"]["data_array"]):
item = {}
- for col_name, val in zip(col_names, data_row):
+ for col_name, val in zip(col_names, data_row, strict=False):
item[col_name] = val
items += [item]
@@ -328,13 +328,13 @@ class DatabricksRM(dspy.Retrieve):
k: int,
columns: List[str],
query_type: str,
- query_text: Optional[str],
- query_vector: Optional[List[float]],
- databricks_token: Optional[str],
- databricks_endpoint: Optional[str],
- databricks_client_id: Optional[str],
- databricks_client_secret: Optional[str],
- filters_json: Optional[str],
+ query_text: str | None,
+ query_vector: List[float] | None,
+ databricks_token: str | None,
+ databricks_endpoint: str | None,
+ databricks_client_id: str | None,
+ databricks_client_secret: str | None,
+ filters_json: str | None,
) -> Dict[str, Any]:
"""
Query a Databricks Vector Search Index via the Databricks SDK.
@@ -401,9 +401,9 @@ class DatabricksRM(dspy.Retrieve):
databricks_token: str,
databricks_endpoint: str,
query_type: str,
- query_text: Optional[str],
- query_vector: Optional[List[float]],
- filters_json: Optional[str],
+ query_text: str | None,
+ query_vector: List[float] | None,
+ filters_json: str | None,
) -> Dict[str, Any]:
"""
Query a Databricks Vector Search Index via the Python requests library.
diff --git a/dspy/retrievers/embeddings.py b/dspy/retrievers/embeddings.py
index bf75aa3ce..ee17c83ca 100644
--- a/dspy/retrievers/embeddings.py
+++ b/dspy/retrievers/embeddings.py
@@ -1,4 +1,4 @@
-from typing import Any, List, Optional
+from typing import Any, List
import numpy as np
@@ -13,7 +13,7 @@ class Embeddings:
corpus: List[str],
embedder,
k: int = 5,
- callbacks: Optional[List[Any]] = None,
+ callbacks: List[Any] | None = None,
cache: bool = False,
brute_force_threshold: int = 20_000,
normalize: bool = True,
diff --git a/dspy/retrievers/retrieve.py b/dspy/retrievers/retrieve.py
index a21cc8c3c..3dda45397 100644
--- a/dspy/retrievers/retrieve.py
+++ b/dspy/retrievers/retrieve.py
@@ -1,5 +1,5 @@
import random
-from typing import List, Optional, Union
+from typing import List
from dspy.predict.parameter import Parameter
from dspy.primitives.prediction import Prediction
@@ -44,9 +44,9 @@ class Retrieve(Parameter):
def forward(
self,
query: str,
- k: Optional[int] = None,
+ k: int | None = None,
**kwargs,
- ) -> Union[List[str], Prediction, List[Prediction]]:
+ ) -> List[str] | Prediction | List[Prediction]:
k = k if k is not None else self.k
import dspy
diff --git a/dspy/retrievers/weaviate_rm.py b/dspy/retrievers/weaviate_rm.py
index 434afc6ec..15e895516 100644
--- a/dspy/retrievers/weaviate_rm.py
+++ b/dspy/retrievers/weaviate_rm.py
@@ -1,4 +1,4 @@
-from typing import List, Optional, Union
+from typing import List
import dspy
from dspy.dsp.utils import dotdict
@@ -50,10 +50,10 @@ class WeaviateRM(dspy.Retrieve):
def __init__(
self,
weaviate_collection_name: str,
- weaviate_client: Union[weaviate.WeaviateClient, weaviate.Client],
- weaviate_collection_text_key: Optional[str] = "content",
+ weaviate_client: weaviate.WeaviateClient | weaviate.Client,
+ weaviate_collection_text_key: str | None = "content",
k: int = 3,
- tenant_id: Optional[str] = None,
+ tenant_id: str | None = None,
):
self._weaviate_collection_name = weaviate_collection_name
self._weaviate_client = weaviate_client
@@ -71,7 +71,7 @@ class WeaviateRM(dspy.Retrieve):
super().__init__(k=k)
- def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = None, **kwargs) -> Prediction:
+ def forward(self, query_or_queries: str | List[str], k: int | None = None, **kwargs) -> Prediction:
"""Search with Weaviate for self.k top passages for query or queries.
Args:
diff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.py
index 061f369cd..5f76b4524 100644
--- a/dspy/signatures/signature.py
+++ b/dspy/signatures/signature.py
@@ -23,7 +23,7 @@ import sys
import types
import typing
from copy import deepcopy
-from typing import Any, Dict, Optional, Tuple, Type, Union
+from typing import Any, Dict, Tuple, Type
from pydantic import BaseModel, Field, create_model
from pydantic.fields import FieldInfo
@@ -248,7 +248,7 @@ class Signature(BaseModel, metaclass=SignatureMeta):
return Signature(cls.fields, instructions)
@classmethod
- def with_updated_fields(cls, name: str, type_: Optional[Type] = None, **kwargs: dict[str, Any]) -> Type["Signature"]:
+ def with_updated_fields(cls, name: str, type_: Type | None = None, **kwargs: dict[str, Any]) -> Type["Signature"]:
"""Create a new Signature class with the updated field information.
Returns a new Signature class with the field, name, updated
@@ -290,7 +290,7 @@ class Signature(BaseModel, metaclass=SignatureMeta):
return Signature(fields, cls.instructions)
@classmethod
- def insert(cls, index: int, name: str, field, type_: Optional[Type] = None) -> Type["Signature"]:
+ def insert(cls, index: int, name: str, field, type_: Type | None = None) -> Type["Signature"]:
# It's possible to set the type as annotation=type in pydantic.Field(...)
# But this may be annoying for users, so we allow them to pass the type
if type_ is None:
@@ -348,14 +348,14 @@ class Signature(BaseModel, metaclass=SignatureMeta):
signature_copy = Signature(deepcopy(cls.fields), cls.instructions)
signature_copy.instructions = state["instructions"]
- for field, saved_field in zip(signature_copy.fields.values(), state["fields"]):
+ for field, saved_field in zip(signature_copy.fields.values(), state["fields"], strict=False):
field.json_schema_extra["prefix"] = saved_field["prefix"]
field.json_schema_extra["desc"] = saved_field["description"]
return signature_copy
-def ensure_signature(signature: Union[str, Type[Signature]], instructions=None) -> Signature:
+def ensure_signature(signature: str | Type[Signature], instructions=None) -> Signature:
if signature is None:
return None
if isinstance(signature, str):
@@ -366,10 +366,10 @@ def ensure_signature(signature: Union[str, Type[Signature]], instructions=None)
def make_signature(
- signature: Union[str, Dict[str, Tuple[type, FieldInfo]]],
- instructions: Optional[str] = None,
+ signature: str | Dict[str, Tuple[type, FieldInfo]],
+ instructions: str | None = None,
signature_name: str = "StringSignature",
- custom_types: Optional[Dict[str, Type]] = None,
+ custom_types: Dict[str, Type] | None = None,
) -> Type[Signature]:
"""Create a new Signature subclass with the specified fields and instructions.
@@ -475,7 +475,7 @@ def _parse_field_string(field_string: str, names=None) -> Dict[str, str]:
args = ast.parse(f"def f({field_string}): pass").body[0].args.args
field_names = [arg.arg for arg in args]
types = [str if arg.annotation is None else _parse_type_node(arg.annotation, names) for arg in args]
- return zip(field_names, types)
+ return zip(field_names, types, strict=False)
def _parse_type_node(node, names=None) -> Any:
@@ -594,7 +594,7 @@ def _parse_type_node(node, names=None) -> Any:
values.append(kw.value.value)
else:
values.append(_parse_type_node(kw.value, names))
- return Field(**dict(zip(keys, values)))
+ return Field(**dict(zip(keys, values, strict=False)))
raise ValueError(
f"Failed to parse string-base Signature due to unhandled AST node type in annotation: {ast.dump(node)}. "
diff --git a/dspy/streaming/messages.py b/dspy/streaming/messages.py
index 49023e8a2..34c264b69 100644
--- a/dspy/streaming/messages.py
+++ b/dspy/streaming/messages.py
@@ -1,7 +1,7 @@
import asyncio
import concurrent.futures
from dataclasses import dataclass
-from typing import Any, Dict, Optional
+from typing import Any, Dict
from asyncer import syncify
@@ -95,7 +95,7 @@ class StatusMessageProvider:
class StatusStreamingCallback(BaseCallback):
- def __init__(self, status_message_provider: Optional[StatusMessageProvider] = None):
+ def __init__(self, status_message_provider: StatusMessageProvider | None = None):
self.status_message_provider = status_message_provider or StatusMessageProvider()
def on_tool_start(
@@ -115,8 +115,8 @@ class StatusStreamingCallback(BaseCallback):
def on_tool_end(
self,
call_id: str,
- outputs: Optional[Dict[str, Any]],
- exception: Optional[Exception] = None,
+ outputs: Dict[str, Any] | None,
+ exception: Exception | None = None,
):
stream = settings.send_stream
if stream is None or outputs == "Completed.":
@@ -143,8 +143,8 @@ class StatusStreamingCallback(BaseCallback):
def on_lm_end(
self,
call_id: str,
- outputs: Optional[Dict[str, Any]],
- exception: Optional[Exception] = None,
+ outputs: Dict[str, Any] | None,
+ exception: Exception | None = None,
):
stream = settings.send_stream
if stream is None:
@@ -171,8 +171,8 @@ class StatusStreamingCallback(BaseCallback):
def on_module_end(
self,
call_id: str,
- outputs: Optional[Dict[str, Any]],
- exception: Optional[Exception] = None,
+ outputs: Dict[str, Any] | None,
+ exception: Exception | None = None,
):
stream = settings.send_stream
if stream is None:
diff --git a/dspy/streaming/streamify.py b/dspy/streaming/streamify.py
index 89ffb6f94..76861d1da 100644
--- a/dspy/streaming/streamify.py
+++ b/dspy/streaming/streamify.py
@@ -4,7 +4,7 @@ import logging
import threading
from asyncio import iscoroutinefunction
from queue import Queue
-from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generator, List, Optional
+from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generator, List
import litellm
import ujson
@@ -26,8 +26,8 @@ if TYPE_CHECKING:
def streamify(
program: "Module",
- status_message_provider: Optional[StatusMessageProvider] = None,
- stream_listeners: Optional[List[StreamListener]] = None,
+ status_message_provider: StatusMessageProvider | None = None,
+ stream_listeners: List[StreamListener] | None = None,
include_final_prediction_in_output_stream: bool = True,
is_async_program: bool = False,
async_streaming: bool = True,
diff --git a/dspy/streaming/streaming_listener.py b/dspy/streaming/streaming_listener.py
index 2aaec847f..88d0925ce 100644
--- a/dspy/streaming/streaming_listener.py
+++ b/dspy/streaming/streaming_listener.py
@@ -1,7 +1,7 @@
import re
from collections import defaultdict
from queue import Queue
-from typing import TYPE_CHECKING, Any, List, Optional
+from typing import TYPE_CHECKING, Any, List
from litellm import ModelResponseStream
@@ -17,7 +17,7 @@ if TYPE_CHECKING:
class StreamListener:
"""Class that listens to the stream to capture the streeaming of a specific output field of a predictor."""
- def __init__(self, signature_field_name: str, predict: Any = None, predict_name: Optional[str] = None):
+ def __init__(self, signature_field_name: str, predict: Any = None, predict_name: str | None = None):
"""
Args:
signature_field_name: The name of the field to listen to.
diff --git a/dspy/teleprompt/avatar_optimizer.py b/dspy/teleprompt/avatar_optimizer.py
index 164db5b67..97278edd7 100644
--- a/dspy/teleprompt/avatar_optimizer.py
+++ b/dspy/teleprompt/avatar_optimizer.py
@@ -1,7 +1,7 @@
from concurrent.futures import ThreadPoolExecutor
from copy import deepcopy
from random import sample
-from typing import Callable, List, Optional, Tuple
+from typing import Callable, List, Tuple
from pydantic import BaseModel
from tqdm import tqdm
@@ -16,7 +16,7 @@ DEFAULT_MAX_EXAMPLES = 10
class EvalResult(BaseModel):
example: dict
score: float
- actions: Optional[List[ActionOutput]] = None
+ actions: List[ActionOutput] | None = None
class Comparator(dspy.Signature):
@@ -79,8 +79,8 @@ class AvatarOptimizer(Teleprompter):
max_iters: int = 10,
lower_bound: int = 0,
upper_bound: int = 1,
- max_positive_inputs: Optional[int] = None,
- max_negative_inputs: Optional[int] = None,
+ max_positive_inputs: int | None = None,
+ max_negative_inputs: int | None = None,
optimize_for: str = "max",
):
assert metric is not None, "`metric` argument cannot be None. Please provide a metric function."
diff --git a/dspy/teleprompt/bettertogether.py b/dspy/teleprompt/bettertogether.py
index 8712f8e82..7f72069d0 100644
--- a/dspy/teleprompt/bettertogether.py
+++ b/dspy/teleprompt/bettertogether.py
@@ -1,6 +1,6 @@
import logging
import random
-from typing import Callable, List, Optional
+from typing import Callable, List
import dspy
from dspy.primitives.example import Example
@@ -24,9 +24,9 @@ class BetterTogether(Teleprompter):
def __init__(self,
metric: Callable,
- prompt_optimizer: Optional[Teleprompter] = None,
- weight_optimizer: Optional[Teleprompter] = None,
- seed: Optional[int] = None,
+ prompt_optimizer: Teleprompter | None = None,
+ weight_optimizer: Teleprompter | None = None,
+ seed: int | None = None,
):
if not dspy.settings.experimental:
raise ValueError("This is an experimental optimizer. Set `dspy.settings.experimental` to `True` to use it.")
@@ -143,7 +143,7 @@ class BetterTogether(Teleprompter):
logger.info("Compiling the prompt optimizer...")
pred_lms = [pred.lm for pred in student.predictors()]
student = self.prompt_optimizer.compile(student, trainset=prompt_trainset, valset=prompt_valset)
- for pred, lm in zip(student.predictors(), pred_lms):
+ for pred, lm in zip(student.predictors(), pred_lms, strict=False):
pred.lm = lm
return student
@@ -163,7 +163,7 @@ class BetterTogether(Teleprompter):
# Updating the train kwargs for the new LMs. This is needed because the
# train_kwargs of the optimizer is configured for the original LMs.
new_lms = [pred.lm for pred in student.predictors()]
- for original_lm, new_lm in zip(original_lms, new_lms):
+ for original_lm, new_lm in zip(original_lms, new_lms, strict=False):
original_params = self.weight_optimizer.train_kwargs[original_lm]
self.weight_optimizer.train_kwargs[new_lm] = original_params
diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.py
index 4cacb0136..23495bbad 100644
--- a/dspy/teleprompt/bootstrap.py
+++ b/dspy/teleprompt/bootstrap.py
@@ -1,7 +1,7 @@
import logging
import random
import threading
-from typing import Dict, Optional
+from typing import Dict
import tqdm
@@ -39,7 +39,7 @@ class BootstrapFewShot(Teleprompter):
self,
metric=None,
metric_threshold=None,
- teacher_settings: Optional[Dict] = None,
+ teacher_settings: Dict | None = None,
max_bootstrapped_demos=4,
max_labeled_demos=16,
max_rounds=1,
@@ -112,7 +112,7 @@ class BootstrapFewShot(Teleprompter):
teacher.predictors(),
), "Student and teacher must have the same number of predictors."
- for (name1, predictor1), (name2, predictor2) in zip(student.named_predictors(), teacher.named_predictors()):
+ for (name1, predictor1), (name2, predictor2) in zip(student.named_predictors(), teacher.named_predictors(), strict=False):
assert name1 == name2, "Student and teacher must have the same program structure."
if hasattr(predictor1.signature, "equals"):
assert predictor1.signature.equals(
diff --git a/dspy/teleprompt/bootstrap_finetune.py b/dspy/teleprompt/bootstrap_finetune.py
index 5028099f4..19db70432 100644
--- a/dspy/teleprompt/bootstrap_finetune.py
+++ b/dspy/teleprompt/bootstrap_finetune.py
@@ -1,7 +1,7 @@
import logging
from collections import defaultdict
from dataclasses import dataclass
-from typing import Any, Callable, Dict, List, Optional, Union
+from typing import Any, Callable, Dict, List
import dspy
from dspy.adapters.base import Adapter
@@ -22,7 +22,7 @@ logger = logging.getLogger(__name__)
class FinetuneTeleprompter(Teleprompter):
def __init__(
self,
- train_kwargs: Optional[Union[Dict[str, Any], Dict[LM, Dict[str, Any]]]] = None,
+ train_kwargs: Dict[str, Any] | Dict[LM, Dict[str, Any]] | None = None,
):
self.train_kwargs: Dict[LM, Any] = self.convert_to_lm_dict(train_kwargs or {})
@@ -38,12 +38,12 @@ class FinetuneTeleprompter(Teleprompter):
class BootstrapFinetune(FinetuneTeleprompter):
def __init__(
self,
- metric: Optional[Callable] = None,
+ metric: Callable | None = None,
multitask: bool = True,
- train_kwargs: Optional[Union[Dict[str, Any], Dict[LM, Dict[str, Any]]]] = None,
- adapter: Optional[Union[Adapter, Dict[LM, Adapter]]] = None,
+ train_kwargs: Dict[str, Any] | Dict[LM, Dict[str, Any]] | None = None,
+ adapter: Adapter | Dict[LM, Adapter] | None = None,
exclude_demos: bool = False,
- num_threads: Optional[int] = None,
+ num_threads: int | None = None,
):
# TODO(feature): Inputs train_kwargs (a dict with string keys) and
# adapter (Adapter) can depend on the LM they are used with. We are
@@ -60,7 +60,7 @@ class BootstrapFinetune(FinetuneTeleprompter):
self.num_threads = num_threads
def compile(
- self, student: Module, trainset: List[Example], teacher: Optional[Union[Module, List[Module]]] = None
+ self, student: Module, trainset: List[Example], teacher: Module | List[Module] | None = None
) -> Module:
# TODO: Print statements can be converted to logger.info if we ensure
# that the default DSPy logger logs info level messages in notebook
@@ -160,7 +160,7 @@ class BootstrapFinetune(FinetuneTeleprompter):
return key_to_lm
- def _prepare_finetune_data(self, trace_data: List[Dict[str, Any]], lm: LM, pred_ind: Optional[int] = None):
+ def _prepare_finetune_data(self, trace_data: List[Dict[str, Any]], lm: LM, pred_ind: int | None = None):
# TODO(nit) Log dataset details/size; make logs nicer
if self.metric:
logger.info(f"Collected data for {len(trace_data)} examples")
@@ -211,14 +211,14 @@ def build_call_data_from_trace(
@dataclass
class FailedPrediction:
completion_text: str
- format_reward: Union[float, None] = None
+ format_reward: float | None = None
def bootstrap_trace_data(
program: Module,
dataset: List[Example],
- metric: Optional[Callable] = None,
- num_threads: Optional[int] = None,
+ metric: Callable | None = None,
+ num_threads: int | None = None,
raise_on_error=True,
capture_failed_parses=False,
failure_score: float = 0,
@@ -367,7 +367,7 @@ def prepare_student(student: Module) -> Module:
return student
-def prepare_teacher(student: Module, teacher: Optional[Module] = None) -> Module:
+def prepare_teacher(student: Module, teacher: Module | None = None) -> Module:
if teacher is None:
return student
@@ -389,7 +389,7 @@ def assert_structural_equivalency(program1: object, program2: object):
err = f"Structurally equivalent programs must have the the number of predictors. The number of predictors for the two modules do not match: {num1} != {num2}"
assert num1 == num2, err
- pzip = zip(program1.named_predictors(), program2.named_predictors())
+ pzip = zip(program1.named_predictors(), program2.named_predictors(), strict=False)
for ind, ((name1, pred1), (name2, pred2)) in enumerate(pzip):
err = f"Program predictor names must match at corresponding indices for structural equivalency. The predictor names for the programs do not match at index {ind}: '{name1}' != '{name2}'"
assert name1 == name2, err
diff --git a/dspy/teleprompt/copro_optimizer.py b/dspy/teleprompt/copro_optimizer.py
index 65d09dc6f..e0f4f7174 100644
--- a/dspy/teleprompt/copro_optimizer.py
+++ b/dspy/teleprompt/copro_optimizer.py
@@ -77,7 +77,7 @@ class COPRO(Teleprompter):
self.track_stats = track_stats
def _check_candidates_equal(self, candidate1, candidate2):
- for p1, p2 in zip(candidate1["program"].predictors(), candidate2["program"].predictors()):
+ for p1, p2 in zip(candidate1["program"].predictors(), candidate2["program"].predictors(), strict=False):
if self._get_signature(p1).instructions != self._get_signature(p2).instructions:
return False
*_, p1_last_field = self._get_signature(p1).fields.values()
@@ -191,7 +191,7 @@ class COPRO(Teleprompter):
latest_scores = []
# Go through our module's predictors
- for p_i, (p_old, p_new) in enumerate(zip(module.predictors(), module_clone.predictors())):
+ for p_i, (p_old, p_new) in enumerate(zip(module.predictors(), module_clone.predictors(), strict=False)):
candidates_ = latest_candidates[id(p_old)] # Use the most recently generated candidates for evaluation
if len(module.predictors()) > 1:
# Unless our program has multiple predictors, in which case we need to reevaluate all prompts with
diff --git a/dspy/teleprompt/grpo.py b/dspy/teleprompt/grpo.py
index 7745d264d..c88b16063 100644
--- a/dspy/teleprompt/grpo.py
+++ b/dspy/teleprompt/grpo.py
@@ -1,7 +1,7 @@
import logging
import random
from collections import Counter
-from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union
+from typing import Any, Callable, Dict, List, Literal, Tuple
from dspy.adapters.base import Adapter
from dspy.adapters.chat_adapter import ChatAdapter
@@ -25,10 +25,10 @@ logger = logging.getLogger(__name__)
class GRPO(FinetuneTeleprompter):
def __init__(
self,
- metric: Optional[Callable] = None,
+ metric: Callable | None = None,
multitask: bool = True,
- train_kwargs: Optional[Union[Dict[str, Any], Dict[LM, Dict[str, Any]]]] = None,
- adapter: Optional[Union[Adapter, Dict[LM, Adapter]]] = None,
+ train_kwargs: Dict[str, Any] | Dict[LM, Dict[str, Any]] | None = None,
+ adapter: Adapter | Dict[LM, Adapter] | None = None,
exclude_demos: bool = False,
num_threads: int = 6,
num_train_steps: int = 100,
@@ -40,8 +40,8 @@ class GRPO(FinetuneTeleprompter):
report_train_scores: bool = False,
failure_score: float = 0,
format_failure_score: float = -1,
- variably_invoked_predictor_grouping_mode: Union[Literal["truncate"], Literal["fill"], Literal["ragged"]] = "truncate",
- variably_invoked_predictor_fill_strategy: Optional[Union[Literal["randint"], Literal["max"]]] = None,
+ variably_invoked_predictor_grouping_mode: Literal["truncate"] | Literal["fill"] | Literal["ragged"] = "truncate",
+ variably_invoked_predictor_fill_strategy: Literal["randint"] | Literal["max"] | None = None,
):
super().__init__(train_kwargs=train_kwargs)
self.metric = metric
@@ -235,8 +235,8 @@ class GRPO(FinetuneTeleprompter):
self,
student: Module,
trainset: List[Example],
- teacher: Optional[Union[Module, List[Module]]] = None,
- valset: Optional[List[Example]] = None,
+ teacher: Module | List[Module] | None = None,
+ valset: List[Example] | None = None,
**kwargs,
) -> Module:
logger.info("Starting the GRPO compilation process... The LM(s) for the student program will be updated in place at the end of the training.")
diff --git a/dspy/teleprompt/mipro_optimizer_v2.py b/dspy/teleprompt/mipro_optimizer_v2.py
index fe1225f79..4ab73b600 100644
--- a/dspy/teleprompt/mipro_optimizer_v2.py
+++ b/dspy/teleprompt/mipro_optimizer_v2.py
@@ -5,7 +5,7 @@ import sys
import textwrap
import time
from collections import defaultdict
-from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Tuple
+from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Tuple
import numpy as np
@@ -52,21 +52,21 @@ class MIPROv2(Teleprompter):
def __init__(
self,
metric: Callable,
- prompt_model: Optional[Any] = None,
- task_model: Optional[Any] = None,
- teacher_settings: Optional[dict] = None,
+ prompt_model: Any | None = None,
+ task_model: Any | None = None,
+ teacher_settings: dict | None = None,
max_bootstrapped_demos: int = 4,
max_labeled_demos: int = 4,
- auto: Optional[Literal["light", "medium", "heavy"]] = "light",
- num_candidates: Optional[int] = None,
- num_threads: Optional[int] = None,
- max_errors: Optional[int] = None,
+ auto: Literal["light", "medium", "heavy"] | None = "light",
+ num_candidates: int | None = None,
+ num_threads: int | None = None,
+ max_errors: int | None = None,
seed: int = 9,
init_temperature: float = 0.5,
verbose: bool = False,
track_stats: bool = True,
- log_dir: Optional[str] = None,
- metric_threshold: Optional[float] = None,
+ log_dir: str | None = None,
+ metric_threshold: float | None = None,
):
# Validate 'auto' parameter
allowed_modes = {None, "light", "medium", "heavy"}
@@ -100,11 +100,11 @@ class MIPROv2(Teleprompter):
*,
trainset: List,
teacher: Any = None,
- valset: Optional[List] = None,
- num_trials: Optional[int] = None,
- max_bootstrapped_demos: Optional[int] = None,
- max_labeled_demos: Optional[int] = None,
- seed: Optional[int] = None,
+ valset: List | None = None,
+ num_trials: int | None = None,
+ max_bootstrapped_demos: int | None = None,
+ max_labeled_demos: int | None = None,
+ seed: int | None = None,
minibatch: bool = True,
minibatch_size: int = 35,
minibatch_full_eval_steps: int = 5,
@@ -114,7 +114,7 @@ class MIPROv2(Teleprompter):
tip_aware_proposer: bool = True,
fewshot_aware_proposer: bool = True,
requires_permission_to_run: bool = True,
- provide_traceback: Optional[bool] = None,
+ provide_traceback: bool | None = None,
) -> Any:
effective_max_errors = (
self.max_errors
@@ -263,7 +263,7 @@ class MIPROv2(Teleprompter):
return num_trials, valset, minibatch
- def _set_and_validate_datasets(self, trainset: List, valset: Optional[List]):
+ def _set_and_validate_datasets(self, trainset: List, valset: List | None):
if not trainset:
raise ValueError("Trainset cannot be empty.")
@@ -403,7 +403,7 @@ class MIPROv2(Teleprompter):
print("\nNo input received within 20 seconds. Proceeding with execution...")
return True
- def _bootstrap_fewshot_examples(self, program: Any, trainset: List, seed: int, teacher: Any) -> Optional[List]:
+ def _bootstrap_fewshot_examples(self, program: Any, trainset: List, seed: int, teacher: Any) -> List | None:
logger.info("\n==> STEP 1: BOOTSTRAP FEWSHOT EXAMPLES <==")
if self.max_bootstrapped_demos > 0:
logger.info(
@@ -448,7 +448,7 @@ class MIPROv2(Teleprompter):
self,
program: Any,
trainset: List,
- demo_candidates: Optional[List],
+ demo_candidates: List | None,
view_data_batch_size: int,
program_aware_proposer: bool,
data_aware_proposer: bool,
@@ -500,7 +500,7 @@ class MIPROv2(Teleprompter):
self,
program: Any,
instruction_candidates: Dict[int, List[str]],
- demo_candidates: Optional[List],
+ demo_candidates: List | None,
evaluate: Evaluate,
valset: List,
num_trials: int,
@@ -508,7 +508,7 @@ class MIPROv2(Teleprompter):
minibatch_size: int,
minibatch_full_eval_steps: int,
seed: int,
- ) -> Optional[Any]:
+ ) -> Any | None:
import optuna
# Run optimization
@@ -745,7 +745,7 @@ class MIPROv2(Teleprompter):
self,
candidate_program: Any,
instruction_candidates: Dict[int, List[str]],
- demo_candidates: Optional[List],
+ demo_candidates: List | None,
trial: "optuna.trial.Trial",
trial_logs: Dict,
trial_num: int,
diff --git a/dspy/teleprompt/simba.py b/dspy/teleprompt/simba.py
index 45fce7c69..388744767 100644
--- a/dspy/teleprompt/simba.py
+++ b/dspy/teleprompt/simba.py
@@ -322,7 +322,7 @@ class SIMBA(Teleprompter):
# Build sorted list of {"score", "program"} dicts
assert len(scores) == len(candidate_programs)
- candidate_data = [{"score": s, "program": p} for s, p in zip(scores, candidate_programs)]
+ candidate_data = [{"score": s, "program": p} for s, p in zip(scores, candidate_programs, strict=False)]
candidate_data.sort(key=lambda x: x["score"], reverse=True)
best_idx = scores.index(max(scores)) if scores else 0
diff --git a/dspy/teleprompt/teleprompt.py b/dspy/teleprompt/teleprompt.py
index 74ed78eaf..ad1b8b865 100644
--- a/dspy/teleprompt/teleprompt.py
+++ b/dspy/teleprompt/teleprompt.py
@@ -1,4 +1,4 @@
-from typing import Any, Optional
+from typing import Any
from dspy.primitives import Example, Module
@@ -7,7 +7,7 @@ class Teleprompter:
def __init__(self):
pass
- def compile(self, student: Module, *, trainset: list[Example], teacher: Optional[Module] = None, valset: Optional[list[Example]] = None, **kwargs) -> Module:
+ def compile(self, student: Module, *, trainset: list[Example], teacher: Module | None = None, valset: list[Example] | None = None, **kwargs) -> Module:
"""
Optimize the student program.
diff --git a/dspy/teleprompt/teleprompt_optuna.py b/dspy/teleprompt/teleprompt_optuna.py
index 31724de49..29b877422 100644
--- a/dspy/teleprompt/teleprompt_optuna.py
+++ b/dspy/teleprompt/teleprompt_optuna.py
@@ -35,7 +35,7 @@ class BootstrapFewShotWithOptuna(Teleprompter):
def objective(self, trial):
program2 = self.student.reset_copy()
for (name, compiled_predictor), (_, program2_predictor) in zip(
- self.compiled_teleprompter.named_predictors(), program2.named_predictors(),
+ self.compiled_teleprompter.named_predictors(), program2.named_predictors(), strict=False,
):
all_demos = compiled_predictor.demos
demo_index = trial.suggest_int(f"demo_index_for_{name}", 0, len(all_demos) - 1)
diff --git a/dspy/utils/callback.py b/dspy/utils/callback.py
index 2753c8f84..7b2570552 100644
--- a/dspy/utils/callback.py
+++ b/dspy/utils/callback.py
@@ -3,7 +3,7 @@ import inspect
import logging
import uuid
from contextvars import ContextVar
-from typing import Any, Callable, Dict, Optional
+from typing import Any, Callable, Dict
import dspy
@@ -81,8 +81,8 @@ class BaseCallback:
def on_module_end(
self,
call_id: str,
- outputs: Optional[Any],
- exception: Optional[Exception] = None,
+ outputs: Any | None,
+ exception: Exception | None = None,
):
"""A handler triggered after forward() method of a module (subclass of dspy.Module) is executed.
@@ -113,8 +113,8 @@ class BaseCallback:
def on_lm_end(
self,
call_id: str,
- outputs: Optional[Dict[str, Any]],
- exception: Optional[Exception] = None,
+ outputs: Dict[str, Any] | None,
+ exception: Exception | None = None,
):
"""A handler triggered after __call__ method of dspy.LM instance is executed.
@@ -145,8 +145,8 @@ class BaseCallback:
def on_adapter_format_end(
self,
call_id: str,
- outputs: Optional[Dict[str, Any]],
- exception: Optional[Exception] = None,
+ outputs: Dict[str, Any] | None,
+ exception: Exception | None = None,
):
"""A handler triggered after format() method of an adapter (subclass of dspy.Adapter) is called..
@@ -177,8 +177,8 @@ class BaseCallback:
def on_adapter_parse_end(
self,
call_id: str,
- outputs: Optional[Dict[str, Any]],
- exception: Optional[Exception] = None,
+ outputs: Dict[str, Any] | None,
+ exception: Exception | None = None,
):
"""A handler triggered after parse() method of an adapter (subclass of dspy.Adapter) is called.
@@ -209,8 +209,8 @@ class BaseCallback:
def on_tool_end(
self,
call_id: str,
- outputs: Optional[Dict[str, Any]],
- exception: Optional[Exception] = None,
+ outputs: Dict[str, Any] | None,
+ exception: Exception | None = None,
):
"""A handler triggered after a tool is executed.
@@ -241,8 +241,8 @@ class BaseCallback:
def on_evaluate_end(
self,
call_id: str,
- outputs: Optional[Any],
- exception: Optional[Exception] = None,
+ outputs: Any | None,
+ exception: Exception | None = None,
):
"""A handler triggered after evaluation is executed.
diff --git a/dspy/utils/dummies.py b/dspy/utils/dummies.py
index 26c06f74f..092187ba9 100644
--- a/dspy/utils/dummies.py
+++ b/dspy/utils/dummies.py
@@ -1,6 +1,6 @@
import random
from collections import defaultdict
-from typing import Any, Dict, Union
+from typing import Any, Dict
import numpy as np
@@ -67,7 +67,7 @@ class DummyLM(LM):
"""
- def __init__(self, answers: Union[list[dict[str, str]], dict[str, dict[str, str]]], follow_examples: bool = False):
+ def __init__(self, answers: list[dict[str, str]] | dict[str, dict[str, str]], follow_examples: bool = False):
super().__init__("dummy", "chat", 0.0, 1000, True)
self.answers = answers
if isinstance(answers, list):
@@ -87,7 +87,7 @@ class DummyLM(LM):
# get the output from the last turn that has the output fields as headers
final_input = messages[-1]["content"].split("\n\n")[0]
- for input, output in zip(reversed(messages[:-1]), reversed(messages)):
+ for input, output in zip(reversed(messages[:-1]), reversed(messages), strict=False):
if any(field in output["content"] for field in output_fields) and final_input in input["content"]:
return output["content"]
@@ -172,7 +172,7 @@ class DummyVectorizer:
def _hash(self, gram):
"""Hashes a string using a polynomial hash function."""
h = 1
- for coeff, c in zip(self.coeffs, gram):
+ for coeff, c in zip(self.coeffs, gram, strict=False):
h = h * coeff + ord(c)
h %= self.P
return h % self.max_length
diff --git a/dspy/utils/exceptions.py b/dspy/utils/exceptions.py
index 70b2f443d..1fbb22beb 100644
--- a/dspy/utils/exceptions.py
+++ b/dspy/utils/exceptions.py
@@ -1,4 +1,3 @@
-from typing import Optional
from dspy.signatures.signature import Signature
@@ -11,8 +10,8 @@ class AdapterParseError(Exception):
adapter_name: str,
signature: Signature,
lm_response: str,
- message: Optional[str] = None,
- parsed_result: Optional[str] = None,
+ message: str | None = None,
+ parsed_result: str | None = None,
):
self.adapter_name = adapter_name
self.signature = signature
diff --git a/dspy/utils/mcp.py b/dspy/utils/mcp.py
index de7e08b06..7eda5877d 100644
--- a/dspy/utils/mcp.py
+++ b/dspy/utils/mcp.py
@@ -1,4 +1,4 @@
-from typing import TYPE_CHECKING, Any, Union
+from typing import TYPE_CHECKING, Any
from dspy.adapters.types.tool import Tool, convert_input_schema_to_tool_args
@@ -6,7 +6,7 @@ if TYPE_CHECKING:
import mcp
-def _convert_mcp_tool_result(call_tool_result: "mcp.types.CallToolResult") -> Union[str, list[Any]]:
+def _convert_mcp_tool_result(call_tool_result: "mcp.types.CallToolResult") -> str | list[Any]:
from mcp.types import TextContent
text_contents: list[TextContent] = []
diff --git a/dspy/utils/unbatchify.py b/dspy/utils/unbatchify.py
index 264a0c56c..d89fb0898 100644
--- a/dspy/utils/unbatchify.py
+++ b/dspy/utils/unbatchify.py
@@ -67,7 +67,7 @@ class Unbatchify:
if batch:
try:
outputs = self.batch_fn(batch)
- for output, future in zip(outputs, futures):
+ for output, future in zip(outputs, futures, strict=False):
future.set_result(output)
except Exception as e:
for future in futures:
diff --git a/pyproject.toml b/pyproject.toml
index cfe0760b7..0c4ce7332 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -13,7 +13,7 @@ description = "DSPy"
readme = "README.md"
authors = [{ name = "Omar Khattab", email = "[email protected]" }]
license = { text = "MIT License" }
-requires-python = ">=3.9, <3.14"
+requires-python = ">=3.10, <3.14"
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Science/Research",
@@ -41,8 +41,7 @@ dependencies = [
"cachetools>=5.5.0",
"cloudpickle>=3.0.0",
"rich>=13.7.1",
- "numpy>=1.26.0,<2.2; python_version == '3.9'",
- "numpy>=1.26.0; python_version >= '3.10'",
+ "numpy>=1.26.0",
]
[project.optional-dependencies]
@@ -122,7 +121,7 @@ exclude = [
line-length = 120
indent-width = 4
-target-version = "py39"
+target-version = "py310"
[tool.ruff.lint]
select = [
diff --git a/tests/adapters/test_base_type.py b/tests/adapters/test_base_type.py
index 5a374406b..1776d3192 100644
--- a/tests/adapters/test_base_type.py
+++ b/tests/adapters/test_base_type.py
@@ -1,4 +1,3 @@
-from typing import Optional
import pydantic
@@ -9,8 +8,8 @@ def test_basic_extract_custom_type_from_annotation():
class Event(dspy.BaseType):
event_name: str
start_date_time: str
- end_date_time: Optional[str]
- location: Optional[str]
+ end_date_time: str | None
+ location: str | None
class ExtractEvent(dspy.Signature):
"""Extract all events from the email content."""
@@ -35,8 +34,8 @@ def test_extract_custom_type_from_annotation_with_nested_type():
class Event(dspy.BaseType):
event_name: str
start_date_time: str
- end_date_time: Optional[str]
- location: Optional[str]
+ end_date_time: str | None
+ location: str | None
class EventIdentifier(dspy.BaseType):
model_config = pydantic.ConfigDict(frozen=True) # Make it hashable
diff --git a/tests/adapters/test_tool.py b/tests/adapters/test_tool.py
index a2d6d3c8b..a4ab072dd 100644
--- a/tests/adapters/test_tool.py
+++ b/tests/adapters/test_tool.py
@@ -1,5 +1,5 @@
import asyncio
-from typing import Any, Optional
+from typing import Any
import pytest
from pydantic import BaseModel
@@ -38,19 +38,19 @@ class Address(BaseModel):
class ContactInfo(BaseModel):
email: str
- phone: Optional[str] = None
+ phone: str | None = None
addresses: list[Address]
class UserProfile(BaseModel):
user_id: int
name: str
- age: Optional[int] = None
+ age: int | None = None
contact: ContactInfo
tags: list[str] = []
-def complex_dummy_function(profile: UserProfile, priority: int, notes: Optional[str] = None) -> dict[str, Any]:
+def complex_dummy_function(profile: UserProfile, priority: int, notes: str | None = None) -> dict[str, Any]:
"""Process user profile with complex nested structure.
Args:
@@ -89,7 +89,7 @@ async def async_dummy_with_pydantic(model: DummyModel) -> str:
async def async_complex_dummy_function(
- profile: UserProfile, priority: int, notes: Optional[str] = None
+ profile: UserProfile, priority: int, notes: str | None = None
) -> dict[str, Any]:
"""Process user profile with complex nested structure asynchronously.
diff --git a/tests/primitives/test_module.py b/tests/primitives/test_module.py
index 4e9901d0d..8ca487c15 100644
--- a/tests/primitives/test_module.py
+++ b/tests/primitives/test_module.py
@@ -23,7 +23,7 @@ def test_named_predictors():
module = HopModule()
named_preds = module.named_predictors()
assert len(named_preds) == 2, "Should identify correct number of Predict instances"
- names, preds = zip(*named_preds)
+ names, preds = zip(*named_preds, strict=False)
assert "predict1" in names and "predict2" in names, "Named predictors should include 'predict1' and 'predict2'"
@@ -57,7 +57,7 @@ def test_nested_named_predictors():
module = Hop2Module()
named_preds = module.named_predictors()
assert len(named_preds) == 2
- names, _preds = zip(*named_preds)
+ names, _preds = zip(*named_preds, strict=False)
assert "hop.predict1" in names
assert "hop.predict2" in names
diff --git a/tests/signatures/test_adapter_image.py b/tests/signatures/test_adapter_image.py
index 9223b2bf0..7f20ebb91 100644
--- a/tests/signatures/test_adapter_image.py
+++ b/tests/signatures/test_adapter_image.py
@@ -1,7 +1,7 @@
import os
import tempfile
from io import BytesIO
-from typing import List, Optional
+from typing import List
import pydantic
import pytest
@@ -290,7 +290,7 @@ def test_save_load_pydantic_model():
class ImageModel(pydantic.BaseModel):
image: dspy.Image
- image_list: Optional[List[dspy.Image]] = None
+ image_list: List[dspy.Image] | None = None
output: str
class PydanticSignature(dspy.Signature):
@@ -330,7 +330,7 @@ def test_optional_image_field():
"""Test that optional image fields are not required"""
class OptionalImageSignature(dspy.Signature):
- image: Optional[dspy.Image] = dspy.InputField()
+ image: dspy.Image | None = dspy.InputField()
output: str = dspy.OutputField()
predictor, lm = setup_predictor(OptionalImageSignature, {"output": "Hello"})
diff --git a/tests/utils/test_saving.py b/tests/utils/test_saving.py
index ccc81f891..51cd9fa68 100644
--- a/tests/utils/test_saving.py
+++ b/tests/utils/test_saving.py
@@ -33,7 +33,7 @@ def test_save_custom_model(tmp_path):
assert isinstance(loaded_model, CustomModel)
assert len(model.predictors()) == len(loaded_model.predictors())
- for predictor, loaded_predictor in zip(model.predictors(), loaded_model.predictors()):
+ for predictor, loaded_predictor in zip(model.predictors(), loaded_model.predictors(), strict=False):
assert predictor.signature == loaded_predictor.signature