_transform_recursive() — anthropic-sdk-python Function Reference
Architecture documentation for the _transform_recursive() function in _transform.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD 76cf4787_3c42_ee68_9967_3160c7a1aa0d["_transform_recursive()"] 17ea9c01_2df6_304d_71e2_31af17ed7395["_transform.py"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|defined in| 17ea9c01_2df6_304d_71e2_31af17ed7395 d6615324_bb52_2f02_df89_851d5be298c2["transform()"] d6615324_bb52_2f02_df89_851d5be298c2 -->|calls| 76cf4787_3c42_ee68_9967_3160c7a1aa0d 73674319_0076_f928_202c_b195fd052f6b["_transform_typeddict()"] 73674319_0076_f928_202c_b195fd052f6b -->|calls| 76cf4787_3c42_ee68_9967_3160c7a1aa0d 6db2b331_e127_3cb2_32c8_72a1730d0041["_async_transform_recursive()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 76cf4787_3c42_ee68_9967_3160c7a1aa0d f1c6c359_ce1b_eb71_916d_f5fa21bbcd3d["strip_annotated_type()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| f1c6c359_ce1b_eb71_916d_f5fa21bbcd3d 17854057_420d_9158_b387_597d6c79873e["get_origin()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 17854057_420d_9158_b387_597d6c79873e 0f0a7322_bca0_54aa_b4a9_622c0c790efb["is_typeddict()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 0f0a7322_bca0_54aa_b4a9_622c0c790efb b41b21ba_47c5_2c24_a6d6_d106399b3c57["is_mapping()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| b41b21ba_47c5_2c24_a6d6_d106399b3c57 73674319_0076_f928_202c_b195fd052f6b["_transform_typeddict()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 73674319_0076_f928_202c_b195fd052f6b a81fb697_e7fd_351f_22da_edb95ea267b6["get_args()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| a81fb697_e7fd_351f_22da_edb95ea267b6 02d7607e_c938_0626_4987_d7b4228394e8["is_list_type()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 02d7607e_c938_0626_4987_d7b4228394e8 106ba2ee_6caf_3f30_1f8a_f3ebc5755de0["is_list()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 106ba2ee_6caf_3f30_1f8a_f3ebc5755de0 80cfdf46_d7df_6d42_a4f0_e4d080fe30c5["is_iterable_type()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 80cfdf46_d7df_6d42_a4f0_e4d080fe30c5 6136e8f7_248c_4449_ec8e_4711c4295b45["is_iterable()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 6136e8f7_248c_4449_ec8e_4711c4295b45 8660e000_c893_852d_2708_5b5d63f76290["is_sequence_type()"] 76cf4787_3c42_ee68_9967_3160c7a1aa0d -->|calls| 8660e000_c893_852d_2708_5b5d63f76290 style 76cf4787_3c42_ee68_9967_3160c7a1aa0d fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/anthropic/_utils/_transform.py lines 154–233
def _transform_recursive(
data: object,
*,
annotation: type,
inner_type: type | None = None,
) -> object:
"""Transform the given data against the expected type.
Args:
annotation: The direct type annotation given to the particular piece of data.
This may or may not be wrapped in metadata types, e.g. `Required[T]`, `Annotated[T, ...]` etc
inner_type: If applicable, this is the "inside" type. This is useful in certain cases where the outside type
is a container type such as `List[T]`. In that case `inner_type` should be set to `T` so that each entry in
the list can be transformed using the metadata from the container type.
Defaults to the same value as the `annotation` argument.
"""
from .._compat import model_dump
if inner_type is None:
inner_type = annotation
stripped_type = strip_annotated_type(inner_type)
origin = get_origin(stripped_type) or stripped_type
if is_typeddict(stripped_type) and is_mapping(data):
return _transform_typeddict(data, stripped_type)
if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
if (
# List[T]
(is_list_type(stripped_type) and is_list(data))
# Iterable[T]
or (is_iterable_type(stripped_type) and is_iterable(data) and not isinstance(data, str))
# Sequence[T]
or (is_sequence_type(stripped_type) and is_sequence(data) and not isinstance(data, str))
):
# dicts are technically iterable, but it is an iterable on the keys of the dict and is not usually
# intended as an iterable, so we don't transform it.
if isinstance(data, dict):
return cast(object, data)
inner_type = extract_type_arg(stripped_type, 0)
if _no_transform_needed(inner_type):
# for some types there is no need to transform anything, so we can get a small
# perf boost from skipping that work.
#
# but we still need to convert to a list to ensure the data is json-serializable
if is_list(data):
return data
return list(data)
return [_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]
if is_union_type(stripped_type):
# For union types we run the transformation against all subtypes to ensure that everything is transformed.
#
# TODO: there may be edge cases where the same normalized field name will transform to two different names
# in different subtypes.
for subtype in get_args(stripped_type):
data = _transform_recursive(data, annotation=annotation, inner_type=subtype)
return data
if isinstance(data, pydantic.BaseModel):
return model_dump(data, exclude_unset=True, mode="json", exclude=getattr(data, "__api_exclude__", None))
annotated_type = _get_annotated_type(annotation)
if annotated_type is None:
return data
# ignore the first argument as it is the actual type
annotations = get_args(annotated_type)[1:]
for annotation in annotations:
if isinstance(annotation, PropertyInfo) and annotation.format is not None:
return _format_data(data, annotation.format, annotation.format_template)
return data
Domain
Subdomains
Defined In
Calls
Source
Frequently Asked Questions
What does _transform_recursive() do?
_transform_recursive() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_utils/_transform.py.
Where is _transform_recursive() defined?
_transform_recursive() is defined in src/anthropic/_utils/_transform.py at line 154.
What does _transform_recursive() call?
_transform_recursive() calls 17 function(s): _format_data, _get_annotated_type, _no_transform_needed, _transform_typeddict, extract_type_arg, get_args, get_origin, is_iterable, and 9 more.
What calls _transform_recursive()?
_transform_recursive() is called by 3 function(s): _async_transform_recursive, _transform_typeddict, transform.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free