DSPy: The framework for programming—not prompting—language models
Python
Use collections for type hints (#8513)
TomuHirata873bdc5acb42
diff --git a/docs/docs/learn/programming/signatures.md b/docs/docs/learn/programming/signatures.mdindex 9b9955ede..8261802f8 100644--- a/docs/docs/learn/programming/signatures.md+++ b/docs/docs/learn/programming/signatures.md@@ -172,7 +172,7 @@ Prediction(DSPy signatures support various annotation types:1. **Basic types** like `str`, `int`, `bool`-2. **Typing module types** like `List[str]`, `Dict[str, int]`, `Optional[float]`. `Union[str, int]`+2. **Typing module types** like `list[str]`, `dict[str, int]`, `Optional[float]`. `Union[str, int]`3. **Custom types** defined in your code4. **Dot notation** for nested types with proper configuration5. **Special data types** like `dspy.Image, dspy.History`diff --git a/docs/docs/tutorials/ai_text_game/index.md b/docs/docs/tutorials/ai_text_game/index.mdindex 76c3e4e82..aeef06548 100644--- a/docs/docs/tutorials/ai_text_game/index.md+++ b/docs/docs/tutorials/ai_text_game/index.md@@ -51,8 +51,8 @@ class Player:health: int = 100level: int = 1experience: int = 0- inventory: List[str] = field(default_factory=list)- skills: Dict[str, int] = field(default_factory=lambda: {+ inventory: list[str] = field(default_factory=list)+ skills: dict[str, int] = field(default_factory=lambda: {"strength": 10,"intelligence": 10,"charisma": 10,@@ -80,10 +80,10 @@ class Player:class GameContext:current_location: str = "Village Square"story_progress: int = 0- visited_locations: List[str] = field(default_factory=list)- npcs_met: List[str] = field(default_factory=list)- completed_quests: List[str] = field(default_factory=list)- game_flags: Dict[str, bool] = field(default_factory=dict)+ visited_locations: list[str] = field(default_factory=list)+ npcs_met: list[str] = field(default_factory=list)+ completed_quests: list[str] = field(default_factory=list)+ game_flags: dict[str, bool] = field(default_factory=dict)def add_flag(self, flag: str, value: bool = True):self.game_flags[flag] = value@@ -176,9 +176,9 @@ class StoryGenerator(dspy.Signature):recent_actions: str = dspy.InputField(desc="Player's recent actions")scene_description: str = dspy.OutputField(desc="Vivid description of current scene")- available_actions: List[str] = dspy.OutputField(desc="List of possible player actions")- npcs_present: List[str] = dspy.OutputField(desc="NPCs present in this location")- items_available: List[str] = dspy.OutputField(desc="Items that can be found or interacted with")+ available_actions: list[str] = dspy.OutputField(desc="List of possible player actions")+ npcs_present: list[str] = dspy.OutputField(desc="NPCs present in this location")+ items_available: list[str] = dspy.OutputField(desc="Items that can be found or interacted with")class DialogueGenerator(dspy.Signature):"""Generate NPC dialogue and responses."""@@ -201,8 +201,8 @@ class ActionResolver(dspy.Signature):success: bool = dspy.OutputField(desc="Whether the action succeeded")outcome_description: str = dspy.OutputField(desc="Description of what happened")- stat_changes: Dict[str, int] = dspy.OutputField(desc="Changes to player stats")- items_gained: List[str] = dspy.OutputField(desc="Items gained from this action")+ stat_changes: dict[str, int] = dspy.OutputField(desc="Changes to player stats")+ items_gained: list[str] = dspy.OutputField(desc="Items gained from this action")experience_gained: int = dspy.OutputField(desc="Experience points gained")class GameAI(dspy.Module):@@ -333,7 +333,7 @@ def display_location(context: GameContext, scene: Dict):console.print(Panel(location_panel.strip(), title="Current Location", style="cyan"))-def display_actions(actions: List[str]):+def display_actions(actions: list[str]):"""Display available actions."""action_text = "\n".join([f"{i+1}. {action}" for i, action in enumerate(actions)])console.print(Panel(action_text, title="Available Actions", style="yellow"))diff --git a/docs/docs/tutorials/cache/index.md b/docs/docs/tutorials/cache/index.mdindex 2989b24cc..aafa10a28 100644--- a/docs/docs/tutorials/cache/index.md+++ b/docs/docs/tutorials/cache/index.md@@ -89,15 +89,15 @@ class CustomCache(dspy.clients.Cache):def __init__(self, **kwargs):{write your own constructor}- 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: Optional[list[str]] = None) -> str:{write your logic of computing cache key}- 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: Optional[list[str]] = None) -> Any:{write your cache read logic}def put(self,- request: Dict[str, Any],+ request: dict[str, Any],value: Any,ignored_args_for_cache_key: Optional[list[str]] = None,enable_memory_cache: bool = True,@@ -118,7 +118,7 @@ Let's illustrate this with a practical example. Suppose we want the cache key co```pythonclass CustomCache(dspy.clients.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: Optional[list[str]] = None) -> str:messages = request.get("messages", [])return sha256(ujson.dumps(messages, sort_keys=True).encode()).hexdigest()@@ -164,7 +164,7 @@ dspy.settings.configure(lm=dspy.LM("openai/gpt-4o-mini"))class CustomCache(dspy.clients.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: Optional[list[str]] = None) -> str:messages = request.get("messages", [])return sha256(ujson.dumps(messages, sort_keys=True).encode()).hexdigest()diff --git a/docs/docs/tutorials/email_extraction/index.md b/docs/docs/tutorials/email_extraction/index.mdindex 5664b76f2..a0f9e17bf 100644--- a/docs/docs/tutorials/email_extraction/index.md+++ b/docs/docs/tutorials/email_extraction/index.md@@ -60,7 +60,7 @@ class EmailInsight(BaseModel):email_type: EmailTypeurgency: UrgencyLevelsummary: str- key_entities: List[ExtractedEntity]+ key_entities: list[ExtractedEntity]action_required: booldeadline: Optional[str] = Noneamount: Optional[float] = None@@ -89,7 +89,7 @@ class ExtractEntities(dspy.Signature):email_content: str = dspy.InputField(desc="The full email content including subject and body")email_type: EmailType = dspy.InputField(desc="The classified type of email")- key_entities: List[ExtractedEntity] = dspy.OutputField(desc="List of extracted entities with type, value, and confidence")+ key_entities: list[ExtractedEntity] = dspy.OutputField(desc="List of extracted entities with type, value, and confidence")financial_amount: Optional[float] = dspy.OutputField(desc="Any monetary amounts found (e.g., '$99.99')")important_dates: list[str] = dspy.OutputField(desc="List of important dates found in the email")contact_info: list[str] = dspy.OutputField(desc="Relevant contact information extracted")@@ -100,7 +100,7 @@ class GenerateActionItems(dspy.Signature):email_type: EmailType = dspy.InputField()urgency: UrgencyLevel = dspy.InputField()email_summary: str = dspy.InputField(desc="Brief summary of the email content")- extracted_entities: List[ExtractedEntity] = dspy.InputField(desc="Key entities found in the email")+ extracted_entities: list[ExtractedEntity] = dspy.InputField(desc="Key entities found in the email")action_required: bool = dspy.OutputField(desc="Whether any action is required")action_items: list[str] = dspy.OutputField(desc="List of specific actions needed")@@ -112,7 +112,7 @@ class SummarizeEmail(dspy.Signature):email_subject: str = dspy.InputField()email_body: str = dspy.InputField()- key_entities: List[ExtractedEntity] = dspy.InputField()+ key_entities: list[ExtractedEntity] = dspy.InputField()summary: str = dspy.OutputField(desc="A 2-3 sentence summary of the email's main points")```diff --git a/docs/docs/tutorials/entity_extraction/index.ipynb b/docs/docs/tutorials/entity_extraction/index.ipynbindex f948e9ef8..8391791c9 100644--- a/docs/docs/tutorials/entity_extraction/index.ipynb+++ b/docs/docs/tutorials/entity_extraction/index.ipynb@@ -111,15 +111,15 @@" os.environ[\"HF_DATASETS_CACHE\"] = temp_dir\n"," return load_dataset(\"conll2003\", trust_remote_code=True)\n","\n",- "def extract_people_entities(data_row: Dict[str, Any]) -> List[str]:\n",+ "def extract_people_entities(data_row: dict[str, Any]) -> list[str]:\n"," \"\"\"\n"," Extracts entities referring to people from a row of the CoNLL-2003 dataset.\n"," \n"," Args:\n",- " data_row (Dict[str, Any]): A row from the dataset containing tokens and NER tags.\n",+ " data_row (dict[str, Any]): A row from the dataset containing tokens and NER tags.\n"," \n"," Returns:\n",- " List[str]: List of tokens tagged as people.\n",+ " list[str]: List of tokens tagged as people.\n"," \"\"\"\n"," return [\n"," token\n",@@ -127,7 +127,7 @@" if ner_tag in (1, 2) # CoNLL entity codes 1 and 2 refer to people\n"," ]\n","\n",- "def prepare_dataset(data_split, start: int, end: int) -> List[dspy.Example]:\n",+ "def prepare_dataset(data_split, start: int, end: int) -> list[dspy.Example]:\n"," \"\"\"\n"," Prepares a sliced dataset split for use with DSPy.\n"," \n",@@ -137,7 +137,7 @@" end (int): Ending index of the slice.\n"," \n"," Returns:\n",- " List[dspy.Example]: List of DSPy Examples with tokens and expected labels.\n",+ " list[dspy.Example]: List of DSPy Examples with tokens and expected labels.\n"," \"\"\"\n"," return [\n"," dspy.Example(\n",diff --git a/docs/docs/tutorials/llms_txt_generation/index.md b/docs/docs/tutorials/llms_txt_generation/index.mdindex 3fe9c419c..e844e9b54 100644--- a/docs/docs/tutorials/llms_txt_generation/index.md+++ b/docs/docs/tutorials/llms_txt_generation/index.md@@ -31,7 +31,7 @@ class AnalyzeRepository(dspy.Signature):readme_content: str = dspy.InputField(desc="README.md content")project_purpose: str = dspy.OutputField(desc="Main purpose and goals of the project")- key_concepts: List[str] = dspy.OutputField(desc="List of important concepts and terminology")+ key_concepts: list[str] = dspy.OutputField(desc="List of important concepts and terminology")architecture_overview: str = dspy.OutputField(desc="High-level architecture description")class AnalyzeCodeStructure(dspy.Signature):@@ -39,17 +39,17 @@ class AnalyzeCodeStructure(dspy.Signature):file_tree: str = dspy.InputField(desc="Repository file structure")package_files: str = dspy.InputField(desc="Key package and configuration files")- important_directories: List[str] = dspy.OutputField(desc="Key directories and their purposes")- entry_points: List[str] = dspy.OutputField(desc="Main entry points and important files")+ important_directories: list[str] = dspy.OutputField(desc="Key directories and their purposes")+ entry_points: list[str] = dspy.OutputField(desc="Main entry points and important files")development_info: str = dspy.OutputField(desc="Development setup and workflow information")class GenerateLLMsTxt(dspy.Signature):"""Generate a comprehensive llms.txt file from analyzed repository information."""project_purpose: str = dspy.InputField()- key_concepts: List[str] = dspy.InputField()+ key_concepts: list[str] = dspy.InputField()architecture_overview: str = dspy.InputField()- important_directories: List[str] = dspy.InputField()- entry_points: List[str] = dspy.InputField()+ important_directories: list[str] = dspy.InputField()+ entry_points: list[str] = dspy.InputField()development_info: str = dspy.InputField()usage_examples: str = dspy.InputField(desc="Common usage patterns and examples")diff --git a/docs/docs/tutorials/sample_code_generation/index.md b/docs/docs/tutorials/sample_code_generation/index.mdindex 78c867676..a5ccc1ebf 100644--- a/docs/docs/tutorials/sample_code_generation/index.md+++ b/docs/docs/tutorials/sample_code_generation/index.md@@ -48,7 +48,7 @@ class DocumentationFetcher:self.html_converter.ignore_links = Falseself.html_converter.ignore_images = True- def fetch_url(self, url: str) -> Dict[str, str]:+ def fetch_url(self, url: str) -> dict[str, str]:"""Fetch content from a single URL."""for attempt in range(self.max_retries):try:@@ -86,7 +86,7 @@ class DocumentationFetcher:return {"url": url, "title": "Failed", "content": "", "success": False}- def fetch_documentation(self, urls: List[str]) -> List[Dict[str, str]]:+ def fetch_documentation(self, urls: list[str]) -> list[dict[str, str]]:"""Fetch documentation from multiple URLs."""results = []@@ -102,11 +102,11 @@ class LibraryAnalyzer(dspy.Signature):library_name: str = dspy.InputField(desc="Name of the library to analyze")documentation_content: str = dspy.InputField(desc="Combined documentation content")- core_concepts: List[str] = dspy.OutputField(desc="Main concepts and components")- common_patterns: List[str] = dspy.OutputField(desc="Common usage patterns")- key_methods: List[str] = dspy.OutputField(desc="Important methods and functions")+ core_concepts: list[str] = dspy.OutputField(desc="Main concepts and components")+ common_patterns: list[str] = dspy.OutputField(desc="Common usage patterns")+ key_methods: list[str] = dspy.OutputField(desc="Important methods and functions")installation_info: str = dspy.OutputField(desc="Installation and setup information")- code_examples: List[str] = dspy.OutputField(desc="Example code snippets found")+ code_examples: list[str] = dspy.OutputField(desc="Example code snippets found")class CodeGenerator(dspy.Signature):"""Generate code examples for specific use cases using the target library."""@@ -116,8 +116,8 @@ class CodeGenerator(dspy.Signature):code_example: str = dspy.OutputField(desc="Complete, working code example")explanation: str = dspy.OutputField(desc="Step-by-step explanation of the code")- best_practices: List[str] = dspy.OutputField(desc="Best practices and tips")- imports_needed: List[str] = dspy.OutputField(desc="Required imports and dependencies")+ best_practices: list[str] = dspy.OutputField(desc="Best practices and tips")+ imports_needed: list[str] = dspy.OutputField(desc="Required imports and dependencies")class DocumentationLearningAgent(dspy.Module):"""Agent that learns from documentation URLs and generates code examples."""@@ -128,10 +128,10 @@ class DocumentationLearningAgent(dspy.Module):self.analyze_docs = dspy.ChainOfThought(LibraryAnalyzer)self.generate_code = dspy.ChainOfThought(CodeGenerator)self.refine_code = dspy.ChainOfThought(- "code, feedback -> improved_code: str, changes_made: List[str]"+ "code, feedback -> improved_code: str, changes_made: list[str]")- def learn_from_urls(self, library_name: str, doc_urls: List[str]) -> Dict:+ def learn_from_urls(self, library_name: str, doc_urls: list[str]) -> Dict:"""Learn about a library from its documentation URLs."""print(f"📚 Learning about {library_name} from {len(doc_urls)} URLs...")@@ -198,7 +198,7 @@ agent = DocumentationLearningAgent()## Step 2: Learning from Documentation URLs```python-def learn_library_from_urls(library_name: str, documentation_urls: List[str]) -> Dict:+def learn_library_from_urls(library_name: str, documentation_urls: list[str]) -> Dict:"""Learn about any library from its documentation URLs."""try:@@ -318,7 +318,7 @@ streamlit_examples = generate_examples_for_library(streamlit_info, "Streamlit")## Step 4: Interactive Library Learning Function```python-def learn_any_library(library_name: str, documentation_urls: List[str], use_cases: List[str] = None):+def learn_any_library(library_name: str, documentation_urls: list[str], use_cases: list[str] = None):"""Learn any library from its documentation and generate examples."""if use_cases is None:@@ -602,7 +602,7 @@ def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):raise HTTPException(status_code=401, detail="Invalid token")@app.post("/login")-async def login(username: str, password: str) -> Dict[str, str]:+async def login(username: str, password: str) -> dict[str, str]:# In production, verify against databaseif username == "admin" and password == "secret":token_data = {"sub": username, "exp": datetime.utcnow() + timedelta(hours=24)}@@ -611,7 +611,7 @@ async def login(username: str, password: str) -> Dict[str, str]:raise HTTPException(status_code=401, detail="Invalid credentials")@app.get("/protected")-async def protected_route(current_user: str = Depends(verify_token)) -> Dict[str, str]:+async def protected_route(current_user: str = Depends(verify_token)) -> dict[str, str]:return {"message": f"Hello {current_user}! This is a protected route."}if __name__ == "__main__":diff --git a/dspy/adapters/base.py b/dspy/adapters/base.pyindex 3e0372c0e..eaea79563 100644--- a/dspy/adapters/base.py+++ b/dspy/adapters/base.py@@ -1,5 +1,5 @@import logging-from typing import TYPE_CHECKING, Any, Type, get_origin+from typing import TYPE_CHECKING, Any, get_originimport json_repairimport litellm@@ -32,7 +32,7 @@ class Adapter:self,lm: "LM",lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],inputs: dict[str, Any],) -> dict[str, Any]:if self.use_native_function_calling:@@ -67,8 +67,8 @@ class Adapter:def _call_postprocess(self,- processed_signature: Type[Signature],- original_signature: Type[Signature],+ processed_signature: type[Signature],+ original_signature: type[Signature],outputs: list[dict[str, Any]],) -> list[dict[str, Any]]:values = []@@ -117,7 +117,7 @@ class Adapter:self,lm: "LM",lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -131,7 +131,7 @@ class Adapter:self,lm: "LM",lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -143,7 +143,7 @@ class Adapter:def format(self,- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -221,7 +221,7 @@ class Adapter:messages = split_message_content_for_custom_types(messages)return messages- def format_field_description(self, signature: Type[Signature]) -> str:+ def format_field_description(self, signature: type[Signature]) -> str:"""Format the field description for the system message.This method formats the field description for the system message. It should return a string that contains@@ -235,7 +235,7 @@ class Adapter:"""raise NotImplementedError- def format_field_structure(self, signature: Type[Signature]) -> str:+ def format_field_structure(self, signature: type[Signature]) -> str:"""Format the field structure for the system message.This method formats the field structure for the system message. It should return a string that dictates the@@ -247,7 +247,7 @@ class Adapter:"""raise NotImplementedError- def format_task_description(self, signature: Type[Signature]) -> str:+ def format_task_description(self, signature: type[Signature]) -> str:"""Format the task description for the system message.This method formats the task description for the system message. In most cases this is just a thin wrapper@@ -263,7 +263,7 @@ class Adapter:def format_user_message_content(self,- signature: Type[Signature],+ signature: type[Signature],inputs: dict[str, Any],prefix: str = "",suffix: str = "",@@ -287,7 +287,7 @@ class Adapter:def format_assistant_message_content(self,- signature: Type[Signature],+ signature: type[Signature],outputs: dict[str, Any],missing_field_message: str | None = None,) -> str:@@ -306,7 +306,7 @@ class Adapter:"""raise NotImplementedError- def format_demos(self, signature: Type[Signature], demos: list[dict[str, Any]]) -> list[dict[str, Any]]:+ def format_demos(self, signature: type[Signature], demos: list[dict[str, Any]]) -> list[dict[str, Any]]:"""Format the few-shot examples.This method formats the few-shot examples as multiturn messages.@@ -368,13 +368,13 @@ class Adapter:return messages- def _get_history_field_name(self, signature: Type[Signature]) -> bool:+ def _get_history_field_name(self, signature: type[Signature]) -> bool:for name, field in signature.input_fields.items():if field.annotation == History:return namereturn None- def _get_tool_call_input_field_name(self, signature: Type[Signature]) -> bool:+ def _get_tool_call_input_field_name(self, signature: type[Signature]) -> bool:for name, field in signature.input_fields.items():# Look for annotation `list[dspy.Tool]` or `dspy.Tool`origin = get_origin(field.annotation)@@ -384,7 +384,7 @@ class Adapter:return namereturn None- def _get_tool_call_output_field_name(self, signature: Type[Signature]) -> bool:+ def _get_tool_call_output_field_name(self, signature: type[Signature]) -> bool:for name, field in signature.output_fields.items():if field.annotation == ToolCalls:return name@@ -392,7 +392,7 @@ class Adapter:def format_conversation_history(self,- signature: Type[Signature],+ signature: type[Signature],history_field_name: str,inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -433,7 +433,7 @@ class Adapter:return messages- def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:+ def parse(self, signature: type[Signature], completion: str) -> dict[str, Any]:"""Parse the LM output into a dictionary of the output fields.This method parses the LM output into a dictionary of the output fields.diff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.pyindex 4ace4214d..c856c2d51 100644--- a/dspy/adapters/chat_adapter.py+++ b/dspy/adapters/chat_adapter.py@@ -1,6 +1,6 @@import reimport textwrap-from typing import Any, Dict, NamedTuple, Type+from typing import Any, NamedTuplefrom litellm import ContextWindowExceededErrorfrom pydantic.fields import FieldInfo@@ -30,7 +30,7 @@ class ChatAdapter(Adapter):self,lm: LM,lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -50,7 +50,7 @@ class ChatAdapter(Adapter):self,lm: LM,lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -66,13 +66,13 @@ class ChatAdapter(Adapter):raise ereturn await JSONAdapter().acall(lm, lm_kwargs, signature, demos, inputs)- def format_field_description(self, signature: Type[Signature]) -> str:+ def format_field_description(self, signature: type[Signature]) -> str:return (f"Your input fields are:\n{get_field_description_string(signature.input_fields)}\n"f"Your output fields are:\n{get_field_description_string(signature.output_fields)}")- def format_field_structure(self, signature: Type[Signature]) -> str:+ def format_field_structure(self, signature: type[Signature]) -> str:"""`ChatAdapter` requires input and output fields to be in their own sections, with section header using markers`[[ ## field_name ## ]]`. An arbitrary field `completed` ([[ ## completed ## ]]) is added to the end of the@@ -81,7 +81,7 @@ class ChatAdapter(Adapter):parts = []parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")- def format_signature_fields_for_instructions(fields: Dict[str, FieldInfo]):+ def format_signature_fields_for_instructions(fields: dict[str, FieldInfo]):return self.format_field_with_value(fields_with_values={FieldInfoWithName(name=field_name, info=field_info): translate_field_type(field_name, field_info)@@ -94,14 +94,14 @@ class ChatAdapter(Adapter):parts.append("[[ ## completed ## ]]\n")return "\n\n".join(parts).strip()- def format_task_description(self, signature: Type[Signature]) -> str:+ def format_task_description(self, signature: type[Signature]) -> str:instructions = textwrap.dedent(signature.instructions)objective = ("\n" + " " * 8).join([""] + instructions.splitlines())return f"In adhering to this structure, your objective is: {objective}"def format_user_message_content(self,- signature: Type[Signature],+ signature: type[Signature],inputs: dict[str, Any],prefix: str = "",suffix: str = "",@@ -122,7 +122,7 @@ class ChatAdapter(Adapter):messages.append(suffix)return "\n\n".join(messages).strip()- def user_message_output_requirements(self, signature: Type[Signature]) -> str:+ def user_message_output_requirements(self, signature: type[Signature]) -> str:"""Returns a simplified format reminder for the language model.In chat-based interactions, language models may lose track of the required output format@@ -153,7 +153,7 @@ class ChatAdapter(Adapter):def format_assistant_message_content(self,- signature: Type[Signature],+ signature: type[Signature],outputs: dict[str, Any],missing_field_message=None,) -> str:@@ -166,7 +166,7 @@ class ChatAdapter(Adapter):assistant_message_content += "\n\n[[ ## completed ## ]]\n"return assistant_message_content- def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:+ def parse(self, signature: type[Signature], completion: str) -> dict[str, Any]:sections = [(None, [])]for line in completion.splitlines():@@ -203,7 +203,7 @@ class ChatAdapter(Adapter):return fields- def format_field_with_value(self, fields_with_values: Dict[FieldInfoWithName, Any]) -> str:+ def format_field_with_value(self, fields_with_values: dict[FieldInfoWithName, Any]) -> str:"""Formats the values of the specified fields according to the field's DSPy type (input or output),annotation (e.g. str, int, etc.), and the type of the value itself. Joins the formatted values@@ -225,7 +225,7 @@ class ChatAdapter(Adapter):def format_finetune_data(self,- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],outputs: dict[str, Any],diff --git a/dspy/adapters/json_adapter.py b/dspy/adapters/json_adapter.pyindex 51470e2ec..898125f00 100644--- a/dspy/adapters/json_adapter.py+++ b/dspy/adapters/json_adapter.py@@ -1,6 +1,6 @@import jsonimport logging-from typing import Any, Dict, Type, get_origin+from typing import Any, get_originimport json_repairimport litellm@@ -62,7 +62,7 @@ class JSONAdapter(ChatAdapter):self,lm: LM,lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -85,7 +85,7 @@ class JSONAdapter(ChatAdapter):self,lm: LM,lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -102,11 +102,11 @@ class JSONAdapter(ChatAdapter):lm_kwargs["response_format"] = {"type": "json_object"}return await super().acall(lm, lm_kwargs, signature, demos, inputs)- def format_field_structure(self, signature: Type[Signature]) -> str:+ def format_field_structure(self, signature: type[Signature]) -> str:parts = []parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")- def format_signature_fields_for_instructions(fields: Dict[str, FieldInfo], role: str):+ def format_signature_fields_for_instructions(fields: dict[str, FieldInfo], role: str):return self.format_field_with_value(fields_with_values={FieldInfoWithName(name=field_name, info=field_info): translate_field_type(field_name, field_info)@@ -121,7 +121,7 @@ class JSONAdapter(ChatAdapter):parts.append(format_signature_fields_for_instructions(signature.output_fields, role="assistant"))return "\n\n".join(parts).strip()- def user_message_output_requirements(self, signature: Type[Signature]) -> str:+ def user_message_output_requirements(self, signature: type[Signature]) -> str:def type_info(v):return (f" (must be formatted as a valid Python {get_annotation_name(v.annotation)})"@@ -136,7 +136,7 @@ class JSONAdapter(ChatAdapter):def format_assistant_message_content(self,- signature: Type[Signature],+ signature: type[Signature],outputs: dict[str, Any],missing_field_message=None,) -> str:@@ -146,7 +146,7 @@ class JSONAdapter(ChatAdapter):}return self.format_field_with_value(fields_with_values, role="assistant")- def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:+ def parse(self, signature: type[Signature], completion: str) -> dict[str, Any]:pattern = r"\{(?:[^{}]|(?R))*\}"match = regex.search(pattern, completion, regex.DOTALL)if match:@@ -178,7 +178,7 @@ class JSONAdapter(ChatAdapter):return fields- def format_field_with_value(self, fields_with_values: Dict[FieldInfoWithName, Any], role: str = "user") -> str:+ def format_field_with_value(self, fields_with_values: dict[FieldInfoWithName, Any], role: str = "user") -> str:"""Formats the values of the specified fields according to the field's DSPy type (input or output),annotation (e.g. str, int, etc.), and the type of the value itself. Joins the formatted values@@ -201,7 +201,7 @@ class JSONAdapter(ChatAdapter):return json.dumps(serialize_for_json(d), indent=2)def format_finetune_data(- self, signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any], outputs: dict[str, Any]+ self, signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any], outputs: dict[str, Any]) -> dict[str, list[Any]]:# TODO: implement format_finetune_data method in JSONAdapterraise NotImplementedErrordiff --git a/dspy/adapters/two_step_adapter.py b/dspy/adapters/two_step_adapter.pyindex 27b8d5e72..9e3f7bbb1 100644--- a/dspy/adapters/two_step_adapter.py+++ b/dspy/adapters/two_step_adapter.py@@ -1,4 +1,4 @@-from typing import Any, Type+from typing import Anyimport json_repair@@ -46,7 +46,7 @@ class TwoStepAdapter(Adapter):self.extraction_model = extraction_modeldef format(- self, signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]+ self, signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]:"""Format a prompt for the first stage with the main LM.@@ -107,7 +107,7 @@ class TwoStepAdapter(Adapter):self,lm: "LM",lm_kwargs: dict[str, Any],- signature: Type[Signature],+ signature: type[Signature],demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:@@ -176,7 +176,7 @@ class TwoStepAdapter(Adapter):def format_user_message_content(self,- signature: Type[Signature],+ signature: type[Signature],inputs: dict[str, Any],prefix: str = "",suffix: str = "",@@ -192,7 +192,7 @@ class TwoStepAdapter(Adapter):def format_assistant_message_content(self,- signature: Type[Signature],+ signature: type[Signature],outputs: dict[str, Any],missing_field_message: str | None = None,) -> str:@@ -206,8 +206,8 @@ class TwoStepAdapter(Adapter):def _create_extractor_signature(self,- original_signature: Type[Signature],- ) -> Type[Signature]:+ original_signature: type[Signature],+ ) -> type[Signature]:"""Create a new signature containing a new 'text' input field and all output fields.Args:diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.pyindex 8e4aeda72..b3e6b4510 100644--- a/dspy/adapters/types/tool.py+++ b/dspy/adapters/types/tool.py@@ -1,6 +1,6 @@import asyncioimport inspect-from typing import TYPE_CHECKING, Any, Callable, Tuple, Type, get_origin, get_type_hints+from typing import TYPE_CHECKING, Any, Callable, Type, get_origin, get_type_hintsfrom jsonschema import ValidationError, validatefrom pydantic import BaseModel, TypeAdapter, create_model@@ -338,7 +338,7 @@ def _resolve_json_schema_reference(schema: dict) -> dict:def convert_input_schema_to_tool_args(schema: dict[str, Any],-) -> Tuple[dict[str, Any], dict[str, Type], dict[str, str]]:+) -> tuple[dict[str, Any], dict[str, Type], dict[str, str]]:"""Convert an input json schema to tool arguments compatible with DSPy Tool.Args:diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.pyindex 476765da2..b96ab24a2 100644--- a/dspy/adapters/utils.py+++ b/dspy/adapters/utils.py@@ -3,7 +3,7 @@ import enumimport inspectimport jsonfrom collections.abc import Mapping-from typing import Any, List, Literal, Union, get_args, get_origin+from typing import Any, Literal, Union, get_args, get_originimport json_repairimport pydantic@@ -217,9 +217,9 @@ def get_field_description_string(fields: dict) -> str:return "\n".join(field_descriptions).strip()-def _format_input_list_field_value(value: List[Any]) -> str:+def _format_input_list_field_value(value: list[Any]) -> str:"""- Formats the value of an input field of type List[Any].+ Formats the value of an input field of type list[Any].Args:value: The value of the list-type input field.diff --git a/dspy/adapters/xml_adapter.py b/dspy/adapters/xml_adapter.pyindex b75dfe90b..5c83cc41d 100644--- a/dspy/adapters/xml_adapter.py+++ b/dspy/adapters/xml_adapter.py@@ -1,5 +1,5 @@import re-from typing import Any, Dict, Type+from typing import Anyfrom dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithNamefrom dspy.adapters.utils import format_field_value@@ -12,20 +12,20 @@ class XMLAdapter(ChatAdapter):super().__init__(callbacks)self.field_pattern = re.compile(r"<(?P<name>\w+)>((?P<content>.*?))</\1>", re.DOTALL)- def format_field_with_value(self, fields_with_values: Dict[FieldInfoWithName, Any]) -> str:+ def format_field_with_value(self, fields_with_values: dict[FieldInfoWithName, Any]) -> str:output = []for field, field_value in fields_with_values.items():formatted = format_field_value(field_info=field.info, value=field_value)output.append(f"<{field.name}>\n{formatted}\n</{field.name}>")return "\n\n".join(output).strip()- def user_message_output_requirements(self, signature: Type[Signature]) -> str:+ def user_message_output_requirements(self, signature: type[Signature]) -> str:message = "Respond with the corresponding output fields wrapped in XML tags"message += ", then ".join(f"`<{f}>`" for f in signature.output_fields)message += ", and then end with the `<completed>` tag."return message- def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:+ def parse(self, signature: type[Signature], completion: str) -> dict[str, Any]:fields = {}for match in self.field_pattern.finditer(completion):name = match.group("name")diff --git a/dspy/clients/cache.py b/dspy/clients/cache.pyindex af3bc47c6..7ba9b0e57 100644--- a/dspy/clients/cache.py+++ b/dspy/clients/cache.py@@ -4,7 +4,7 @@ import loggingimport threadingfrom functools import wrapsfrom hashlib import sha256-from typing import Any, Dict+from typing import Anyimport cloudpickleimport pydantic@@ -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: list[str] | None = 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 JSONrepresentation. 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: list[str] | None = 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:@@ -122,7 +122,7 @@ class Cache:def put(self,- request: Dict[str, Any],+ request: dict[str, Any],value: Any,ignored_args_for_cache_key: list[str] | None = None,enable_memory_cache: bool = True,diff --git a/dspy/clients/databricks.py b/dspy/clients/databricks.pyindex fea86fe5c..6829a3c11 100644--- a/dspy/clients/databricks.py+++ b/dspy/clients/databricks.py@@ -2,7 +2,7 @@ import loggingimport osimport reimport time-from typing import TYPE_CHECKING, Any, Dict, List+from typing import TYPE_CHECKING, Anyimport requestsimport ujson@@ -168,9 +168,9 @@ class DatabricksProvider(Provider):def finetune(job: TrainingJobDatabricks,model: str,- train_data: List[Dict[str, Any]],+ train_data: list[dict[str, Any]],train_data_format: TrainDataFormat | str | None = "chat",- train_kwargs: Dict[str, Any] | None = None,+ train_kwargs: dict[str, Any] | None = None,) -> str:if isinstance(train_data_format, str):if train_data_format == "chat":@@ -243,7 +243,7 @@ class DatabricksProvider(Provider):return f"databricks/{job.endpoint_name}"@staticmethod- def upload_data(train_data: List[Dict[str, Any]], databricks_unity_catalog_path: str, data_format: TrainDataFormat):+ def upload_data(train_data: list[dict[str, Any]], databricks_unity_catalog_path: str, data_format: TrainDataFormat):logger.info("Uploading finetuning data to Databricks Unity Catalog...")file_path = _save_data_to_local_file(train_data, data_format)@@ -303,7 +303,7 @@ def _create_directory_in_databricks_unity_catalog(w: "WorkspaceClient", databriclogger.info(f"Successfully created directory {databricks_unity_catalog_path} in Databricks Unity Catalog!")-def _save_data_to_local_file(train_data: List[Dict[str, Any]], data_format: TrainDataFormat):+def _save_data_to_local_file(train_data: list[dict[str, Any]], data_format: TrainDataFormat):import uuidfile_name = f"finetuning_{uuid.uuid4()}.jsonl"@@ -322,7 +322,7 @@ def _save_data_to_local_file(train_data: List[Dict[str, Any]], data_format: Traireturn file_path-def _validate_chat_data(data: Dict[str, Any]):+def _validate_chat_data(data: dict[str, Any]):if "messages" not in data:raise ValueError("Each finetuning data must be a dict with a 'messages' key when `task=CHAT_COMPLETION`, but "@@ -344,7 +344,7 @@ def _validate_chat_data(data: Dict[str, Any]):)-def _validate_completion_data(data: Dict[str, Any]):+def _validate_completion_data(data: dict[str, Any]):if "prompt" not in data:raise ValueError("Each finetuning data must be a dict with a 'prompt' key when `task=INSTRUCTION_FINETUNE`, but "diff --git a/dspy/clients/lm.py b/dspy/clients/lm.pyindex e79a73168..019859130 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -2,7 +2,7 @@ import loggingimport osimport reimport threading-from typing import Any, Dict, List, Literal, cast+from typing import Any, Literal, castimport litellmfrom anyio.streams.memory import MemoryObjectSendStream@@ -34,7 +34,7 @@ class LM(BaseLM):max_tokens: int = 4000,cache: bool = True,cache_in_memory: bool = True,- callbacks: List[BaseCallback] | None = None,+ callbacks: list[BaseCallback] | None = None,num_retries: int = 3,provider: Provider | None = None,finetuning_model: str | None = None,@@ -173,17 +173,17 @@ class LM(BaseLM):settings.usage_tracker.add_usage(self.model, dict(results.usage))return results- def launch(self, launch_kwargs: Dict[str, Any] | None = None):+ def launch(self, launch_kwargs: dict[str, Any] | None = None):self.provider.launch(self, launch_kwargs)- def kill(self, launch_kwargs: Dict[str, Any] | None = 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: list[dict[str, Any]],train_data_format: TrainDataFormat | None,- train_kwargs: Dict[str, Any] | None = None,+ train_kwargs: dict[str, Any] | None = None,) -> TrainingJob:from dspy import settings as settings@@ -255,8 +255,8 @@ class LM(BaseLM):def _get_stream_completion_fn(- request: Dict[str, Any],- cache_kwargs: Dict[str, Any],+ request: dict[str, Any],+ cache_kwargs: dict[str, Any],sync=True,):stream = dspy.settings.send_stream@@ -272,7 +272,7 @@ def _get_stream_completion_fn(if dspy.settings.track_usage:request["stream_options"] = {"include_usage": True}- async def stream_completion(request: Dict[str, Any], cache_kwargs: Dict[str, Any]):+ async def stream_completion(request: dict[str, Any], cache_kwargs: dict[str, Any]):response = await litellm.acompletion(cache=cache_kwargs,stream=True,@@ -300,7 +300,7 @@ def _get_stream_completion_fn(return async_stream_completion-def litellm_completion(request: Dict[str, Any], num_retries: int, cache: Dict[str, Any] | None = 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: Dict[streturn stream_completion()-def litellm_text_completion(request: Dict[str, Any], num_retries: int, cache: Dict[str, Any] | None = 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: Di)-async def alitellm_completion(request: Dict[str, Any], num_retries: int, cache: Dict[str, Any] | None = 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: Dict[str, Any] | None = 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.pyindex 7a314b4c1..c937ee6e5 100644--- a/dspy/clients/lm_local.py+++ b/dspy/clients/lm_local.py@@ -6,7 +6,7 @@ import stringimport subprocessimport threadingimport time-from typing import TYPE_CHECKING, Any, Dict, List+from typing import TYPE_CHECKING, Anyimport requests@@ -26,7 +26,7 @@ class LocalProvider(Provider):self.TrainingJob = TrainingJob@staticmethod- def launch(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):+ def launch(lm: "LM", launch_kwargs: dict[str, Any] | None = None):try:import sglang # noqa: F401except ImportError:@@ -118,7 +118,7 @@ class LocalProvider(Provider):lm.thread = thread@staticmethod- def kill(lm: "LM", launch_kwargs: Dict[str, Any] | None = None):+ def kill(lm: "LM", launch_kwargs: dict[str, Any] | None = None):from sglang.utils import terminate_processif not hasattr(lm, "process"):@@ -136,9 +136,9 @@ class LocalProvider(Provider):def finetune(job: TrainingJob,model: str,- train_data: List[Dict[str, Any]],+ train_data: list[dict[str, Any]],train_data_format: TrainDataFormat | None,- train_kwargs: Dict[str, Any] | None = None,+ train_kwargs: dict[str, Any] | None = None,) -> str:if model.startswith("openai/"):model = model[7:]diff --git a/dspy/clients/lm_local_arbor.py b/dspy/clients/lm_local_arbor.pyindex 70a8b1303..813a2aa8f 100644--- a/dspy/clients/lm_local_arbor.py+++ b/dspy/clients/lm_local_arbor.py@@ -1,6 +1,6 @@import timefrom datetime import datetime-from typing import TYPE_CHECKING, Any, Dict, List, TypedDict+from typing import TYPE_CHECKING, Any, TypedDictimport openaiimport requests@@ -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: TrainDataFormat | str | None):+ 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@@ -237,7 +237,7 @@ class ArborProvider(Provider):self.ReinforceJob = ArborReinforceJob@staticmethod- def launch(lm: "LM", launch_kwargs: Dict[str, Any] | None = 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: Dict[str, Any] | None = None):+ def kill(lm: "LM", launch_kwargs: dict[str, Any] | None = None):api_base = lm.kwargs["api_base"]response = requests.post(@@ -295,9 +295,9 @@ class ArborProvider(Provider):def finetune(job: ArborTrainingJob,model: str,- train_data: List[Dict[str, Any]],+ train_data: list[dict[str, Any]],train_data_format: TrainDataFormat | None,- train_kwargs: Dict[str, Any] | None = 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@@ -337,7 +337,7 @@ class ArborProvider(Provider):return ArborProvider._add_provider_prefix(model)@staticmethod- def does_job_exist(job_id: str, training_kwargs: Dict[str, Any]) -> bool:+ def does_job_exist(job_id: str, training_kwargs: dict[str, Any]) -> bool:try:original_base_url = openai.base_urlopenai.base_url = ArborProvider._get_arbor_base_api()@@ -348,7 +348,7 @@ class ArborProvider(Provider):return False@staticmethod- def does_file_exist(file_id: str, training_kwargs: Dict[str, Any]) -> bool:+ def does_file_exist(file_id: str, training_kwargs: dict[str, Any]) -> bool:try:original_base_url = openai.base_urlopenai.base_url = ArborProvider._get_arbor_base_api()@@ -367,7 +367,7 @@ class ArborProvider(Provider):]@staticmethod- def get_training_status(job_id: str, training_kwargs: Dict[str, Any]) -> TrainingStatus:+ def get_training_status(job_id: str, training_kwargs: dict[str, Any]) -> TrainingStatus:provider_status_to_training_status = {"validating_files": TrainingStatus.pending,"queued": TrainingStatus.pending,@@ -412,7 +412,7 @@ class ArborProvider(Provider):raise ValueError(err_msg)@staticmethod- def upload_data(data_path: str, training_kwargs: Dict[str, Any]) -> str:+ def upload_data(data_path: str, training_kwargs: dict[str, Any]) -> str:original_base_url = openai.base_urlopenai.base_url = ArborProvider._get_arbor_base_api()provider_file = openai.files.create(@@ -424,7 +424,7 @@ class ArborProvider(Provider):return provider_file.id@staticmethod- def _start_remote_training(train_file_id: str, model: str, train_kwargs: Dict[str, Any]) -> str:+ def _start_remote_training(train_file_id: str, model: str, train_kwargs: dict[str, Any]) -> str:train_kwargs = train_kwargs or {}original_base_url = openai.base_urlopenai.base_url = ArborProvider._get_arbor_base_api()@@ -439,7 +439,7 @@ class ArborProvider(Provider):@staticmethoddef wait_for_job(job: TrainingJob,- training_kwargs: Dict[str, Any],+ training_kwargs: dict[str, Any],poll_frequency: int = 20,):done = False@@ -477,7 +477,7 @@ class ArborProvider(Provider):done = ArborProvider.is_terminal_training_status(job.status())@staticmethod- def get_trained_model(job, training_kwargs: Dict[str, Any]):+ def get_trained_model(job, training_kwargs: dict[str, Any]):status = job.status()if status != TrainingStatus.succeeded:err_msg = f"Job status is {status}."diff --git a/dspy/clients/openai.py b/dspy/clients/openai.pyindex adb5ddeab..c740ed19d 100644--- a/dspy/clients/openai.py+++ b/dspy/clients/openai.py@@ -1,7 +1,7 @@import reimport timefrom datetime import datetime-from typing import Any, Dict, List+from typing import Anyimport openai@@ -121,9 +121,9 @@ class OpenAIProvider(Provider):def finetune(job: TrainingJobOpenAI,model: str,- train_data: List[Dict[str, Any]],+ train_data: list[dict[str, Any]],train_data_format: TrainDataFormat | None,- train_kwargs: Dict[str, Any] | None = 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: Dict[str, Any] | None = 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.pyindex 01684bf76..5425136c2 100644--- a/dspy/clients/provider.py+++ b/dspy/clients/provider.py@@ -1,7 +1,7 @@from abc import abstractmethodfrom concurrent.futures import Futurefrom threading import Thread-from typing import TYPE_CHECKING, Any, Dict, List+from typing import TYPE_CHECKING, Anyfrom dspy.clients.utils_finetune import TrainDataFormat@@ -14,9 +14,9 @@ class TrainingJob(Future):self,thread: Thread | None = None,model: str | None = None,- train_data: List[Dict[str, Any]] | None = None,+ train_data: list[dict[str, Any]] | None = None,train_data_format: TrainDataFormat | None = None,- train_kwargs: Dict[str, Any] | None = None,+ train_kwargs: dict[str, Any] | None = None,):self.thread = threadself.model = model@@ -36,7 +36,7 @@ class TrainingJob(Future):class ReinforceJob:- def __init__(self, lm: "LM", train_kwargs: Dict[str, Any] | None = None):+ def __init__(self, lm: "LM", train_kwargs: dict[str, Any] | None = None):self.lm = lmself.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: TrainDataFormat | str | None = 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: Dict[str, Any] | None = 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: Dict[str, Any] | None = 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@@ -101,8 +101,8 @@ class Provider:def finetune(job: TrainingJob,model: str,- train_data: List[Dict[str, Any]],+ train_data: list[dict[str, Any]],train_data_format: TrainDataFormat | str | None,- train_kwargs: Dict[str, Any] | None = None,+ train_kwargs: dict[str, Any] | None = None,) -> str:raise NotImplementedErrordiff --git a/dspy/clients/utils_finetune.py b/dspy/clients/utils_finetune.pyindex de8be77f3..ea234fc37 100644--- a/dspy/clients/utils_finetune.py+++ b/dspy/clients/utils_finetune.py@@ -1,6 +1,6 @@import osfrom enum import Enum-from typing import Any, Dict, List, Literal, TypedDict+from typing import Any, Literal, TypedDictimport ujson@@ -35,12 +35,12 @@ class MessageAssistant(TypedDict):class GRPOChatData(TypedDict):- messages: List[Message]+ messages: list[Message]completion: MessageAssistantreward: float-GRPOGroup = List[GRPOChatData]+GRPOGroup = list[GRPOChatData]def infer_data_format(adapter: Adapter) -> str:@@ -64,7 +64,7 @@ def write_lines(file_path, data):def save_data(- data: List[Dict[str, Any]],+ data: list[dict[str, Any]],) -> str:from datasets.fingerprint import Hasher@@ -82,7 +82,7 @@ def save_data(def validate_data_format(- data: List[Dict[str, Any]],+ data: list[dict[str, Any]],data_format: TrainDataFormat,):find_err_funcs = {@@ -116,7 +116,7 @@ def validate_data_format(raise ValueError(err)-def find_data_errors_completion(data_dict: Dict[str, str]) -> str | None:+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]) -> str | None:# 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]) -> str | None:+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]) -> str | None:return f"Error in message at index {ind}: {err}"-def find_data_error_chat_message(message: Dict[str, Any]) -> str | None:+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.pyindex 9050dd7aa..559553b2b 100644--- a/dspy/datasets/dataloader.py+++ b/dspy/datasets/dataloader.py@@ -1,6 +1,6 @@import randomfrom collections.abc import Mapping-from typing import TYPE_CHECKING, List, Tuple+from typing import TYPE_CHECKINGimport dspyfrom dspy.datasets.dataset import Dataset@@ -17,10 +17,10 @@ class DataLoader(Dataset):self,dataset_name: str,*args,- input_keys: Tuple[str] = (),- fields: Tuple[str] | None = None,+ input_keys: tuple[str] = (),+ fields: tuple[str] | None = None,**kwargs,- ) -> 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,9 +63,9 @@ class DataLoader(Dataset):def from_csv(self,file_path: str,- fields: List[str] | None = None,- input_keys: Tuple[str] = (),- ) -> List[dspy.Example]:+ fields: list[str] | None = None,+ input_keys: tuple[str] = (),+ ) -> list[dspy.Example]:from datasets import load_datasetdataset = load_dataset("csv", data_files=file_path)["train"]@@ -78,7 +78,7 @@ class DataLoader(Dataset):def from_pandas(self,df: "pd.DataFrame",- fields: List[str] | None = None,+ fields: list[str] | None = None,input_keys: tuple[str] = (),) -> list[dspy.Example]:if fields is None:@@ -91,9 +91,9 @@ class DataLoader(Dataset):def from_json(self,file_path: str,- fields: List[str] | None = None,- input_keys: Tuple[str] = (),- ) -> List[dspy.Example]:+ fields: list[str] | None = None,+ input_keys: tuple[str] = (),+ ) -> list[dspy.Example]:from datasets import load_datasetdataset = load_dataset("json", data_files=file_path)["train"]@@ -106,9 +106,9 @@ class DataLoader(Dataset):def from_parquet(self,file_path: str,- fields: List[str] | None = None,- input_keys: Tuple[str] = (),- ) -> List[dspy.Example]:+ fields: list[str] | None = None,+ input_keys: tuple[str] = (),+ ) -> list[dspy.Example]:from datasets import load_datasetdataset = load_dataset("parquet", data_files=file_path)["train"]@@ -118,7 +118,7 @@ class DataLoader(Dataset):return [dspy.Example({field: row[field] for field in fields}).with_inputs(*input_keys) for row in dataset]- def from_rm(self, num_samples: int, fields: List[str], input_keys: List[str]) -> List[dspy.Example]:+ def from_rm(self, num_samples: int, fields: list[str], input_keys: list[str]) -> list[dspy.Example]:try:rm = dspy.settings.rmtry:@@ -137,11 +137,11 @@ class DataLoader(Dataset):def sample(self,- dataset: List[dspy.Example],+ dataset: list[dspy.Example],n: int,*args,**kwargs,- ) -> List[dspy.Example]:+ ) -> list[dspy.Example]:if not isinstance(dataset, list):raise ValueError(f"Invalid dataset provided of type {type(dataset)}. Please provide a list of `dspy.Example`s."@@ -151,11 +151,11 @@ class DataLoader(Dataset):def train_test_split(self,- dataset: List[dspy.Example],+ dataset: list[dspy.Example],train_size: int | float = 0.75,test_size: int | float | None = None,random_state: int | None = None,- ) -> Mapping[str, List[dspy.Example]]:+ ) -> 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.pyindex 90b2850e8..d6c01fb8b 100644--- a/dspy/dsp/colbertv2.py+++ b/dspy/dsp/colbertv2.py@@ -1,4 +1,4 @@-from typing import Any, List+from typing import Anyimport requests@@ -75,11 +75,11 @@ colbertv2_post_request = colbertv2_post_request_v2_wrappedclass ColBERTv2RetrieverLocal:- def __init__(self, passages: List[str], colbert_config=None, load_only: bool = False):+ def __init__(self, passages: list[str], colbert_config=None, load_only: bool = False):"""Colbertv2 retriever moduleArgs:- passages (List[str]): list of passages+ passages (list[str]): list of passagescolbert_config (ColBERTConfig, optional): colbert config for building and searching. Defaults to None.load_only (bool, optional): whether to load the index or build and then load. Defaults to False."""@@ -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: List[str] | None = None):+ def forward(self, query: str, passages: list[str] | None = None):assert len(passages) > 0, "Passages should not be empty"import numpy as npdiff --git a/dspy/evaluate/evaluate.py b/dspy/evaluate/evaluate.pyindex 0fdec0b6e..3203b7293 100644--- a/dspy/evaluate/evaluate.py+++ b/dspy/evaluate/evaluate.py@@ -1,7 +1,7 @@import importlibimport loggingimport types-from typing import TYPE_CHECKING, Any, Callable, List, Tuple+from typing import TYPE_CHECKING, Any, Callableif TYPE_CHECKING:import pandas as pd@@ -51,7 +51,7 @@ class EvaluationResult(Prediction):- score: An float value (e.g., 67.30) representing the overall performance- results: a list of (example, prediction, score) tuples for each example in devset"""- def __init__(self, score: float, results: list[Tuple["dspy.Example", "dspy.Example", Any]]):+ def __init__(self, score: float, results: list[tuple["dspy.Example", "dspy.Example", Any]]):super().__init__(score=score, results=results)def __repr__(self):@@ -68,7 +68,7 @@ class Evaluate:def __init__(self,*,- devset: List["dspy.Example"],+ devset: list["dspy.Example"],metric: Callable | None = None,num_threads: int | None = None,display_progress: bool = False,@@ -80,7 +80,7 @@ class Evaluate:):"""Args:- devset (List[dspy.Example]): the evaluation dataset.+ devset (list[dspy.Example]): the evaluation dataset.metric (Callable): The metric function to use for evaluation.num_threads (Optional[int]): The number of threads to use for parallel evaluation.display_progress (bool): Whether to display progress during evaluation.@@ -105,7 +105,7 @@ class Evaluate:self,program: "dspy.Module",metric: Callable | None = None,- devset: List["dspy.Example"] | None = None,+ devset: list["dspy.Example"] | None = None,num_threads: int | None = None,display_progress: bool | None = None,display_table: bool | int | None = None,@@ -115,7 +115,7 @@ class Evaluate:Args:program (dspy.Module): The DSPy program to evaluate.metric (Callable): The metric function to use for evaluation. if not provided, use `self.metric`.- devset (List[dspy.Example]): the evaluation dataset. if not provided, use `self.devset`.+ devset (list[dspy.Example]): the evaluation dataset. if not provided, use `self.devset`.num_threads (Optional[int]): The number of threads to use for parallel evaluation. if not provided, use`self.num_threads`.display_progress (bool): Whether to display progress during evaluation. if not provided, use@@ -193,7 +193,7 @@ class Evaluate:def _construct_result_table(- self, results: list[Tuple["dspy.Example", "dspy.Example", Any]], metric_name: str+ self, results: list[tuple["dspy.Example", "dspy.Example", Any]], metric_name: str) -> "pd.DataFrame":"""Construct a pandas DataFrame from the specified result list.diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.pyindex 95e2e6f2b..96afef858 100644--- a/dspy/predict/chain_of_thought.py+++ b/dspy/predict/chain_of_thought.py@@ -1,4 +1,4 @@-from typing import Any, Type+from typing import Anyfrom pydantic.fields import FieldInfo@@ -10,9 +10,9 @@ from dspy.signatures.signature import Signature, ensure_signatureclass ChainOfThought(Module):def __init__(self,- signature: str | Type[Signature],+ signature: str | type[Signature],rationale_field: FieldInfo | None = None,- rationale_field_type: Type = str,+ rationale_field_type: type = str,**config: dict[str, Any],):"""diff --git a/dspy/predict/code_act.py b/dspy/predict/code_act.pyindex d85b3e993..f880917c4 100644--- a/dspy/predict/code_act.py+++ b/dspy/predict/code_act.py@@ -1,6 +1,6 @@import inspectimport logging-from typing import Callable, Type+from typing import Callableimport dspyfrom 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: str | Type[Signature], tools: list[Callable], max_iters: int = 5, interpreter: PythonInterpreter | None = 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.pyindex b0de0251f..5c183885e 100644--- a/dspy/predict/parallel.py+++ b/dspy/predict/parallel.py@@ -1,5 +1,5 @@import threading-from typing import Any, List, Tuple+from typing import Anyfrom dspy.dsp.utils.settings import settingsfrom dspy.primitives.example import Example@@ -30,7 +30,7 @@ class Parallel:self.failed_examples = []self.exceptions = []- def forward(self, exec_pairs: List[Tuple[Any, Example]], num_threads: int | None = 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_threadsexecutor = ParallelExecutor(diff --git a/dspy/predict/predict.py b/dspy/predict/predict.pyindex bec1e4720..a87523715 100644--- a/dspy/predict/predict.py+++ b/dspy/predict/predict.py@@ -1,6 +1,5 @@import loggingimport random-from typing import Typefrom pydantic import BaseModel@@ -18,7 +17,7 @@ logger = logging.getLogger(__name__)class Predict(Module, Parameter):- def __init__(self, signature: str | Type[Signature], callbacks: list[BaseCallback] | None = 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.pyindex 86fb5fee4..0296bf522 100644--- a/dspy/predict/program_of_thought.py+++ b/dspy/predict/program_of_thought.py@@ -1,7 +1,6 @@import jsonimport loggingimport re-from typing import Typeimport dspyfrom dspy.primitives.module import Module@@ -27,7 +26,7 @@ class ProgramOfThought(Module):```"""- def __init__(self, signature: str | Type[Signature], max_iters: int = 3, interpreter: PythonInterpreter | None = 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/react.py b/dspy/predict/react.pyindex 212f7b216..42c259c14 100644--- a/dspy/predict/react.py+++ b/dspy/predict/react.py@@ -1,5 +1,5 @@import logging-from typing import TYPE_CHECKING, Any, Callable, Literal, Type+from typing import TYPE_CHECKING, Any, Callable, Literalfrom litellm import ContextWindowExceededError@@ -15,7 +15,7 @@ if TYPE_CHECKING:class ReAct(Module):- def __init__(self, signature: Type["Signature"], tools: list[Callable], max_iters: int = 10):+ def __init__(self, signature: type["Signature"], tools: list[Callable], max_iters: int = 10):"""ReAct stands for "Reasoning and Acting," a popular paradigm for building tool-using agents.In this approach, the language model is iteratively provided with a list of tools and hasdiff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.pyindex dccecb30b..66eb12603 100644--- a/dspy/primitives/python_interpreter.py+++ b/dspy/primitives/python_interpreter.py@@ -3,7 +3,7 @@ import osimport subprocessfrom os import PathLikefrom types import TracebackType-from typing import Any, Dict, List+from typing import Anyclass InterpreterError(RuntimeError):@@ -27,11 +27,11 @@ class PythonInterpreter:def __init__(self,- 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,+ 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:"""@@ -141,7 +141,7 @@ class PythonInterpreter:)raise InterpreterError(install_instructions) from e- def _inject_variables(self, code: str, variables: Dict[str, Any]) -> str:+ def _inject_variables(self, code: str, variables: dict[str, Any]) -> str:# Insert Python assignments for each variable at the top of the codeinjected_lines = []for key, value in variables.items():@@ -168,7 +168,7 @@ class PythonInterpreter:def execute(self,code: str,- variables: Dict[str, Any] | None = None,+ variables: dict[str, Any] | None = None,) -> Any:variables = variables or {}code = self._inject_variables(code, variables)@@ -232,7 +232,7 @@ class PythonInterpreter:def __call__(self,code: str,- variables: Dict[str, Any] | None = 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.pyindex bca5d541e..dc4966bf2 100644--- a/dspy/retrievers/databricks_rm.py+++ b/dspy/retrievers/databricks_rm.py@@ -2,7 +2,7 @@ import jsonimport osfrom dataclasses import dataclassfrom importlib.util import find_spec-from typing import Any, Dict, List+from typing import Anyimport requests@@ -15,10 +15,10 @@ _databricks_sdk_installed = find_spec("databricks.sdk") is not None@dataclassclass Document:page_content: str- metadata: Dict[str, Any]+ metadata: dict[str, Any]type: str- def to_dict(self) -> Dict[str, Any]:+ def to_dict(self) -> dict[str, Any]:return {"page_content": self.page_content,"metadata": self.metadata,@@ -88,7 +88,7 @@ class DatabricksRM(dspy.Retrieve):databricks_token: str | None = None,databricks_client_id: str | None = None,databricks_client_secret: str | None = None,- columns: List[str] | None = None,+ columns: list[str] | None = None,filters_json: str | None = None,k: int = 3,docs_id_column_name: str = "id",@@ -111,7 +111,7 @@ class DatabricksRM(dspy.Retrieve):the token is resolved from the current environment (DATABRICKS_CLIENT_ID).databricks_client_secret (str): Databricks service principal secret. If not specified,the endpoint is resolved from the current environment (DATABRICKS_CLIENT_SECRET).- columns (Optional[List[str]]): Extra column names to include in response,+ columns (Optional[list[str]]): Extra column names to include in response,in addition to the document id and text columns specified by``docs_id_column_name`` and ``text_column_name``.filters_json (Optional[str]): A JSON string specifying additional query filters.@@ -172,11 +172,11 @@ class DatabricksRM(dspy.Retrieve):"you must install the mlflow Python library. Please install mlflow via `pip install mlflow`.")- def _extract_doc_ids(self, item: Dict[str, Any]) -> str:+ def _extract_doc_ids(self, item: dict[str, Any]) -> str:"""Extracts the document id from a search resultArgs:- item: Dict[str, Any]: a record from the search results.+ item: dict[str, Any]: a record from the search results.Returns:str: document id."""@@ -185,13 +185,13 @@ class DatabricksRM(dspy.Retrieve):return docs_dict["document_id"]return item[self.docs_id_column_name]- def _get_extra_columns(self, item: Dict[str, Any]) -> Dict[str, Any]:+ def _get_extra_columns(self, item: dict[str, Any]) -> dict[str, Any]:"""Extracts search result column values, excluding the "text" and not "id" columnsArgs:- item: Dict[str, Any]: a record from the search results.+ item: dict[str, Any]: a record from the search results.Returns:- Dict[str, Any]: Search result column values, excluding the "text", "id" and "uri" columns.+ dict[str, Any]: Search result column values, excluding the "text", "id" and "uri" columns."""extra_columns = {k: v@@ -207,16 +207,16 @@ class DatabricksRM(dspy.Retrieve):def forward(self,- query: str | List[float],+ query: str | list[float],query_type: str = "ANN",filters_json: str | None = None,- ) -> dspy.Prediction | List[Dict[str, Any]]:+ ) -> dspy.Prediction | list[dict[str, Any]]:"""Retrieve documents from a Databricks Mosaic AI Vector Search Index that are relevant to thespecified query.Args:- query (Union[str, List[float]]): The query text or numeric query vector for which to+ query (Union[str, list[float]]): The query text or numeric query vector for which toretrieve relevant documents.query_type (str): The type of search query to perform against the Databricks VectorSearch Index. Must be either 'ANN' (approximate nearest neighbor) or 'HYBRID'@@ -326,16 +326,16 @@ class DatabricksRM(dspy.Retrieve):def _query_via_databricks_sdk(index_name: str,k: int,- columns: List[str],+ columns: list[str],query_type: str,query_text: str | None,- query_vector: List[float] | 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]:+ ) -> dict[str, Any]:"""Query a Databricks Vector Search Index via the Databricks SDK.Assumes that the databricks-sdk Python library is installed.@@ -343,10 +343,10 @@ class DatabricksRM(dspy.Retrieve):Args:index_name (str): Name of the Databricks vector search index to queryk (int): Number of relevant documents to retrieve.- columns (List[str]): Column names to include in response.+ columns (list[str]): Column names to include in response.query_text (Optional[str]): Text query for which to find relevant documents. Exactlyone of query_text or query_vector must be specified.- query_vector (Optional[List[float]]): Numeric query vector for which to find relevant+ query_vector (Optional[list[float]]): Numeric query vector for which to find relevantdocuments. Exactly one of query_text or query_vector must be specified.filters_json (Optional[str]): JSON string representing additional query filters.databricks_token (str): Databricks authentication token. If not specified,@@ -359,7 +359,7 @@ class DatabricksRM(dspy.Retrieve):the endpoint is resolved from the current environment (DATABRICKS_CLIENT_SECRET).Returns:Returns:- Dict[str, Any]: Parsed JSON response from the Databricks Vector Search Index query.+ dict[str, Any]: Parsed JSON response from the Databricks Vector Search Index query."""from databricks.sdk import WorkspaceClient@@ -397,31 +397,31 @@ class DatabricksRM(dspy.Retrieve):def _query_via_requests(index_name: str,k: int,- columns: List[str],+ columns: list[str],databricks_token: str,databricks_endpoint: str,query_type: str,query_text: str | None,- query_vector: List[float] | None,+ query_vector: list[float] | None,filters_json: str | None,- ) -> Dict[str, Any]:+ ) -> dict[str, Any]:"""Query a Databricks Vector Search Index via the Python requests library.Args:index_name (str): Name of the Databricks vector search index to queryk (int): Number of relevant documents to retrieve.- columns (List[str]): Column names to include in response.+ columns (list[str]): Column names to include in response.databricks_token (str): Databricks authentication token.databricks_endpoint (str): Databricks index endpoint url.query_text (Optional[str]): Text query for which to find relevant documents. Exactlyone of query_text or query_vector must be specified.- query_vector (Optional[List[float]]): Numeric query vector for which to find relevant+ query_vector (Optional[list[float]]): Numeric query vector for which to find relevantdocuments. Exactly one of query_text or query_vector must be specified.filters_json (Optional[str]): JSON string representing additional query filters.Returns:- Dict[str, Any]: Parsed JSON response from the Databricks Vector Search Index query.+ dict[str, Any]: Parsed JSON response from the Databricks Vector Search Index query."""if (query_text, query_vector).count(None) != 1:raise ValueError("Exactly one of query_text or query_vector must be specified.")diff --git a/dspy/retrievers/embeddings.py b/dspy/retrievers/embeddings.pyindex ee17c83ca..9b9b31be7 100644--- a/dspy/retrievers/embeddings.py+++ b/dspy/retrievers/embeddings.py@@ -1,4 +1,4 @@-from typing import Any, List+from typing import Anyimport numpy as np@@ -10,10 +10,10 @@ from dspy.utils.unbatchify import Unbatchifyclass Embeddings:def __init__(self,- corpus: List[str],+ corpus: list[str],embedder,k: int = 5,- callbacks: List[Any] | None = None,+ callbacks: list[Any] | None = None,cache: bool = False,brute_force_threshold: int = 20_000,normalize: bool = True,@@ -40,7 +40,7 @@ class Embeddings:passages, indices = self.search_fn(query)return dspy.Prediction(passages=passages, indices=indices)- def _batch_forward(self, queries: List[str]):+ def _batch_forward(self, queries: list[str]):q_embeds = self.embedder(queries)q_embeds = self._normalize(q_embeds) if self.normalize else q_embedsdiff --git a/dspy/retrievers/retrieve.py b/dspy/retrievers/retrieve.pyindex 3dda45397..ef69d7a4b 100644--- a/dspy/retrievers/retrieve.py+++ b/dspy/retrievers/retrieve.py@@ -1,5 +1,4 @@import random-from typing import Listfrom dspy.predict.parameter import Parameterfrom dspy.primitives.prediction import Prediction@@ -46,7 +45,7 @@ class Retrieve(Parameter):query: str,k: int | None = None,**kwargs,- ) -> List[str] | Prediction | List[Prediction]:+ ) -> list[str] | Prediction | list[Prediction]:k = k if k is not None else self.kimport dspydiff --git a/dspy/retrievers/weaviate_rm.py b/dspy/retrievers/weaviate_rm.pyindex 15e895516..381919254 100644--- a/dspy/retrievers/weaviate_rm.py+++ b/dspy/retrievers/weaviate_rm.py@@ -1,4 +1,3 @@-from typing import Listimport dspyfrom dspy.dsp.utils import dotdict@@ -71,11 +70,11 @@ class WeaviateRM(dspy.Retrieve):super().__init__(k=k)- def forward(self, query_or_queries: str | List[str], k: int | None = 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:- query_or_queries (Union[str, List[str]]): The query or queries to search for.+ query_or_queries (Union[str, list[str]]): The query or queries to search for.k (Optional[int]): The number of top passages to retrieve. Defaults to self.k.kwargs :@@ -111,7 +110,7 @@ class WeaviateRM(dspy.Retrieve):return passages- def get_objects(self, num_samples: int, fields: List[str]) -> List[dict]:+ def get_objects(self, num_samples: int, fields: list[str]) -> list[dict]:"""Get objects from Weaviate using the cursor API."""if self._client_type == "WeaviateClient":objects = []diff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.pyindex 1b92e60aa..236a774dd 100644--- a/dspy/signatures/signature.py+++ b/dspy/signatures/signature.py@@ -23,7 +23,7 @@ import sysimport typesimport typingfrom copy import deepcopy-from typing import Any, Dict, Tuple, Type+from typing import Anyfrom pydantic import BaseModel, Field, create_modelfrom pydantic.fields import FieldInfo@@ -226,7 +226,7 @@ class SignatureMeta(type(BaseModel)):Uses the form:Signature(question, context -> answerquestion: str = InputField(desc="..."),- context: List[str] = InputField(desc="..."),+ context: list[str] = InputField(desc="..."),answer: int = OutputField(desc="..."),)."""@@ -244,11 +244,11 @@ class Signature(BaseModel, metaclass=SignatureMeta):# for any signature that doesn't define it's own instructions.@classmethod- def with_instructions(cls, instructions: str) -> Type["Signature"]:+ def with_instructions(cls, instructions: str) -> type["Signature"]:return Signature(cls.fields, instructions)@classmethod- def with_updated_fields(cls, name: str, type_: Type | None = 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@@ -274,15 +274,15 @@ class Signature(BaseModel, metaclass=SignatureMeta):return Signature(fields_copy, cls.instructions)@classmethod- def prepend(cls, name, field, type_=None) -> Type["Signature"]:+ def prepend(cls, name, field, type_=None) -> type["Signature"]:return cls.insert(0, name, field, type_)@classmethod- def append(cls, name, field, type_=None) -> Type["Signature"]:+ def append(cls, name, field, type_=None) -> type["Signature"]:return cls.insert(-1, name, field, type_)@classmethod- def delete(cls, name) -> Type["Signature"]:+ def delete(cls, name) -> type["Signature"]:fields = dict(cls.fields)fields.pop(name, None)@@ -290,7 +290,7 @@ class Signature(BaseModel, metaclass=SignatureMeta):return Signature(fields, cls.instructions)@classmethod- def insert(cls, index: int, name: str, field, type_: Type | None = 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 typeif type_ is None:@@ -355,7 +355,7 @@ class Signature(BaseModel, metaclass=SignatureMeta):return signature_copy-def ensure_signature(signature: str | Type[Signature], instructions=None) -> Type[Signature]:+def ensure_signature(signature: str | type[Signature], instructions=None) -> type[Signature]:if signature is None:return Noneif isinstance(signature, str):@@ -366,11 +366,11 @@ def ensure_signature(signature: str | Type[Signature], instructions=None) -> Typdef make_signature(- signature: str | Dict[str, Tuple[type, FieldInfo]],+ signature: str | dict[str, tuple[type, FieldInfo]],instructions: str | None = None,signature_name: str = "StringSignature",- custom_types: Dict[str, Type] | None = None,-) -> Type[Signature]:+ custom_types: dict[str, type] | None = None,+) -> type[Signature]:"""Create a new Signature subclass with the specified fields and instructions.Args:@@ -449,7 +449,7 @@ def make_signature()-def _parse_signature(signature: str, names=None) -> Dict[str, Tuple[Type, Field]]:+def _parse_signature(signature: str, names=None) -> dict[str, tuple[type, Field]]:if signature.count("->") != 1:raise ValueError(f"Invalid signature format: '{signature}', must contain exactly one '->'.")@@ -464,7 +464,7 @@ def _parse_signature(signature: str, names=None) -> Dict[str, Tuple[Type, Field]return fields-def _parse_field_string(field_string: str, names=None) -> Dict[str, str]:+def _parse_field_string(field_string: str, names=None) -> dict[str, str]:"""Extract the field name and type from field string in the string-based Signature.It takes a string like "x: int, y: str" and returns a dictionary mapping field names to their types.@@ -482,11 +482,11 @@ def _parse_type_node(node, names=None) -> Any:"""Recursively parse an AST node representing a type annotation.This function converts Python's Abstract Syntax Tree (AST) nodes into actual Python types.- It's used to parse type annotations in signature strings like "x: List[int] -> y: str".+ It's used to parse type annotations in signature strings like "x: list[int] -> y: str".Examples:- For "x: int", the AST node represents 'int' and returns the int type- - For "x: List[str]", it processes a subscript node to return typing.List[str]+ - For "x: list[str]", it processes a subscript node to return typing.list[str]- For "x: Optional[int]", it handles the Union type to return Optional[int]- For "x: MyModule.CustomType", it processes attribute access to return the actual type@@ -495,7 +495,7 @@ def _parse_type_node(node, names=None) -> Any:Common node types include:- ast.Name: Simple types like 'int', 'str'- ast.Attribute: Nested types like 'typing.List'- - ast.Subscript: Generic types like 'List[int]'+ - ast.Subscript: Generic types like 'list[int]'names: Optional dictionary mapping type names to their actual type objects.Defaults to Python's typing module contents plus NoneType.diff --git a/dspy/streaming/messages.py b/dspy/streaming/messages.pyindex 34c264b69..9a73f19b8 100644--- a/dspy/streaming/messages.py+++ b/dspy/streaming/messages.py@@ -1,7 +1,7 @@import asyncioimport concurrent.futuresfrom dataclasses import dataclass-from typing import Any, Dict+from typing import Anyfrom asyncer import syncify@@ -69,7 +69,7 @@ class StatusMessageProvider:```"""- def tool_start_status_message(self, instance: Any, inputs: Dict[str, Any]):+ def tool_start_status_message(self, instance: Any, inputs: dict[str, Any]):"""Status message before a `dspy.Tool` is called."""return f"Calling tool {instance.name}..."@@ -77,7 +77,7 @@ class StatusMessageProvider:"""Status message after a `dspy.Tool` is called."""return "Tool calling finished! Querying the LLM with tool calling results..."- def module_start_status_message(self, instance: Any, inputs: Dict[str, Any]):+ def module_start_status_message(self, instance: Any, inputs: dict[str, Any]):"""Status message before a `dspy.Module` or `dspy.Predict` is called."""pass@@ -85,7 +85,7 @@ class StatusMessageProvider:"""Status message after a `dspy.Module` or `dspy.Predict` is called."""pass- def lm_start_status_message(self, instance: Any, inputs: Dict[str, Any]):+ def lm_start_status_message(self, instance: Any, inputs: dict[str, Any]):"""Status message before a `dspy.LM` is called."""pass@@ -102,7 +102,7 @@ class StatusStreamingCallback(BaseCallback):self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):stream = settings.send_streamif stream is None or instance.name == "finish":@@ -115,7 +115,7 @@ class StatusStreamingCallback(BaseCallback):def on_tool_end(self,call_id: str,- outputs: Dict[str, Any] | None,+ outputs: dict[str, Any] | None,exception: Exception | None = None,):stream = settings.send_stream@@ -130,7 +130,7 @@ class StatusStreamingCallback(BaseCallback):self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):stream = settings.send_streamif stream is None:@@ -143,7 +143,7 @@ class StatusStreamingCallback(BaseCallback):def on_lm_end(self,call_id: str,- outputs: Dict[str, Any] | None,+ outputs: dict[str, Any] | None,exception: Exception | None = None,):stream = settings.send_stream@@ -158,7 +158,7 @@ class StatusStreamingCallback(BaseCallback):self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):stream = settings.send_streamif stream is None:@@ -171,7 +171,7 @@ class StatusStreamingCallback(BaseCallback):def on_module_end(self,call_id: str,- outputs: Dict[str, Any] | None,+ outputs: dict[str, Any] | None,exception: Exception | None = None,):stream = settings.send_streamdiff --git a/dspy/streaming/streamify.py b/dspy/streaming/streamify.pyindex ab2201d4a..5376d286e 100644--- a/dspy/streaming/streamify.py+++ b/dspy/streaming/streamify.py@@ -4,7 +4,7 @@ import loggingimport threadingfrom asyncio import iscoroutinefunctionfrom queue import Queue-from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generator, List+from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generatorimport litellmimport ujson@@ -27,7 +27,7 @@ if TYPE_CHECKING:def streamify(program: "Module",status_message_provider: StatusMessageProvider | None = None,- stream_listeners: List[StreamListener] | 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.pyindex 91489e207..98fd4aca7 100644--- a/dspy/streaming/streaming_listener.py+++ b/dspy/streaming/streaming_listener.py@@ -1,7 +1,7 @@import refrom collections import defaultdictfrom queue import Queue-from typing import TYPE_CHECKING, Any, List+from typing import TYPE_CHECKING, Anyfrom litellm import ModelResponseStream@@ -199,7 +199,7 @@ class StreamListener:)-def find_predictor_for_stream_listeners(program: "Module", stream_listeners: List[StreamListener]):+def find_predictor_for_stream_listeners(program: "Module", stream_listeners: list[StreamListener]):"""Find the predictor for each stream listener.This is a utility function to automatically find the predictor for each stream listener. It is used when somediff --git a/dspy/teleprompt/avatar_optimizer.py b/dspy/teleprompt/avatar_optimizer.pyindex 97278edd7..ddba74e5f 100644--- a/dspy/teleprompt/avatar_optimizer.py+++ b/dspy/teleprompt/avatar_optimizer.py@@ -1,7 +1,7 @@from concurrent.futures import ThreadPoolExecutorfrom copy import deepcopyfrom random import sample-from typing import Callable, List, Tuple+from typing import Callablefrom pydantic import BaseModelfrom tqdm import tqdm@@ -16,7 +16,7 @@ DEFAULT_MAX_EXAMPLES = 10class EvalResult(BaseModel):example: dictscore: float- actions: List[ActionOutput] | None = None+ actions: list[ActionOutput] | None = Noneclass Comparator(dspy.Signature):@@ -31,15 +31,15 @@ Task:prefix="Instruction: ",desc="Instruction for the actor to execute the task",)- actions: List[str] = dspy.InputField(+ actions: list[str] = dspy.InputField(prefix="Actions: ",desc="Actions actor can take to complete the task",)- pos_input_with_metrics: List[EvalResult] = dspy.InputField(+ pos_input_with_metrics: list[EvalResult] = dspy.InputField(prefix="Positive Inputs: ",desc="Positive inputs along with their score on a evaluation metric and actions taken",)- neg_input_with_metrics: List[EvalResult] = dspy.InputField(+ neg_input_with_metrics: list[EvalResult] = dspy.InputField(prefix="Negative Inputs: ",desc="Negative inputs along with their score on a evaluation metric and actions taken",)@@ -148,8 +148,8 @@ class AvatarOptimizer(Teleprompter):def _get_pos_neg_results(self,actor: dspy.Module,- trainset: List[dspy.Example]- ) -> Tuple[float, List[EvalResult], List[EvalResult]]:+ trainset: list[dspy.Example]+ ) -> tuple[float, list[EvalResult], list[EvalResult]]:pos_inputs = []neg_inputs = []diff --git a/dspy/teleprompt/bettertogether.py b/dspy/teleprompt/bettertogether.pyindex 7f72069d0..6c61974df 100644--- a/dspy/teleprompt/bettertogether.py+++ b/dspy/teleprompt/bettertogether.py@@ -1,6 +1,6 @@import loggingimport random-from typing import Callable, List+from typing import Callableimport dspyfrom dspy.primitives.example import Example@@ -53,7 +53,7 @@ class BetterTogether(Teleprompter):def compile(self,student: Module,- trainset: List[Example],+ trainset: list[Example],strategy: str = "p -> w -> p",valset_ratio = 0.1,) -> Module:diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.pyindex d7031194a..bc75d3efe 100644--- a/dspy/teleprompt/bootstrap.py+++ b/dspy/teleprompt/bootstrap.py@@ -1,7 +1,6 @@import loggingimport randomimport threading-from typing import Dictimport tqdm@@ -39,7 +38,7 @@ class BootstrapFewShot(Teleprompter):self,metric=None,metric_threshold=None,- teacher_settings: Dict | None = None,+ teacher_settings: dict | None = None,max_bootstrapped_demos=4,max_labeled_demos=16,max_rounds=1,diff --git a/dspy/teleprompt/bootstrap_finetune.py b/dspy/teleprompt/bootstrap_finetune.pyindex 19db70432..b8d531f79 100644--- a/dspy/teleprompt/bootstrap_finetune.py+++ b/dspy/teleprompt/bootstrap_finetune.py@@ -1,7 +1,7 @@import loggingfrom collections import defaultdictfrom dataclasses import dataclass-from typing import Any, Callable, Dict, List+from typing import Any, Callableimport dspyfrom dspy.adapters.base import Adapter@@ -22,12 +22,12 @@ logger = logging.getLogger(__name__)class FinetuneTeleprompter(Teleprompter):def __init__(self,- train_kwargs: Dict[str, Any] | Dict[LM, Dict[str, Any]] | None = 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 {})+ self.train_kwargs: dict[LM, Any] = self.convert_to_lm_dict(train_kwargs or {})@staticmethod- def convert_to_lm_dict(arg) -> Dict[LM, Any]:+ def convert_to_lm_dict(arg) -> dict[LM, Any]:non_empty_dict = arg and isinstance(arg, dict)if non_empty_dict and all(isinstance(k, LM) for k in arg.keys()):return arg@@ -40,8 +40,8 @@ class BootstrapFinetune(FinetuneTeleprompter):self,metric: Callable | None = None,multitask: bool = True,- train_kwargs: Dict[str, Any] | Dict[LM, Dict[str, Any]] | None = None,- adapter: Adapter | Dict[LM, Adapter] | None = 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 | None = None,):@@ -55,12 +55,12 @@ class BootstrapFinetune(FinetuneTeleprompter):super().__init__(train_kwargs=train_kwargs)self.metric = metricself.multitask = multitask- self.adapter: Dict[LM, Adapter] = self.convert_to_lm_dict(adapter)+ self.adapter: dict[LM, Adapter] = self.convert_to_lm_dict(adapter)self.exclude_demos = exclude_demosself.num_threads = num_threadsdef compile(- self, student: Module, trainset: List[Example], teacher: Module | List[Module] | None = 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@@ -129,7 +129,7 @@ class BootstrapFinetune(FinetuneTeleprompter):return student@staticmethod- def finetune_lms(finetune_dict) -> Dict[Any, LM]:+ def finetune_lms(finetune_dict) -> dict[Any, LM]:num_jobs = len(finetune_dict)logger.info(f"Starting {num_jobs} fine-tuning job(s)...")# TODO(nit) Pass an identifier to the job so that we can tell the logs@@ -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: int | None = 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 nicerif self.metric:logger.info(f"Collected data for {len(trace_data)} examples")@@ -190,11 +190,11 @@ class BootstrapFinetune(FinetuneTeleprompter):# Similar methods are implemented separately and used by other DSPy# teleprompters. These can be moved to shared locations.def build_call_data_from_trace(- trace: List[Dict],+ trace: list[dict],pred_ind: int,adapter: Adapter,exclude_demos: bool = False,-) -> Dict[str, List[Dict[str, Any]]]:+) -> dict[str, list[dict[str, Any]]]:# Find data that's relevant to the predictorpred, inputs, outputs = trace[pred_ind] # assuming that the order is kept@@ -216,7 +216,7 @@ class FailedPrediction:def bootstrap_trace_data(program: Module,- dataset: List[Example],+ dataset: list[Example],metric: Callable | None = None,num_threads: int | None = None,raise_on_error=True,@@ -224,7 +224,7 @@ def bootstrap_trace_data(failure_score: float = 0,format_failure_score: float = -1,log_format_failures: bool = False,-) -> List[Dict[str, Any]]:+) -> list[dict[str, Any]]:# Return a list of dicts with the following keys: example_ind, example, prediction, trace, and score# (if metric != None)evaluator = Evaluate(@@ -320,7 +320,7 @@ def bootstrap_trace_data(# example: Example,# program: Program,# metric: Optional[Callable] = None-# ) -> Dict[str, Any]:+# ) -> dict[str, Any]:# # Return a dict with the following keys:# # example, prediction, trace, and score (if metric != None)# with dspy.context(trace=[]):@@ -407,7 +407,7 @@ def assert_no_shared_predictor(program1: Module, program2: Module):assert not shared_ids, err-def get_unique_lms(program: Module) -> List[LM]:+def get_unique_lms(program: Module) -> list[LM]:lms = [pred.lm for pred in program.predictors()]return list(set(lms))diff --git a/dspy/teleprompt/grpo.py b/dspy/teleprompt/grpo.pyindex c88b16063..3f929f4cd 100644--- a/dspy/teleprompt/grpo.py+++ b/dspy/teleprompt/grpo.py@@ -1,7 +1,7 @@import loggingimport randomfrom collections import Counter-from typing import Any, Callable, Dict, List, Literal, Tuple+from typing import Any, Callable, Literalfrom dspy.adapters.base import Adapterfrom dspy.adapters.chat_adapter import ChatAdapter@@ -27,8 +27,8 @@ class GRPO(FinetuneTeleprompter):self,metric: Callable | None = None,multitask: bool = True,- train_kwargs: Dict[str, Any] | Dict[LM, Dict[str, Any]] | None = None,- adapter: Adapter | Dict[LM, Adapter] | None = 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,@@ -46,7 +46,7 @@ class GRPO(FinetuneTeleprompter):super().__init__(train_kwargs=train_kwargs)self.metric = metricself.multitask = multitask- self.adapter: Dict[LM, Adapter] = self.convert_to_lm_dict(adapter)+ self.adapter: dict[LM, Adapter] = self.convert_to_lm_dict(adapter)self.exclude_demos = exclude_demosself.num_threads = num_threadsself.num_train_steps = num_train_steps@@ -81,13 +81,13 @@ class GRPO(FinetuneTeleprompter):def validate_trace_data_and_log_issues(self,- trace_data: List[List[List[Dict[str, Any]]]],- subsample_training_dataset: List[Example],+ trace_data: list[list[list[dict[str, Any]]]],+ subsample_training_dataset: list[Example],num_teachers: int,num_samples_per_input: int,- pred_signature_hash_to_ind: Dict[int, int],+ pred_signature_hash_to_ind: dict[int, int],):- # At this point, trace_data: List[example_idx -> List[teacher_idx -> [num_samples_per_input * Dict(example, prediction, trace, example_ind, score)]]]+ # At this point, trace_data: list[example_idx -> list[teacher_idx -> [num_samples_per_input * Dict(example, prediction, trace, example_ind, score)]]]# Shape of trace is: [dspy_module_invocation_idx -> Tuple[Predictor, PredictorInputs, Prediction]]assert len(trace_data) == len(subsample_training_dataset), f"Trace data length {len(trace_data)} does not match the number of examples {len(subsample_training_dataset)}"assert len(trace_data[0]) == num_teachers, f"Trace data length {len(trace_data[0])} does not match the number of teachers {num_teachers}"@@ -208,9 +208,9 @@ class GRPO(FinetuneTeleprompter):def select_training_sample_and_update_shuffled_trainset(self,- original_trainset: List[Example],+ original_trainset: list[Example],train_step_idx: int,- ) -> List[Example]:+ ) -> list[Example]:base_idx = train_step_idx * self.num_dspy_examples_per_grpo_stepif self.epoch == -1:curr_epoch = 0@@ -234,9 +234,9 @@ class GRPO(FinetuneTeleprompter):def compile(self,student: Module,- trainset: List[Example],- teacher: Module | List[Module] | None = None,- valset: List[Example] | None = None,+ trainset: list[Example],+ 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.")@@ -375,7 +375,7 @@ class GRPO(FinetuneTeleprompter):# The trace_data for examples with FailedPrediction cases will have the signature at index 0, instead of the predictor# We need to replace the signature with the predictor- # At this point, trace_data: List[example_idx -> List[teacher_idx -> [num_samples_per_input * Dict(example, prediction, trace, example_ind, score)]]]+ # At this point, trace_data: list[example_idx -> list[teacher_idx -> [num_samples_per_input * Dict(example, prediction, trace, example_ind, score)]]]# Shape of trace is: [dspy_module_invocation_idx -> Tuple[Predictor, PredictorInputs, Prediction]]self.validate_trace_data_and_log_issues(trace_data=trace_data,@@ -387,15 +387,15 @@ class GRPO(FinetuneTeleprompter):logger.info("Preparing the training data batch from bootstrapped examples for GRPO...")# Now, we need to prepare batches of data to be sent for training- # Shape of train_batch_per_predictor: List[num_student_predictors -> List[ ]]- train_batch_per_predictor: List[List[GRPOGroup]] = [[] for _ in range(num_student_predictors)]+ # Shape of train_batch_per_predictor: list[num_student_predictors -> list[ ]]+ train_batch_per_predictor: list[list[GRPOGroup]] = [[] for _ in range(num_student_predictors)]for pred_id in range(num_student_predictors):for example_ind, example_data in enumerate(trace_data):# Each example_data is a list of teacher_idx -> [num_samples_per_input * Dict(example, prediction, trace, example_ind, score)]# We need to flatten this list and create a batch for each predictor# TODO(Lakshya, Omar, Noah): Discuss what to do with the same module being invoked multiple times within a single dspy.Example- predictor_example_invocations: List[List[Tuple]] = []+ predictor_example_invocations: list[list[tuple]] = []for teacher_data in example_data:for sample in teacher_data:@@ -434,7 +434,7 @@ class GRPO(FinetuneTeleprompter):assert self.variably_invoked_predictor_grouping_mode == "ragged", f"Unknown variably invoked predictor grouping mode {self.variably_invoked_predictor_grouping_mode}"max_len = max([len(predictor_example_invocations[i]) for i in range(len(predictor_example_invocations))])- example_training_data: List[GRPOGroup] = [[] for _ in range(max_len)]+ example_training_data: list[GRPOGroup] = [[] for _ in range(max_len)]for group_idx in range(max_len):for rollout_idx in range(len(predictor_example_invocations)):@@ -519,7 +519,7 @@ class GRPO(FinetuneTeleprompter):# LM.logger.info("Invoking GRPO training step...")for (_, data_key), job in grpo_training_jobs.items():- train_data: List[GRPOGroup] = sum(train_batch_per_predictor, []) if data_key is None else train_batch_per_predictor[data_key] #noqa: RUF017+ train_data: list[GRPOGroup] = sum(train_batch_per_predictor, []) if data_key is None else train_batch_per_predictor[data_key] #noqa: RUF017for group in train_data:if len(group) != self.num_rollouts_per_grpo_step:# TODO(GRPO Team): This is very undesirable. This occurs only because in some of the generations, the model does not follow the correct dspy format.diff --git a/dspy/teleprompt/mipro_optimizer_v2.py b/dspy/teleprompt/mipro_optimizer_v2.pyindex eb1a4d80a..117dd183e 100644--- a/dspy/teleprompt/mipro_optimizer_v2.py+++ b/dspy/teleprompt/mipro_optimizer_v2.py@@ -4,7 +4,7 @@ import sysimport textwrapimport timefrom collections import defaultdict-from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Tuple+from typing import TYPE_CHECKING, Any, Callable, Literalimport numpy as np@@ -97,9 +97,9 @@ class MIPROv2(Teleprompter):self,student: Any,*,- trainset: List,+ trainset: list,teacher: Any = None,- valset: List | None = None,+ valset: list | None = None,num_trials: int | None = None,max_bootstrapped_demos: int | None = None,max_labeled_demos: int | None = None,@@ -242,8 +242,8 @@ class MIPROv2(Teleprompter):num_trials: int,minibatch: bool,zeroshot_opt: bool,- valset: List,- ) -> Tuple[int, List, bool]:+ valset: list,+ ) -> tuple[int, list, bool]:if self.auto is None:return num_trials, valset, minibatch@@ -262,7 +262,7 @@ class MIPROv2(Teleprompter):return num_trials, valset, minibatch- def _set_and_validate_datasets(self, trainset: List, valset: List | None):+ def _set_and_validate_datasets(self, trainset: list, valset: list | None):if not trainset:raise ValueError("Trainset cannot be empty.")@@ -279,7 +279,7 @@ class MIPROv2(Teleprompter):return trainset, valset- def _print_auto_run_settings(self, num_trials: int, minibatch: bool, valset: List):+ def _print_auto_run_settings(self, num_trials: int, minibatch: bool, valset: list):logger.info(f"\nRUNNING WITH THE FOLLOWING {self.auto.upper()} AUTO RUN SETTINGS:"f"\nnum_trials: {num_trials}"@@ -296,9 +296,9 @@ class MIPROv2(Teleprompter):minibatch: bool,minibatch_size: int,minibatch_full_eval_steps: int,- valset: List,+ valset: list,program_aware_proposer: bool,- ) -> Tuple[str, str]:+ ) -> tuple[str, str]:num_predictors = len(program.predictors())# Estimate prompt model calls@@ -343,7 +343,7 @@ class MIPROv2(Teleprompter):minibatch: bool,minibatch_size: int,minibatch_full_eval_steps: int,- valset: List,+ valset: list,program_aware_proposer: bool,) -> bool:prompt_model_line, task_model_line = self._estimate_lm_calls(@@ -410,7 +410,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) -> List | None:+ 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(@@ -454,14 +454,14 @@ class MIPROv2(Teleprompter):def _propose_instructions(self,program: Any,- trainset: List,- demo_candidates: List | None,+ trainset: list,+ demo_candidates: list | None,view_data_batch_size: int,program_aware_proposer: bool,data_aware_proposer: bool,tip_aware_proposer: bool,fewshot_aware_proposer: bool,- ) -> Dict[int, List[str]]:+ ) -> dict[int, list[str]]:logger.info("\n==> STEP 2: PROPOSE INSTRUCTION CANDIDATES <==")logger.info("We will use the few-shot examples from the previous step, a generated dataset summary, a summary of the program code, and a randomly selected prompting tip to propose instructions."@@ -506,10 +506,10 @@ class MIPROv2(Teleprompter):def _optimize_prompt_parameters(self,program: Any,- instruction_candidates: Dict[int, List[str]],- demo_candidates: List | None,+ instruction_candidates: dict[int, list[str]],+ demo_candidates: list | None,evaluate: Evaluate,- valset: List,+ valset: list,num_trials: int,minibatch: bool,minibatch_size: int,@@ -751,12 +751,12 @@ class MIPROv2(Teleprompter):def _select_and_insert_instructions_and_demos(self,candidate_program: Any,- instruction_candidates: Dict[int, List[str]],- demo_candidates: List | None,+ instruction_candidates: dict[int, list[str]],+ demo_candidates: list | None,trial: "optuna.trial.Trial",- trial_logs: Dict,+ trial_logs: dict,trial_num: int,- ) -> List[str]:+ ) -> list[str]:chosen_params = []raw_chosen_params = {}@@ -799,18 +799,18 @@ class MIPROv2(Teleprompter):self,trial_num: int,adjusted_num_trials: int,- param_score_dict: Dict,- fully_evaled_param_combos: Dict,+ param_score_dict: dict,+ fully_evaled_param_combos: dict,evaluate: Evaluate,- valset: List,- trial_logs: Dict,+ valset: list,+ trial_logs: dict,total_eval_calls: int,score_data,best_score: float,best_program: Any,study: "optuna.Study",- instruction_candidates: List,- demo_candidates: List,+ instruction_candidates: list,+ demo_candidates: list,):import optunadiff --git a/dspy/utils/callback.py b/dspy/utils/callback.pyindex 7b2570552..20be402e8 100644--- a/dspy/utils/callback.py+++ b/dspy/utils/callback.py@@ -3,7 +3,7 @@ import inspectimport loggingimport uuidfrom contextvars import ContextVar-from typing import Any, Callable, Dict+from typing import Any, Callableimport dspy@@ -66,7 +66,7 @@ class BaseCallback:self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):"""A handler triggered when forward() method of a module (subclass of dspy.Module) is called.@@ -98,7 +98,7 @@ class BaseCallback:self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):"""A handler triggered when __call__ method of dspy.LM instance is called.@@ -113,7 +113,7 @@ class BaseCallback:def on_lm_end(self,call_id: str,- outputs: Dict[str, Any] | None,+ outputs: dict[str, Any] | None,exception: Exception | None = None,):"""A handler triggered after __call__ method of dspy.LM instance is executed.@@ -130,7 +130,7 @@ class BaseCallback:self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):"""A handler triggered when format() method of an adapter (subclass of dspy.Adapter) is called.@@ -145,7 +145,7 @@ class BaseCallback:def on_adapter_format_end(self,call_id: str,- outputs: Dict[str, Any] | 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..@@ -162,7 +162,7 @@ class BaseCallback:self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):"""A handler triggered when parse() method of an adapter (subclass of dspy.Adapter) is called.@@ -177,7 +177,7 @@ class BaseCallback:def on_adapter_parse_end(self,call_id: str,- outputs: Dict[str, Any] | 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.@@ -194,7 +194,7 @@ class BaseCallback:self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):"""A handler triggered when a tool is called.@@ -209,7 +209,7 @@ class BaseCallback:def on_tool_end(self,call_id: str,- outputs: Dict[str, Any] | None,+ outputs: dict[str, Any] | None,exception: Exception | None = None,):"""A handler triggered after a tool is executed.@@ -226,7 +226,7 @@ class BaseCallback:self,call_id: str,instance: Any,- inputs: Dict[str, Any],+ inputs: dict[str, Any],):"""A handler triggered when evaluation is started.diff --git a/dspy/utils/dummies.py b/dspy/utils/dummies.pyindex 092187ba9..631d618c3 100644--- a/dspy/utils/dummies.py+++ b/dspy/utils/dummies.py@@ -1,6 +1,6 @@import randomfrom collections import defaultdict-from typing import Any, Dict+from typing import Anyimport numpy as np@@ -93,7 +93,7 @@ class DummyLM(LM):@with_callbacksdef __call__(self, prompt=None, messages=None, **kwargs):- def format_answer_fields(field_names_and_values: Dict[str, Any]):+ def format_answer_fields(field_names_and_values: dict[str, Any]):return ChatAdapter().format_field_with_value(fields_with_values={FieldInfoWithName(name=field_name, info=OutputField()): valuediff --git a/dspy/utils/unbatchify.py b/dspy/utils/unbatchify.pyindex d89fb0898..44a4f5950 100644--- a/dspy/utils/unbatchify.py+++ b/dspy/utils/unbatchify.py@@ -2,13 +2,13 @@ import queueimport threadingimport timefrom concurrent.futures import Future-from typing import Any, Callable, List+from typing import Any, Callableclass Unbatchify:def __init__(self,- batch_fn: Callable[[List[Any]], List[Any]],+ batch_fn: Callable[[list[Any]], list[Any]],max_batch_size: int = 32,max_wait_time: float = 0.1):diff --git a/pyproject.toml b/pyproject.tomlindex a2c092853..88a244199 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -142,7 +142,6 @@ ignore = ["FBT003",# Allow boolean positional values in function calls"C901", # Ignore complexity checking"E501", # Ignore line length errors (handled by formatter)- "UP006", # Allow python typing modules"UP035", # Allow python typing modules"RUF005", # Allow using + operator to concatenate collections"B904", # Allow raise custom exceptions in except blocksdiff --git a/tests/evaluate/test_evaluate.py b/tests/evaluate/test_evaluate.pyindex 62572685f..106396c15 100644--- a/tests/evaluate/test_evaluate.py+++ b/tests/evaluate/test_evaluate.py@@ -153,11 +153,11 @@ def test_evaluate_call_wrong_answer():# Create programs that do not return dictionary-like objects because Evaluate()# has failed for such cases in the past(- lambda text: Predict("text: str -> entities: List[str]")(text=text).entities,+ lambda text: Predict("text: str -> entities: list[str]")(text=text).entities,dspy.Example(text="United States", entities=["United States"]).with_inputs("text"),),(- lambda text: Predict("text: str -> entities: List[Dict[str, str]]")(text=text).entities,+ lambda text: Predict("text: str -> entities: list[dict[str, str]]")(text=text).entities,dspy.Example(text="United States", entities=[{"name": "United States", "type": "location"}]).with_inputs("text"),diff --git a/tests/reliability/complex_types/generated/test_nesting_1/program.py b/tests/reliability/complex_types/generated/test_nesting_1/program.pyindex 607f1675d..259464a28 100644--- a/tests/reliability/complex_types/generated/test_nesting_1/program.py+++ b/tests/reliability/complex_types/generated/test_nesting_1/program.py@@ -39,7 +39,7 @@ from pydantic import BaseModel, Fieldclass ResultLevel5(BaseModel):outputField1: bool = Field(..., description="A boolean field indicating success or failure")- outputField2: List[str] = Field(..., description="An array of strings representing messages")+ outputField2: list[str] = Field(..., description="An array of strings representing messages")class ResultLevel4(BaseModel):diff --git a/tests/reliability/generate/__init__.py b/tests/reliability/generate/__init__.pyindex 43dd27deb..c98680332 100644--- a/tests/reliability/generate/__init__.py+++ b/tests/reliability/generate/__init__.py@@ -15,7 +15,7 @@ def generate_test_cases(num_inputs: int = 1,program_instructions: Optional[str] = None,input_instructions: Optional[str] = None,-) -> List[GeneratedTestCase]:+) -> list[GeneratedTestCase]:os.makedirs(dst_path, exist_ok=True)if _directory_contains_program(dst_path):print(f"Found an existing test program at path {dst_path}. Generating new" f" test inputs for this program.")diff --git a/tests/reliability/generate/utils.py b/tests/reliability/generate/utils.pyindex 2ac871f2e..e73599ece 100644--- a/tests/reliability/generate/utils.py+++ b/tests/reliability/generate/utils.py@@ -57,7 +57,7 @@ def generate_test_program(dst_path: str, additional_instructions: Optional[str]A dspy.Module object representing the generated program."""- def generate_models(schema: Dict[str, Any], class_name: str) -> str:+ def generate_models(schema: dict[str, Any], class_name: str) -> str:with tempfile.TemporaryDirectory() as tmp_dir:tmp_schema_path = os.path.join(tmp_dir, "schema.json")tmp_model_path = os.path.join(tmp_dir, "model.py")@@ -85,9 +85,9 @@ def generate_test_program(dst_path: str, additional_instructions: Optional[str]return f.read()def rename_conflicting_fields(- input_schema: Dict[str, Any],- output_schema: Dict[str, Any],- ) -> Dict[str, Any]:+ input_schema: dict[str, Any],+ output_schema: dict[str, Any],+ ) -> dict[str, Any]:input_fields = set(input_schema.get("properties", {}))output_schema["properties"] = {(f"{field}_output" if field in input_fields else field): properties@@ -259,10 +259,10 @@ class GeneratedTestCase:# A JSON representation of the input to the program that the test case is testing.program_input: str# The assertions that the output of the program must satisfy for the test case to pass.- output_assertions: List[str]+ output_assertions: list[str]-def load_generated_cases(dir_path) -> List[GeneratedTestCase]:+def load_generated_cases(dir_path) -> list[GeneratedTestCase]:"""Recursively loads generated test cases from the specified directory and its subdirectories.@@ -437,7 +437,7 @@ def _get_test_inputs_generation_program() -> dspy.Module:description="The output signature of the program in JSON Schema format, including output field names, types, and descriptions. The outermost fields in the JSON schema definition represent the top-level output fields of the program.")additional_instructions: str = dspy.InputField(description="Additional instructions for generating test inputs")- test_inputs: List[_TestInput] = dspy.OutputField(+ test_inputs: list[_TestInput] = dspy.OutputField(description="Generated test inputs for the program, used to verify the correctness of the program outputs for a variety of inputs")@@ -488,14 +488,14 @@ def _get_assertions_generation_program() -> dspy.Module:program_output_signature: str = dspy.InputField(description="The output signature of the program in JSON Schema format, including output field names, types, and descriptions. The outermost fields in the JSON schema definition represent the top-level output fields of the program.")- output_assertions: List[str] = dspy.OutputField(+ output_assertions: list[str] = dspy.OutputField(description="Assertions used to verify the correctness of the program output after running the program on the specified input")return dspy.ChainOfThought(_TestInputsGeneration)-def _clean_json_schema_property(prop: Dict[str, Any]) -> Dict[str, Any]:+def _clean_json_schema_property(prop: dict[str, Any]) -> dict[str, Any]:"""Remove unnecessary keys from a JSON schema property dictionary, as well asall of its child properties.@@ -516,7 +516,7 @@ def _clean_json_schema_property(prop: Dict[str, Any]) -> Dict[str, Any]:return cleaned_prop-def _get_json_schema(signature: dspy.Signature) -> Dict[str, Any]:+def _get_json_schema(signature: dspy.Signature) -> dict[str, Any]:"""Obtain the JSON schema representation of a DSPy signature.@@ -526,7 +526,7 @@ def _get_json_schema(signature: dspy.Signature) -> Dict[str, Any]:A JSON schema representation of the signature."""- def expand_refs(schema: Dict[str, Any], definitions: Dict[str, Any]) -> Dict[str, Any]:+ def expand_refs(schema: dict[str, Any], definitions: dict[str, Any]) -> dict[str, Any]:"""Expand $ref fields in a JSON schema, inlining the referenced schema definitionsdirectly into the $ref field locations.@@ -551,7 +551,7 @@ def _get_json_schema(signature: dspy.Signature) -> Dict[str, Any]:return expand_refs(signature_schema_with_refs, definitions)-def _split_schema(schema: Dict[str, Any]) -> Tuple[Dict[str, Any], Dict[str, Any]]:+def _split_schema(schema: dict[str, Any]) -> Tuple[dict[str, Any], dict[str, Any]]:"""Split a JSON schema into input and output components based on DSPy field types.@@ -586,7 +586,7 @@ def _split_schema(schema: Dict[str, Any]) -> Tuple[Dict[str, Any], Dict[str, Anyreturn inputs, outputs-def _clean_schema(prop: Dict[str, Any]) -> Dict[str, Any]:+def _clean_schema(prop: dict[str, Any]) -> dict[str, Any]:"""Recursively clean a JSON schema property by removing unnecessary keys.@@ -606,7 +606,7 @@ def _clean_schema(prop: Dict[str, Any]) -> Dict[str, Any]:return cleaned_prop-def _json_input_to_program_input(input_schema: pydantic.BaseModel, json_input: str) -> Dict[str, Any]:+def _json_input_to_program_input(input_schema: pydantic.BaseModel, json_input: str) -> dict[str, Any]:"""Convert a JSON input string to a DSPy program input dictionary, validating it against theprovided program signature.@@ -693,7 +693,7 @@ def _remove_comments_from_file(file_path: str) -> None:file.writelines(cleaned_lines)-def _write_pretty_json(data: Dict[str, Any], path: Optional[str] = None) -> Optional[str]:+def _write_pretty_json(data: dict[str, Any], path: Optional[str] = None) -> Optional[str]:"""Format JSON data with indentation, and write it to a file if specified.diff --git a/tests/reliability/test_pydantic_models.py b/tests/reliability/test_pydantic_models.pyindex 1853fc885..edd560b38 100644--- a/tests/reliability/test_pydantic_models.py+++ b/tests/reliability/test_pydantic_models.py@@ -14,7 +14,7 @@ def test_qa_with_pydantic_answer_model():certainty: float = pydantic.Field(description="A value between 0 and 1 indicating the model's confidence in the answer.")- comments: List[str] = pydantic.Field(+ comments: list[str] = pydantic.Field(description="At least two comments providing additional details about the answer.")diff --git a/tests/reliability/utils.py b/tests/reliability/utils.pyindex 349192fe3..c71307240 100644--- a/tests/reliability/utils.py+++ b/tests/reliability/utils.py@@ -15,7 +15,7 @@ JUDGE_MODEL_NAME = "judge"def assert_program_output_correct(program_input: Any,program_output: Any,- grading_guidelines: Union[str, List[str]],+ grading_guidelines: Union[str, list[str]],):"""With the help of an LLM judge, assert that the specified output of a DSPy program is correct,@@ -40,7 +40,7 @@ def assert_program_output_correct(assert judge_response.correct, f"Output: {program_output}. Reason incorrect: {judge_response.justification}"-def known_failing_models(models: List[str]):+def known_failing_models(models: list[str]):"""Decorator to allow specific test cases to fail for certain models. This is useful when amodel is known to be unable to perform a specific task (e.g. output formatting with complex@@ -115,7 +115,7 @@ def _get_judge_program():class ReliabilityTestConf(pydantic.BaseModel):adapter: str- models: Dict[str, Any]+ models: dict[str, Any]@lru_cache(maxsize=None)diff --git a/tests/signatures/test_adapter_image.py b/tests/signatures/test_adapter_image.pyindex 7f20ebb91..f458a489e 100644--- a/tests/signatures/test_adapter_image.py+++ b/tests/signatures/test_adapter_image.py@@ -1,7 +1,6 @@import osimport tempfilefrom io import BytesIO-from typing import Listimport pydanticimport pytest@@ -78,7 +77,7 @@ def setup_predictor(signature, expected_output):[{"name": "probabilistic_classification",- "signature": "image: dspy.Image, class_labels: List[str] -> probabilities: Dict[str, float]",+ "signature": "image: dspy.Image, class_labels: list[str] -> probabilities: dict[str, float]","inputs": {"image": "https://example.com/dog.jpg", "class_labels": ["dog", "cat", "bird"]},"key_output": "probabilities","expected": {"probabilities": {"dog": 0.8, "cat": 0.1, "bird": 0.1}},@@ -92,14 +91,14 @@ def setup_predictor(signature, expected_output):},{"name": "bbox_detection",- "signature": "image: dspy.Image -> bboxes: List[Tuple[int, int, int, int]]",+ "signature": "image: dspy.Image -> bboxes: list[Tuple[int, int, int, int]]","inputs": {"image": "https://example.com/image.jpg"},"key_output": "bboxes","expected": {"bboxes": [(10, 20, 30, 40), (50, 60, 70, 80)]},},{"name": "multilingual_caption",- "signature": "image: dspy.Image, languages: List[str] -> captions: Dict[str, str]",+ "signature": "image: dspy.Image, languages: list[str] -> captions: dict[str, str]","inputs": {"image": "https://example.com/dog.jpg", "languages": ["en", "es", "fr"]},"key_output": "captions","expected": {@@ -139,7 +138,7 @@ def test_image_input_formats(request, sample_pil_image, sample_dspy_image_download, sample_dspy_image_no_download, image_input, description):"""Test different input formats for image fields"""- signature = "image: dspy.Image, class_labels: List[str] -> probabilities: Dict[str, float]"+ signature = "image: dspy.Image, class_labels: list[str] -> probabilities: dict[str, float]"expected = {"probabilities": {"dog": 0.8, "cat": 0.1, "bird": 0.1}}predictor, lm = setup_predictor(signature, expected)@@ -195,7 +194,7 @@ def test_save_load_complex_default_types():]class ComplexTypeSignature(dspy.Signature):- image_list: List[dspy.Image] = dspy.InputField(desc="A list of images")+ image_list: list[dspy.Image] = dspy.InputField(desc="A list of images")caption: str = dspy.OutputField(desc="A caption for the image list")predictor, lm = setup_predictor(ComplexTypeSignature, {"caption": "A list of images"})@@ -223,7 +222,7 @@ class BasicImageSignature(dspy.Signature):class ImageListSignature(dspy.Signature):"""Signature with a list of images input"""- image_list: List[dspy.Image] = dspy.InputField()+ image_list: list[dspy.Image] = dspy.InputField()output: str = dspy.OutputField()@@ -290,7 +289,7 @@ def test_save_load_pydantic_model():class ImageModel(pydantic.BaseModel):image: dspy.Image- image_list: List[dspy.Image] | None = None+ image_list: list[dspy.Image] | None = Noneoutput: strclass PydanticSignature(dspy.Signature):diff --git a/tests/signatures/test_signature.py b/tests/signatures/test_signature.pyindex 2a18d5ac5..9e63743b9 100644--- a/tests/signatures/test_signature.py+++ b/tests/signatures/test_signature.py@@ -1,5 +1,5 @@from types import UnionType-from typing import Any, Dict, List, Optional, Tuple, Union+from typing import Any, Optional, Unionimport pydanticimport pytest@@ -15,13 +15,13 @@ def test_field_types_and_custom_attributes():input1: str = InputField()input2: int = InputField()- output1: List[str] = OutputField()+ output1: list[str] = OutputField()output2 = OutputField()assert TestSignature.instructions == "Instructions"assert TestSignature.input_fields["input1"].annotation == strassert TestSignature.input_fields["input2"].annotation == int- assert TestSignature.output_fields["output1"].annotation == List[str]+ assert TestSignature.output_fields["output1"].annotation == list[str]assert TestSignature.output_fields["output2"].annotation == str@@ -255,13 +255,13 @@ def test_typed_signatures_basic_types():def test_typed_signatures_generics():sig = Signature(- "input_list: List[int], input_dict: Dict[str, float] -> output_tuple: Tuple[str, int]")+ "input_list: list[int], input_dict: dict[str, float] -> output_tuple: tuple[str, int]")assert "input_list" in sig.input_fields- assert sig.input_fields["input_list"].annotation == List[int]+ assert sig.input_fields["input_list"].annotation == list[int]assert "input_dict" in sig.input_fields- assert sig.input_fields["input_dict"].annotation == Dict[str, float]+ assert sig.input_fields["input_dict"].annotation == dict[str, float]assert "output_tuple" in sig.output_fields- assert sig.output_fields["output_tuple"].annotation == Tuple[str, int]+ assert sig.output_fields["output_tuple"].annotation == tuple[str, int]def test_typed_signatures_unions_and_optionals():@@ -305,7 +305,7 @@ def test_typed_signatures_any():def test_typed_signatures_nested():sig = Signature(- "input_nested: List[Union[str, int]] -> output_nested: Tuple[int, Optional[float], List[str]]")+ "input_nested: list[Union[str, int]] -> output_nested: Tuple[int, Optional[float], list[str]]")input_nested_ann = sig.input_fields["input_nested"].annotationassert getattr(input_nested_ann, "__origin__", None) is listassert len(input_nested_ann.__args__) == 1@@ -320,7 +320,7 @@ def test_typed_signatures_nested():second_arg = output_nested_ann.__args__[1]assert getattr(second_arg, "__origin__", None) is Unionassert float in second_arg.__args__ and type(None) in second_arg.__args__- # The third arg is List[str]+ # The third arg is list[str]third_arg = output_nested_ann.__args__[2]assert getattr(third_arg, "__origin__", None) is listassert third_arg.__args__[0] == str@@ -328,28 +328,28 @@ def test_typed_signatures_nested():def test_typed_signatures_from_dict():fields = {- "input_str_list": (List[str], InputField()),- "input_dict_int": (Dict[str, int], InputField()),- "output_tup": (Tuple[int, float], OutputField()),+ "input_str_list": (list[str], InputField()),+ "input_dict_int": (dict[str, int], InputField()),+ "output_tup": (tuple[int, float], OutputField()),}sig = Signature(fields)assert "input_str_list" in sig.input_fields- assert sig.input_fields["input_str_list"].annotation == List[str]+ assert sig.input_fields["input_str_list"].annotation == list[str]assert "input_dict_int" in sig.input_fields- assert sig.input_fields["input_dict_int"].annotation == Dict[str, int]+ assert sig.input_fields["input_dict_int"].annotation == dict[str, int]assert "output_tup" in sig.output_fields- assert sig.output_fields["output_tup"].annotation == Tuple[int, float]+ assert sig.output_fields["output_tup"].annotation == tuple[int, float]def test_typed_signatures_complex_combinations():sig = Signature(- "input_complex: Dict[str, List[Optional[Tuple[int, str]]]] -> output_complex: Union[List[str], Dict[str, Any]]"+ "input_complex: dict[str, list[Optional[Tuple[int, str]]]] -> output_complex: Union[list[str], dict[str, Any]]")input_complex_ann = sig.input_fields["input_complex"].annotationassert getattr(input_complex_ann, "__origin__", None) is dictkey_arg, value_arg = input_complex_ann.__args__assert key_arg == str- # value_arg: List[Optional[Tuple[int, str]]]+ # value_arg: list[Optional[Tuple[int, str]]]assert getattr(value_arg, "__origin__", None) is listinner_union = value_arg.__args__[0]# inner_union should be Optional[Tuple[int, str]]@@ -363,9 +363,9 @@ def test_typed_signatures_complex_combinations():assert getattr(output_complex_ann, "__origin__", None) is Unionassert len(output_complex_ann.__args__) == 2possible_args = set(output_complex_ann.__args__)- # Expecting List[str] and Dict[str, Any]+ # Expecting list[str] and dict[str, Any]# Because sets don't preserve order, just check membership.- # Find the List[str] arg+ # Find the list[str] arglist_arg = next(a for a in possible_args if getattr(a, "__origin__", None) is list)dict_arg = next(a for a in possible_args if getattr(@@ -377,13 +377,13 @@ def test_typed_signatures_complex_combinations():def test_make_signature_from_string():sig = Signature(- "input1: int, input2: Dict[str, int] -> output1: List[str], output2: Union[int, str]")+ "input1: int, input2: dict[str, int] -> output1: list[str], output2: Union[int, str]")assert "input1" in sig.input_fieldsassert sig.input_fields["input1"].annotation == intassert "input2" in sig.input_fields- assert sig.input_fields["input2"].annotation == Dict[str, int]+ assert sig.input_fields["input2"].annotation == dict[str, int]assert "output1" in sig.output_fields- assert sig.output_fields["output1"].annotation == List[str]+ assert sig.output_fields["output1"].annotation == list[str]assert "output2" in sig.output_fieldsassert sig.output_fields["output2"].annotation == Union[int, str]@@ -514,7 +514,7 @@ def test_pep604_union_type_class_equivalence():output: int | str = OutputField()class Sig2(Signature):- input: Optional[str] = InputField() # noqa: UP045+ input: str | None = InputField()output: Union[int, str] = OutputField() # noqa: UP007# PEP 604 union types in class signatures should be equivalent to Optional and Union typesdiff --git a/tests/test_utils/server/__init__.py b/tests/test_utils/server/__init__.pyindex 647c848ed..70215d8c5 100644--- a/tests/test_utils/server/__init__.py+++ b/tests/test_utils/server/__init__.py@@ -4,7 +4,7 @@ import socketimport subprocessimport tempfileimport time-from typing import Any, Dict, List, Tuple+from typing import Anyimport pytest@@ -12,7 +12,7 @@ from tests.test_utils.server.litellm_server import LITELLM_TEST_SERVER_LOG_FILE_@pytest.fixture()-def litellm_test_server() -> Tuple[str, str]:+def litellm_test_server() -> tuple[str, str]:"""Start a LiteLLM test server for a DSPy integration test case, and tear down theserver when the test case completes.@@ -45,7 +45,7 @@ def litellm_test_server() -> Tuple[str, str]:process.wait()-def read_litellm_test_server_request_logs(server_log_file_path: str) -> List[Dict[str, Any]]:+def read_litellm_test_server_request_logs(server_log_file_path: str) -> list[dict[str, Any]]:"""Read request logs from a LiteLLM server used during DSPy integration tests.