DSPy: The framework for programming—not prompting—language models
Python
Rename dspy.BaseType to dspy.Type (#8510)
Chen Qian538fbb3a4c49
diff --git a/dspy/__init__.py b/dspy/__init__.pyindex 9113e4f5e..f2e3c20a9 100644--- a/dspy/__init__.py+++ b/dspy/__init__.py@@ -6,7 +6,7 @@ from dspy.teleprompt import *from dspy.evaluate import Evaluate # isort: skipfrom dspy.clients import * # isort: skip-from dspy.adapters import Adapter, ChatAdapter, JSONAdapter, XMLAdapter, TwoStepAdapter, Image, Audio, History, BaseType, Tool, ToolCalls # isort: skip+from dspy.adapters import Adapter, ChatAdapter, JSONAdapter, XMLAdapter, TwoStepAdapter, Image, Audio, History, Type, Tool, ToolCalls # isort: skipfrom dspy.utils.logging_utils import configure_dspy_loggers, disable_logging, enable_loggingfrom dspy.utils.asyncify import asyncifyfrom dspy.utils.saving import loaddiff --git a/dspy/adapters/__init__.py b/dspy/adapters/__init__.pyindex 6d342df33..c2c6d23a3 100644--- a/dspy/adapters/__init__.py+++ b/dspy/adapters/__init__.py@@ -2,13 +2,13 @@ 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 Audio, BaseType, History, Image, Tool, ToolCalls+from dspy.adapters.types import Audio, History, Image, Tool, ToolCalls, Typefrom dspy.adapters.xml_adapter import XMLAdapter__all__ = ["Adapter","ChatAdapter",- "BaseType",+ "Type","History","Image","Audio",diff --git a/dspy/adapters/types/__init__.py b/dspy/adapters/types/__init__.pyindex 68607e527..bc57d50d5 100644--- a/dspy/adapters/types/__init__.py+++ b/dspy/adapters/types/__init__.py@@ -1,7 +1,7 @@from dspy.adapters.types.audio import Audio-from dspy.adapters.types.base_type import BaseType+from dspy.adapters.types.base_type import Typefrom dspy.adapters.types.history import Historyfrom dspy.adapters.types.image import Imagefrom dspy.adapters.types.tool import Tool, ToolCalls-__all__ = ["History", "Image", "Audio", "BaseType", "Tool", "ToolCalls"]+__all__ = ["History", "Image", "Audio", "Type", "Tool", "ToolCalls"]diff --git a/dspy/adapters/types/audio.py b/dspy/adapters/types/audio.pyindex b4637a063..83e041562 100644--- a/dspy/adapters/types/audio.py+++ b/dspy/adapters/types/audio.py@@ -7,7 +7,7 @@ from typing import Any, Unionimport pydanticimport requests-from dspy.adapters.types.base_type import BaseType+from dspy.adapters.types.base_type import Typetry:import soundfile as sf@@ -17,7 +17,7 @@ except ImportError:SF_AVAILABLE = False-class Audio(BaseType):+class Audio(Type):data: straudio_format: strdiff --git a/dspy/adapters/types/base_type.py b/dspy/adapters/types/base_type.pyindex 4ac90970d..e35369227 100644--- a/dspy/adapters/types/base_type.py+++ b/dspy/adapters/types/base_type.py@@ -9,7 +9,7 @@ CUSTOM_TYPE_START_IDENTIFIER = "<<CUSTOM-TYPE-START-IDENTIFIER>>"CUSTOM_TYPE_END_IDENTIFIER = "<<CUSTOM-TYPE-END-IDENTIFIER>>"-class BaseType(pydantic.BaseModel):+class Type(pydantic.BaseModel):"""Base class to support creating custom types for DSPy signatures.This is the parent class of DSPy custom types, e.g, dspy.Image. Subclasses must implement the `format` method to@@ -18,7 +18,7 @@ class BaseType(pydantic.BaseModel):Example:```python- class Image(BaseType):+ class Image(Type):url: strdef format(self) -> list[dict[str, Any]]:@@ -85,7 +85,7 @@ def split_message_content_for_custom_types(messages: list[dict[str, Any]]) -> liThis is implemented by finding the `<<CUSTOM-TYPE-START-IDENTIFIER>>` and `<<CUSTOM-TYPE-END-IDENTIFIER>>`in the user message content and splitting the content around them. The `<<CUSTOM-TYPE-START-IDENTIFIER>>`- and `<<CUSTOM-TYPE-END-IDENTIFIER>>` are the reserved identifiers for the custom types as in `dspy.BaseType`.+ and `<<CUSTOM-TYPE-END-IDENTIFIER>>` are the reserved identifiers for the custom types as in `dspy.Type`.Args:messages: a list of messages sent to the LM. The format is the same as [OpenAI API's messagesdiff --git a/dspy/adapters/types/image.py b/dspy/adapters/types/image.pyindex e8b44ffe3..32fb786ed 100644--- a/dspy/adapters/types/image.py+++ b/dspy/adapters/types/image.py@@ -8,7 +8,7 @@ from urllib.parse import urlparseimport pydanticimport requests-from dspy.adapters.types.base_type import BaseType+from dspy.adapters.types.base_type import Typetry:from PIL import Image as PILImage@@ -18,7 +18,7 @@ except ImportError:PIL_AVAILABLE = False-class Image(BaseType):+class Image(Type):url: strmodel_config = {diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.pyindex c2ca4990c..8e4aeda72 100644--- a/dspy/adapters/types/tool.py+++ b/dspy/adapters/types/tool.py@@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Any, Callable, Tuple, Type, get_origin, get_tyfrom jsonschema import ValidationError, validatefrom pydantic import BaseModel, TypeAdapter, create_model-from dspy.adapters.types.base_type import BaseType+from dspy.adapters.types.base_type import Typefrom dspy.dsp.utils.settings import settingsfrom dspy.utils.callback import with_callbacks@@ -16,7 +16,7 @@ if TYPE_CHECKING:_TYPE_MAPPING = {"string": str, "integer": int, "number": float, "boolean": bool, "array": list, "object": dict}-class Tool(BaseType):+class Tool(Type):"""Tool class.This class is used to simplify the creation of tools for tool calling (function calling) in LLMs. Only supports@@ -254,7 +254,7 @@ class Tool(BaseType):return f"{self.name}{desc} {arg_desc}"-class ToolCalls(BaseType):+class ToolCalls(Type):class ToolCall(BaseModel):name: strargs: dict[str, Any]@@ -303,7 +303,8 @@ class ToolCalls(BaseType):"name": tool_call.name,"arguments": tool_call.args,},- } for tool_call in self.tool_calls+ }+ for tool_call in self.tool_calls],}]diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.pyindex 6632d325c..476765da2 100644--- a/dspy/adapters/utils.py+++ b/dspy/adapters/utils.py@@ -10,7 +10,7 @@ import pydanticfrom pydantic import TypeAdapterfrom pydantic.fields import FieldInfo-from dspy.adapters.types.base_type import BaseType+from dspy.adapters.types.base_type import Typefrom dspy.signatures.utils import get_dspy_field_type@@ -204,7 +204,7 @@ def get_field_description_string(fields: dict) -> str:field_message += f" ({get_annotation_name(v.annotation)})"desc = v.json_schema_extra["desc"] if v.json_schema_extra["desc"] != f"${{{k}}}" else ""- custom_types = BaseType.extract_custom_type_from_annotation(v.annotation)+ custom_types = Type.extract_custom_type_from_annotation(v.annotation)for custom_type in custom_types:if len(custom_type.description()) > 0:desc += f"\n Type description of {get_annotation_name(custom_type)}: {custom_type.description()}"diff --git a/tests/adapters/test_base_type.py b/tests/adapters/test_base_type.pyindex 1776d3192..b7d308642 100644--- a/tests/adapters/test_base_type.py+++ b/tests/adapters/test_base_type.py@@ -1,11 +1,10 @@-import pydanticimport dspydef test_basic_extract_custom_type_from_annotation():- class Event(dspy.BaseType):+ class Event(dspy.Type):event_name: strstart_date_time: strend_date_time: str | None@@ -17,7 +16,7 @@ def test_basic_extract_custom_type_from_annotation():email: str = dspy.InputField()event: Event = dspy.OutputField()- assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvent.output_fields["event"].annotation) == [Event]+ assert dspy.Type.extract_custom_type_from_annotation(ExtractEvent.output_fields["event"].annotation) == [Event]class ExtractEvents(dspy.Signature):"""Extract all events from the email content."""@@ -25,19 +24,17 @@ def test_basic_extract_custom_type_from_annotation():email: str = dspy.InputField()events: list[Event] = dspy.OutputField()- assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [- Event- ]+ assert dspy.Type.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [Event]def test_extract_custom_type_from_annotation_with_nested_type():- class Event(dspy.BaseType):+ class Event(dspy.Type):event_name: strstart_date_time: strend_date_time: str | Nonelocation: str | None- class EventIdentifier(dspy.BaseType):+ class EventIdentifier(dspy.Type):model_config = pydantic.ConfigDict(frozen=True) # Make it hashableevent_id: strevent_name: str@@ -48,7 +45,7 @@ def test_extract_custom_type_from_annotation_with_nested_type():email: str = dspy.InputField()events: list[dict[EventIdentifier, Event]] = dspy.OutputField()- assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [+ assert dspy.Type.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [EventIdentifier,Event,]