construct_type() — anthropic-sdk-python Function Reference
Architecture documentation for the construct_type() function in _models.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD 1f1fa4b5_5943_136f_2cef_23465a8f7aee["construct_type()"] 3912cc3f_b0e8_a732_b8e2_613b018b830d["_models.py"] 1f1fa4b5_5943_136f_2cef_23465a8f7aee -->|defined in| 3912cc3f_b0e8_a732_b8e2_613b018b830d 518c8bd6_474a_7933_381f_46a8c55e9f51["_process_response_data()"] 518c8bd6_474a_7933_381f_46a8c55e9f51 -->|calls| 1f1fa4b5_5943_136f_2cef_23465a8f7aee 57e8da7b_af31_35a8_78f7_c274f7fbb350["construct()"] 57e8da7b_af31_35a8_78f7_c274f7fbb350 -->|calls| 1f1fa4b5_5943_136f_2cef_23465a8f7aee f18cfc0d_b1d8_c1ad_81bd_c1f4634e0a56["_construct_field()"] f18cfc0d_b1d8_c1ad_81bd_c1f4634e0a56 -->|calls| 1f1fa4b5_5943_136f_2cef_23465a8f7aee 0ea38c39_d71d_a0c9_d116_abfa98f7d429["build()"] 0ea38c39_d71d_a0c9_d116_abfa98f7d429 -->|calls| 1f1fa4b5_5943_136f_2cef_23465a8f7aee f1aa7565_7bdb_7819_e2bd_73da6f5bb2af["construct_type_unchecked()"] f1aa7565_7bdb_7819_e2bd_73da6f5bb2af -->|calls| 1f1fa4b5_5943_136f_2cef_23465a8f7aee 18b1ba83_3e17_a5a3_fd52_d68fd9b6f945["validate_type()"] 1f1fa4b5_5943_136f_2cef_23465a8f7aee -->|calls| 18b1ba83_3e17_a5a3_fd52_d68fd9b6f945 d05ec88e_2ca5_0a17_2c96_e788b0ef33ff["_build_discriminated_union_meta()"] 1f1fa4b5_5943_136f_2cef_23465a8f7aee -->|calls| d05ec88e_2ca5_0a17_2c96_e788b0ef33ff afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7["get()"] 1f1fa4b5_5943_136f_2cef_23465a8f7aee -->|calls| afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7 20614ced_60ce_b78c_1bcd_09af0463ff5b["parse_date()"] 1f1fa4b5_5943_136f_2cef_23465a8f7aee -->|calls| 20614ced_60ce_b78c_1bcd_09af0463ff5b 57e8da7b_af31_35a8_78f7_c274f7fbb350["construct()"] 1f1fa4b5_5943_136f_2cef_23465a8f7aee -->|calls| 57e8da7b_af31_35a8_78f7_c274f7fbb350 style 1f1fa4b5_5943_136f_2cef_23465a8f7aee fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/anthropic/_models.py lines 498–615
def construct_type(*, value: object, type_: object, metadata: Optional[List[Any]] = None) -> object:
"""Loose coercion to the expected type with construction of nested values.
If the given value does not match the expected type then it is returned as-is.
"""
# store a reference to the original type we were given before we extract any inner
# types so that we can properly resolve forward references in `TypeAliasType` annotations
original_type = None
# we allow `object` as the input type because otherwise, passing things like
# `Literal['value']` will be reported as a type error by type checkers
type_ = cast("type[object]", type_)
if is_type_alias_type(type_):
original_type = type_ # type: ignore[unreachable]
type_ = type_.__value__ # type: ignore[unreachable]
# unwrap `Annotated[T, ...]` -> `T`
if metadata is not None and len(metadata) > 0:
meta: tuple[Any, ...] = tuple(metadata)
elif is_annotated_type(type_):
meta = get_args(type_)[1:]
type_ = extract_type_arg(type_, 0)
else:
meta = tuple()
# we need to use the origin class for any types that are subscripted generics
# e.g. Dict[str, object]
origin = get_origin(type_) or type_
args = get_args(type_)
if is_union(origin):
try:
return validate_type(type_=cast("type[object]", original_type or type_), value=value)
except Exception:
pass
# if the type is a discriminated union then we want to construct the right variant
# in the union, even if the data doesn't match exactly, otherwise we'd break code
# that relies on the constructed class types, e.g.
#
# class FooType:
# kind: Literal['foo']
# value: str
#
# class BarType:
# kind: Literal['bar']
# value: int
#
# without this block, if the data we get is something like `{'kind': 'bar', 'value': 'foo'}` then
# we'd end up constructing `FooType` when it should be `BarType`.
discriminator = _build_discriminated_union_meta(union=type_, meta_annotations=meta)
if discriminator and is_mapping(value):
variant_value = value.get(discriminator.field_alias_from or discriminator.field_name)
if variant_value and isinstance(variant_value, str):
variant_type = discriminator.mapping.get(variant_value)
if variant_type:
return construct_type(type_=variant_type, value=value)
# if the data is not valid, use the first variant that doesn't fail while deserializing
for variant in args:
try:
return construct_type(value=value, type_=variant)
except Exception:
continue
raise RuntimeError(f"Could not convert data into a valid instance of {type_}")
if origin == dict:
if not is_mapping(value):
return value
_, items_type = get_args(type_) # Dict[_, items_type]
return {key: construct_type(value=item, type_=items_type) for key, item in value.items()}
if (
not is_literal_type(type_)
and inspect.isclass(origin)
and (issubclass(origin, BaseModel) or issubclass(origin, GenericModel))
):
if is_list(value):
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does construct_type() do?
construct_type() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_models.py.
Where is construct_type() defined?
construct_type() is defined in src/anthropic/_models.py at line 498.
What does construct_type() call?
construct_type() calls 5 function(s): _build_discriminated_union_meta, construct, get, parse_date, validate_type.
What calls construct_type()?
construct_type() is called by 5 function(s): _construct_field, _process_response_data, build, construct, construct_type_unchecked.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free