DSPy: The framework for programming—not prompting—language models
Python
Fix the custom type extraction in dspy.BaseType (#8320)
Chen Qian4f154a7cac43
diff --git a/dspy/adapters/types/base_type.py b/dspy/adapters/types/base_type.pyindex a2b71339f..eece3469e 100644--- a/dspy/adapters/types/base_type.py+++ b/dspy/adapters/types/base_type.py@@ -1,6 +1,5 @@import jsonimport re-import inspectfrom typing import Any, Union, get_args, get_originimport json_repair@@ -42,18 +41,13 @@ class BaseType(pydantic.BaseModel):This is used to extract all custom types from the annotation of a field, while the annotation canhave arbitrary level of nesting. For example, we detect `Tool` is in `list[dict[str, Tool]]`."""- # Direct match. Some typing constructs (like `typing.Any`, `TypeAlias`,- # or weird internals) may pass `isinstance(..., type)` but are not- # valid classes for `issubclass`. We defensively guard against this by- # using `inspect.isclass` and wrapping the call in a try/except block.+ # Direct match. Nested type like `list[dict[str, Event]]` passes `isinstance(annotation, type)` in python 3.10+ # while fails in python 3.11. To accomodate users using python 3.10, we need to capture the error and ignore it.try:- if inspect.isclass(annotation) and issubclass(annotation, cls):+ if isinstance(annotation, type) and issubclass(annotation, cls):return [annotation]except TypeError:- # `issubclass` can raise `TypeError` if the argument is not actually- # a class (even if `inspect.isclass` thought otherwise). In these- # cases we just ignore the annotation.- return []+ passorigin = get_origin(annotation)if origin is None:diff --git a/tests/adapters/test_base_type.py b/tests/adapters/test_base_type.pynew file mode 100644index 000000000..878e11cda--- /dev/null+++ b/tests/adapters/test_base_type.py@@ -0,0 +1,53 @@+import dspy+from typing import Optional+import pydantic+++def test_basic_extract_custom_type_from_annotation():+ class Event(dspy.BaseType):+ event_name: str+ start_date_time: str+ end_date_time: Optional[str]+ location: Optional[str]++ class ExtractEvent(dspy.Signature):+ """Extract all events from the email content."""++ email: str = dspy.InputField()+ event: Event = dspy.OutputField()++ assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvent.output_fields["event"].annotation) == [Event]++ class ExtractEvents(dspy.Signature):+ """Extract all events from the email content."""++ email: str = dspy.InputField()+ events: list[Event] = dspy.OutputField()++ assert dspy.BaseType.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):+ event_name: str+ start_date_time: str+ end_date_time: Optional[str]+ location: Optional[str]++ class EventIdentifier(dspy.BaseType):+ model_config = pydantic.ConfigDict(frozen=True) # Make it hashable+ event_id: str+ event_name: str++ class ExtractEvents(dspy.Signature):+ """Extract all events from the email content."""++ email: str = dspy.InputField()+ events: list[dict[EventIdentifier, Event]] = dspy.OutputField()++ assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [+ EventIdentifier,+ Event,+ ]