Home / Function/ _extract_items() — anthropic-sdk-python Function Reference

_extract_items() — anthropic-sdk-python Function Reference

Architecture documentation for the _extract_items() function in _utils.py from the anthropic-sdk-python codebase.

Function python AnthropicClient AsyncAPI calls 4 called by 1

Entity Profile

Dependency Diagram

graph TD
  3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4["_extract_items()"]
  875202ec_3744_577d_9ec4_ed9fbc6aaf41["_utils.py"]
  3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4 -->|defined in| 875202ec_3744_577d_9ec4_ed9fbc6aaf41
  4a5c63c8_b631_4371_7b42_e932e335f345["extract_files()"]
  4a5c63c8_b631_4371_7b42_e932e335f345 -->|calls| 3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4
  134c332e_654b_985e_839c_e13698f1b9e7["is_given()"]
  3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4 -->|calls| 134c332e_654b_985e_839c_e13698f1b9e7
  106ba2ee_6caf_3f30_1f8a_f3ebc5755de0["is_list()"]
  3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4 -->|calls| 106ba2ee_6caf_3f30_1f8a_f3ebc5755de0
  aac400f1_87c8_65d8_8fb1_d1ee593fa07b["is_dict()"]
  3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4 -->|calls| aac400f1_87c8_65d8_8fb1_d1ee593fa07b
  5d1e81d6_eb6c_2f14_b670_935cdaaf55b3["flatten()"]
  3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4 -->|calls| 5d1e81d6_eb6c_2f14_b670_935cdaaf55b3
  style 3cc9768b_7fb8_a4f4_5e9d_bdc7bcb1b1a4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_utils/_utils.py lines 56–126

def _extract_items(
    obj: object,
    path: Sequence[str],
    *,
    index: int,
    flattened_key: str | None,
) -> list[tuple[str, FileTypes]]:
    try:
        key = path[index]
    except IndexError:
        if not is_given(obj):
            # no value was provided - we can safely ignore
            return []

        # cyclical import
        from .._files import assert_is_file_content

        # We have exhausted the path, return the entry we found.
        assert flattened_key is not None

        if is_list(obj):
            files: list[tuple[str, FileTypes]] = []
            for entry in obj:
                assert_is_file_content(entry, key=flattened_key + "[]" if flattened_key else "")
                files.append((flattened_key + "[]", cast(FileTypes, entry)))
            return files

        assert_is_file_content(obj, key=flattened_key)
        return [(flattened_key, cast(FileTypes, obj))]

    index += 1
    if is_dict(obj):
        try:
            # We are at the last entry in the path so we must remove the field
            if (len(path)) == index:
                item = obj.pop(key)
            else:
                item = obj[key]
        except KeyError:
            # Key was not present in the dictionary, this is not indicative of an error
            # as the given path may not point to a required field. We also do not want
            # to enforce required fields as the API may differ from the spec in some cases.
            return []
        if flattened_key is None:
            flattened_key = key
        else:
            flattened_key += f"[{key}]"
        return _extract_items(
            item,
            path,
            index=index,
            flattened_key=flattened_key,
        )
    elif is_list(obj):
        if key != "<array>":
            return []

        return flatten(
            [
                _extract_items(
                    item,
                    path,
                    index=index,
                    flattened_key=flattened_key + "[]" if flattened_key is not None else "[]",
                )
                for item in obj
            ]
        )

    # Something unexpected was passed, just ignore it.
    return []

Subdomains

Called By

Frequently Asked Questions

What does _extract_items() do?
_extract_items() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_utils/_utils.py.
Where is _extract_items() defined?
_extract_items() is defined in src/anthropic/_utils/_utils.py at line 56.
What does _extract_items() call?
_extract_items() calls 4 function(s): flatten, is_dict, is_given, is_list.
What calls _extract_items()?
_extract_items() is called by 1 function(s): extract_files.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free