_async_transform_recursive() — anthropic-sdk-python Function Reference
Architecture documentation for the _async_transform_recursive() function in _transform.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD 6db2b331_e127_3cb2_32c8_72a1730d0041["_async_transform_recursive()"] 17ea9c01_2df6_304d_71e2_31af17ed7395["_transform.py"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|defined in| 17ea9c01_2df6_304d_71e2_31af17ed7395 7450322a_5cbb_6034_d7f1_29c32ca76e4b["async_transform()"] 7450322a_5cbb_6034_d7f1_29c32ca76e4b -->|calls| 6db2b331_e127_3cb2_32c8_72a1730d0041 15bcb228_a48d_a6a6_e8ae_a9233b3a1bae["_async_transform_typeddict()"] 15bcb228_a48d_a6a6_e8ae_a9233b3a1bae -->|calls| 6db2b331_e127_3cb2_32c8_72a1730d0041 f1c6c359_ce1b_eb71_916d_f5fa21bbcd3d["strip_annotated_type()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| f1c6c359_ce1b_eb71_916d_f5fa21bbcd3d 17854057_420d_9158_b387_597d6c79873e["get_origin()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 17854057_420d_9158_b387_597d6c79873e 0f0a7322_bca0_54aa_b4a9_622c0c790efb["is_typeddict()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 0f0a7322_bca0_54aa_b4a9_622c0c790efb b41b21ba_47c5_2c24_a6d6_d106399b3c57["is_mapping()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| b41b21ba_47c5_2c24_a6d6_d106399b3c57 15bcb228_a48d_a6a6_e8ae_a9233b3a1bae["_async_transform_typeddict()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 15bcb228_a48d_a6a6_e8ae_a9233b3a1bae a81fb697_e7fd_351f_22da_edb95ea267b6["get_args()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| a81fb697_e7fd_351f_22da_edb95ea267b6 76cf4787_3c42_ee68_9967_3160c7a1aa0d["_transform_recursive()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 76cf4787_3c42_ee68_9967_3160c7a1aa0d 02d7607e_c938_0626_4987_d7b4228394e8["is_list_type()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 02d7607e_c938_0626_4987_d7b4228394e8 106ba2ee_6caf_3f30_1f8a_f3ebc5755de0["is_list()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 106ba2ee_6caf_3f30_1f8a_f3ebc5755de0 80cfdf46_d7df_6d42_a4f0_e4d080fe30c5["is_iterable_type()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 80cfdf46_d7df_6d42_a4f0_e4d080fe30c5 6136e8f7_248c_4449_ec8e_4711c4295b45["is_iterable()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 6136e8f7_248c_4449_ec8e_4711c4295b45 8660e000_c893_852d_2708_5b5d63f76290["is_sequence_type()"] 6db2b331_e127_3cb2_32c8_72a1730d0041 -->|calls| 8660e000_c893_852d_2708_5b5d63f76290 style 6db2b331_e127_3cb2_32c8_72a1730d0041 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/anthropic/_utils/_transform.py lines 320–399
async def _async_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 await _async_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 [await _async_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 = await _async_transform_recursive(data, annotation=annotation, inner_type=subtype)
return data
if isinstance(data, pydantic.BaseModel):
return model_dump(data, exclude_unset=True, mode="json")
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 await _async_format_data(data, annotation.format, annotation.format_template)
return data
Domain
Subdomains
Defined In
Calls
- _async_format_data()
- _async_transform_typeddict()
- _get_annotated_type()
- _no_transform_needed()
- _transform_recursive()
- extract_type_arg()
- get_args()
- get_origin()
- is_iterable()
- is_iterable_type()
- is_list()
- is_list_type()
- is_mapping()
- is_sequence()
- is_sequence_type()
- is_typeddict()
- is_union_type()
- strip_annotated_type()
Source
Frequently Asked Questions
What does _async_transform_recursive() do?
_async_transform_recursive() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_utils/_transform.py.
Where is _async_transform_recursive() defined?
_async_transform_recursive() is defined in src/anthropic/_utils/_transform.py at line 320.
What does _async_transform_recursive() call?
_async_transform_recursive() calls 18 function(s): _async_format_data, _async_transform_typeddict, _get_annotated_type, _no_transform_needed, _transform_recursive, extract_type_arg, get_args, get_origin, and 10 more.
What calls _async_transform_recursive()?
_async_transform_recursive() is called by 2 function(s): _async_transform_typeddict, async_transform.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free