DSPy: The framework for programming—not prompting—language models
Python
auto style fix (#8309)
Chen Qian62bcf129a84a
diff --git a/dspy/adapters/__init__.py b/dspy/adapters/__init__.pyindex 15fc74a8a..9bb499f38 100644--- a/dspy/adapters/__init__.py+++ b/dspy/adapters/__init__.py@@ -2,7 +2,7 @@ from dspy.adapters.base import Adapterfrom dspy.adapters.chat_adapter import ChatAdapterfrom dspy.adapters.json_adapter import JSONAdapterfrom dspy.adapters.two_step_adapter import TwoStepAdapter-from dspy.adapters.types import History, Image, Audio, BaseType, Tool, ToolCalls+from dspy.adapters.types import Audio, BaseType, History, Image, Tool, ToolCalls__all__ = ["Adapter",diff --git a/dspy/adapters/types/__init__.py b/dspy/adapters/types/__init__.pyindex 11d09227c..68607e527 100644--- a/dspy/adapters/types/__init__.py+++ b/dspy/adapters/types/__init__.py@@ -1,7 +1,7 @@-from dspy.adapters.types.history import History-from dspy.adapters.types.image import Imagefrom dspy.adapters.types.audio import Audiofrom dspy.adapters.types.base_type import BaseType+from dspy.adapters.types.history import History+from dspy.adapters.types.image import Imagefrom dspy.adapters.types.tool import Tool, ToolCalls__all__ = ["History", "Image", "Audio", "BaseType", "Tool", "ToolCalls"]diff --git a/dspy/adapters/types/audio.py b/dspy/adapters/types/audio.pyindex 77c74b53a..800017e75 100644--- a/dspy/adapters/types/audio.py+++ b/dspy/adapters/types/audio.py@@ -48,7 +48,7 @@ class Audio(BaseType):"""if isinstance(values, cls):return {"data": values.data, "format": values.format}- return encode_audio(values)+ return encode_audio(values)@classmethoddef from_url(cls, url: str) -> "Audio":@@ -142,4 +142,4 @@ def encode_audio(audio: Union[str, bytes, dict, "Audio", Any], sampling_rate: inencoded = base64.b64encode(audio).decode("utf-8")return {"data": encoded, "format": format}else:- raise ValueError(f"Unsupported type for encode_audio: {type(audio)}")\ No newline at end of file+ raise ValueError(f"Unsupported type for encode_audio: {type(audio)}")diff --git a/dspy/dsp/utils/dpr.py b/dspy/dsp/utils/dpr.pyindex 5a46e61fd..e9fc27813 100644--- a/dspy/dsp/utils/dpr.py+++ b/dspy/dsp/utils/dpr.py@@ -158,7 +158,7 @@ class SimpleTokenizer(Tokenizer):annotators: None or empty set (only tokenizes)."""self._regexp = regex.compile(- "(%s)|(%s)" % (self.ALPHA_NUM, self.NON_WS), # noqa: UP031+ "(%s)|(%s)" % (self.ALPHA_NUM, self.NON_WS),flags=regex.IGNORECASE + regex.UNICODE + regex.MULTILINE,)if len(kwargs.get("annotators", {})) > 0:diff --git a/dspy/predict/__init__.py b/dspy/predict/__init__.pyindex f1f54c337..24926ef1b 100644--- a/dspy/predict/__init__.py+++ b/dspy/predict/__init__.py@@ -2,13 +2,13 @@ from dspy.predict.aggregation import majorityfrom dspy.predict.best_of_n import BestOfNfrom dspy.predict.chain_of_thought import ChainOfThoughtfrom dspy.predict.chain_of_thought_with_hint import ChainOfThoughtWithHint+from dspy.predict.code_act import CodeActfrom dspy.predict.knn import KNNfrom dspy.predict.multi_chain_comparison import MultiChainComparisonfrom dspy.predict.parallel import Parallelfrom dspy.predict.predict import Predictfrom dspy.predict.program_of_thought import ProgramOfThoughtfrom dspy.predict.react import ReAct, Tool-from dspy.predict.code_act import CodeActfrom dspy.predict.refine import Refine__all__ = [diff --git a/dspy/predict/code_act.py b/dspy/predict/code_act.pyindex 1633e3395..c48d2c5ae 100644--- a/dspy/predict/code_act.py+++ b/dspy/predict/code_act.py@@ -68,7 +68,7 @@ class CodeAct(ReAct, ProgramOfThought):self.extractor = dspy.ChainOfThought(extract_signature)# It will raises exception when dspy cannot find available deno instance by now.self.interpreter = PythonInterpreter()-+def _build_instructions(self, signature, tools):instructions = [f"{signature.instructions}\n"] if signature.instructions else []inputs = ", ".join([f"`{k}`" for k in signature.input_fields.keys()])@@ -85,14 +85,14 @@ class CodeAct(ReAct, ProgramOfThought):for idx, tool in enumerate(tools.values()):instructions.append(f"({idx + 1}) {tool}")-+return instructionsdef forward(self, **kwargs):# Define the tool funcitons in the interpreterfor tool in self.tools.values():self.interpreter(inspect.getsource(tool.func))-+trajectory = {}max_iters = kwargs.pop("max_iters", self.max_iters)for idx in range(max_iters):@@ -111,7 +111,7 @@ class CodeAct(ReAct, ProgramOfThought):trajectory[f"code_output_{idx}"] = outputelse:trajectory[f"observation_{idx}"] = f"Failed to execute the generated code: {error}"-+if code_data.finished:breakdiff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.pyindex 4606db87c..c25f4e7e0 100644--- a/dspy/signatures/signature.py+++ b/dspy/signatures/signature.py@@ -19,9 +19,9 @@ import astimport importlibimport inspectimport re+import sysimport typesimport typing-import sysfrom copy import deepcopyfrom typing import Any, Dict, Optional, Tuple, Type, Union@@ -71,34 +71,34 @@ class SignatureMeta(type(BaseModel)):# Get type references from caller frames by walking the stackfound_types = {}-+needed_types = set()dotted_types = {}-+for type_name in type_names:parts = type_name.split('.')base_name = parts[0]-+if base_name not in typing.__dict__ and base_name not in __builtins__:if len(parts) > 1:dotted_types[type_name] = base_nameneeded_types.add(base_name)else:needed_types.add(type_name)-+if not needed_types:return None-+frame = Nonetry:frame = sys._getframe(1) # Start one level up (skip this function)-+max_frames = 100frame_count = 0-+while frame and needed_types and frame_count < max_frames:frame_count += 1-+for type_name in list(needed_types):if type_name in frame.f_locals:found_types[type_name] = frame.f_locals[type_name]@@ -106,13 +106,13 @@ class SignatureMeta(type(BaseModel)):elif frame.f_globals and type_name in frame.f_globals:found_types[type_name] = frame.f_globals[type_name]needed_types.remove(type_name)-+# If we found all needed types, stop lookingif not needed_types:break-+frame = frame.f_back-+if needed_types and frame_count >= max_frames:import logginglogging.getLogger("dspy").warning(@@ -132,7 +132,7 @@ class SignatureMeta(type(BaseModel)):return found_types or None- def __new__(mcs, signature_name, bases, namespace, **kwargs): # noqa: N804+ def __new__(mcs, signature_name, bases, namespace, **kwargs):# At this point, the orders have been swapped already.field_order = [name for name, value in namespace.items() if isinstance(value, FieldInfo)]# Set `str` as the default type for all fields@@ -283,10 +283,7 @@ class Signature(BaseModel, metaclass=SignatureMeta):def delete(cls, name) -> Type["Signature"]:fields = dict(cls.fields)- if name in fields:- del fields[name]- else:- raise ValueError(f"Field `{name}` not found in `{cls.__name__}`.")+ fields.pop(name, None)return Signature(fields, cls.instructions)@@ -411,7 +408,7 @@ def make_signature(if custom_types:names = dict(typing.__dict__)names.update(custom_types)-+fields = _parse_signature(signature, names) if isinstance(signature, str) else signature# Validate the fields, this is important because we sometimes forget the@@ -549,15 +546,15 @@ def _parse_type_node(node, names=None) -> Any:if isinstance(node, ast.Attribute):base = _parse_type_node(node.value, names)attr_name = node.attr-+if hasattr(base, attr_name):return getattr(base, attr_name)-+if isinstance(node.value, ast.Name):full_name = f"{node.value.id}.{attr_name}"if full_name in names:return names[full_name]-+raise ValueError(f"Unknown attribute: {attr_name} on {base}")if isinstance(node, ast.Subscript):diff --git a/dspy/teleprompt/grpo.py b/dspy/teleprompt/grpo.pyindex 7ee397845..0872dd155 100644--- a/dspy/teleprompt/grpo.py+++ b/dspy/teleprompt/grpo.py@@ -298,7 +298,7 @@ class GRPO(FinetuneTeleprompter):num_student_predictors = len(student.predictors())logging.info("Preparing the teacher program(s)... We will ensure that the provided programs have the same program structure as the student program.")- if isinstance(teacher, list) and len(teacher) == 0 or teacher is None:+ if (isinstance(teacher, list) and len(teacher) == 0) or teacher is None:teacher = studentteachers = teacher if isinstance(teacher, list) else [teacher]for t in teachers:diff --git a/dspy/teleprompt/mipro_optimizer_v2.py b/dspy/teleprompt/mipro_optimizer_v2.pyindex 05dd4eec1..eae337e33 100644--- a/dspy/teleprompt/mipro_optimizer_v2.py+++ b/dspy/teleprompt/mipro_optimizer_v2.py@@ -4,9 +4,8 @@ import selectimport sysimport textwrapimport time-from typing import TYPE_CHECKINGfrom collections import defaultdict-from typing import Any, Callable, Dict, List, Literal, Optional, Tuple+from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Tupleimport numpy as npdiff --git a/dspy/utils/langchain_tool.py b/dspy/utils/langchain_tool.pyindex cce44660a..306ede8b9 100644--- a/dspy/utils/langchain_tool.py+++ b/dspy/utils/langchain_tool.py@@ -24,13 +24,13 @@ def convert_langchain_tool(tool: "BaseTool") -> Tool:result = await tool.ainvoke(kwargs)return resultexcept Exception as e:- raise RuntimeError(f"Failed to call LangChain tool {tool.name}: {str(e)}")-+ raise RuntimeError(f"Failed to call LangChain tool {tool.name}: {e!s}")+# Get args_schema from the tool# https://python.langchain.com/api_reference/core/tools/langchain_core.tools.base.BaseTool.html#langchain_core.tools.base.BaseTool.args_schemaargs_schema = tool.args_schemaargs, _, arg_desc = convert_input_schema_to_tool_args(args_schema.model_json_schema())-+# The args_schema of Langchain tool is a pydantic model, so we can get the type hints from the model fieldsarg_types = {key: field.annotation if field.annotation is not None else Any@@ -44,4 +44,4 @@ def convert_langchain_tool(tool: "BaseTool") -> Tool:args=args,arg_types=arg_types,arg_desc=arg_desc- )\ No newline at end of file+ )diff --git a/tests/adapters/test_chat_adapter.py b/tests/adapters/test_chat_adapter.pyindex 816c04a31..3d8a67b7b 100644--- a/tests/adapters/test_chat_adapter.py+++ b/tests/adapters/test_chat_adapter.py@@ -5,8 +5,6 @@ import pydanticimport pytestimport dspy-import pydantic-import json@pytest.mark.parametrize(diff --git a/tests/adapters/test_json_adapter.py b/tests/adapters/test_json_adapter.pyindex 622458114..f1449e4ef 100644--- a/tests/adapters/test_json_adapter.py+++ b/tests/adapters/test_json_adapter.py@@ -5,7 +5,6 @@ import pytestfrom litellm.utils import Choices, Message, ModelResponseimport dspy-import jsondef test_json_adapter_passes_structured_output_when_supported_by_model():diff --git a/tests/adapters/test_tool.py b/tests/adapters/test_tool.pyindex 925e9b9d4..de2f64d88 100644--- a/tests/adapters/test_tool.py+++ b/tests/adapters/test_tool.py@@ -1,12 +1,10 @@import asynciofrom typing import Any, Optional-import dspyimport pytestfrom pydantic import BaseModelfrom dspy.adapters.types.tool import Tool-from unittest import mock# Test fixtures@@ -259,7 +257,7 @@ def test_tool_str():def add(x: int, y: int = 0) -> int:"""Add two integers."""return x + y-+tool = Tool(add)assert (str(tool)diff --git a/tests/predict/test_code_act.py b/tests/predict/test_code_act.pyindex 256593264..3bebfd69b 100644--- a/tests/predict/test_code_act.py+++ b/tests/predict/test_code_act.py@@ -1,6 +1,7 @@-import pytestimport shutil+import pytest+import dspyfrom dspy import Signaturefrom dspy.predict import CodeActdiff --git a/tests/primitives/test_module.py b/tests/primitives/test_module.pyindex 498510a52..bc1d2975c 100644--- a/tests/primitives/test_module.py+++ b/tests/primitives/test_module.py@@ -1,10 +1,10 @@import loggingimport os-from litellm import ModelResponse, Message, Choicesimport threadingfrom unittest.mock import patchimport pytest+from litellm import Choices, Message, ModelResponseimport dspyfrom dspy.utils.dummies import DummyLMdiff --git a/tests/signatures/test_custom_types.py b/tests/signatures/test_custom_types.pyindex b2d0dfbaa..5393dc24d 100644--- a/tests/signatures/test_custom_types.py+++ b/tests/signatures/test_custom_types.py@@ -1,10 +1,9 @@+import pydanticimport pytest-from typing import List, Dict, Anyimport dspyfrom dspy import Signature-from dspy.utils.dummies import DummyLMdef test_basic_custom_type_resolution():@@ -18,7 +17,7 @@ def test_basic_custom_type_resolution():custom_types={"CustomType": CustomType})assert explicit_sig.input_fields["input"].annotation == CustomType-+# Custom types can also be auto-resolved from caller's scopeauto_sig = Signature("input: CustomType -> output: str")assert auto_sig.input_fields["input"].annotation == CustomType@@ -29,7 +28,7 @@ def test_type_alias_for_nested_types():class Container:class NestedType(pydantic.BaseModel):value: str-+NestedType = Container.NestedTypealias_sig = Signature("input: str -> output: NestedType")assert alias_sig.output_fields["output"].annotation == Container.NestedType@@ -66,18 +65,18 @@ class OuterContainer:def test_recommended_patterns():"""Test recommended patterns for working with custom types in signatures."""-+# PATTERN 1: Local type with auto-resolutionclass LocalType(pydantic.BaseModel):value: str-+sig1 = Signature("input: str -> output: LocalType")assert sig1.output_fields["output"].annotation == LocalType-+# PATTERN 2: Module-level type with auto-resolutionsig2 = Signature("input: str -> output: GlobalCustomType")assert sig2.output_fields["output"].annotation == GlobalCustomType-+# PATTERN 3: Nested type with dot notationsig3 = Signature("input: str -> output: OuterContainer.InnerType")assert sig3.output_fields["output"].annotation == OuterContainer.InnerType@@ -117,4 +116,4 @@ def test_basic_custom_type_resolution():assert sig.input_fields["input"].annotation == CustomTypesig = Signature("input: CustomType -> output: str")- assert sig.input_fields["input"].annotation == CustomType\ No newline at end of file+ assert sig.input_fields["input"].annotation == CustomTypediff --git a/tests/signatures/test_signature.py b/tests/signatures/test_signature.pyindex b1c79ae01..9d58e1f72 100644--- a/tests/signatures/test_signature.py+++ b/tests/signatures/test_signature.py@@ -435,4 +435,4 @@ def test_custom_type_from_different_module():path_obj = Path("/test/path")pred = dspy.Predict(test_signature)(input=path_obj)- assert pred.output == "/test/path"\ No newline at end of file+ assert pred.output == "/test/path"diff --git a/tests/streaming/test_streaming.py b/tests/streaming/test_streaming.pyindex 5caca0d1f..6019964c1 100644--- a/tests/streaming/test_streaming.py+++ b/tests/streaming/test_streaming.py@@ -1,12 +1,12 @@import os+from unittest import mock+from unittest.mock import AsyncMockimport pytest+from litellm.types.utils import Delta, ModelResponseStream, StreamingChoicesimport dspyfrom dspy.streaming import StatusMessage, StatusMessageProvider, streaming_response-from unittest import mock-from unittest.mock import AsyncMock-from litellm.types.utils import ModelResponseStream, StreamingChoices, Delta@pytest.mark.anyiodiff --git a/tests/utils/test_langchain_tool.py b/tests/utils/test_langchain_tool.pyindex ef12a2cea..8d8c8904e 100644--- a/tests/utils/test_langchain_tool.py+++ b/tests/utils/test_langchain_tool.py@@ -1,4 +1,5 @@import importlib+import pytestif importlib.util.find_spec("langchain_core") is None:@@ -41,7 +42,7 @@ async def test_convert_custom_tool_with_custom_class():def get_age(profile: Profile) -> int:"""Get the age of the profile."""return profile.age-+tool = convert_langchain_tool(get_age)assert tool.name == "get_age"assert tool.desc == "Get the age of the profile."