File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change 11import json
22import re
3+ import inspect
34from typing import Any , Union , get_args , get_origin
45
56import 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 :
You can’t perform that action at this time.
0 commit comments