Skip to content

Commit cc78b46

Browse files
authored
fix BaseType annotation check (#8318)
1 parent 9f24553 commit cc78b46

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

dspy/adapters/types/base_type.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import re
3+
import inspect
34
from typing import Any, Union, get_args, get_origin
45

56
import json_repair
@@ -41,9 +42,18 @@ def extract_custom_type_from_annotation(cls, annotation):
4142
This is used to extract all custom types from the annotation of a field, while the annotation can
4243
have arbitrary level of nesting. For example, we detect `Tool` is in `list[dict[str, Tool]]`.
4344
"""
44-
# Direct match
45-
if isinstance(annotation, type) and issubclass(annotation, cls):
46-
return [annotation]
45+
# Direct match. Some typing constructs (like `typing.Any`, `TypeAlias`,
46+
# or weird internals) may pass `isinstance(..., type)` but are not
47+
# valid classes for `issubclass`. We defensively guard against this by
48+
# using `inspect.isclass` and wrapping the call in a try/except block.
49+
try:
50+
if inspect.isclass(annotation) and issubclass(annotation, cls):
51+
return [annotation]
52+
except TypeError:
53+
# `issubclass` can raise `TypeError` if the argument is not actually
54+
# a class (even if `inspect.isclass` thought otherwise). In these
55+
# cases we just ignore the annotation.
56+
return []
4757

4858
origin = get_origin(annotation)
4959
if origin is None:

0 commit comments

Comments
 (0)