From 9f7c7d4fb55e1d17ff5f7686ff01aac799ea97c0 Mon Sep 17 00:00:00 2001 From: PHclaw Date: Thu, 4 Jun 2026 15:00:16 +0800 Subject: [PATCH] Fix construct_type() crash on bare list/dict types Fix IndexError when origin==list with no type args (e.g. list instead of list[T]). Fix ValueError when origin==dict with no type args (e.g. dict instead of dict[K, V]). Fixes #1626 (bare list). Fixes #1619 (bare dict). --- src/anthropic/_models.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/anthropic/_models.py b/src/anthropic/_models.py index dc00516bc..c48f1a122 100644 --- a/src/anthropic/_models.py +++ b/src/anthropic/_models.py @@ -647,7 +647,10 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any] if not is_mapping(value): return value - _, items_type = get_args(type_) # Dict[_, items_type] + args_tuple = get_args(type_) + if len(args_tuple) < 2: + return value + _, items_type = args_tuple # Dict[_, items_type] return {key: construct_type(value=item, type_=items_type) for key, item in value.items()} if ( @@ -668,6 +671,8 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any] if not is_list(value): return value + if not args: + return value inner_type = args[0] # List[inner_type] return [construct_type(value=entry, type_=inner_type) for entry in value]