def _convert_dict()

in google/generativeai/types/content_types.py [0:0]


def _convert_dict(d: Mapping) -> protos.Content | protos.Part | protos.Blob:
    if is_content_dict(d):
        content = dict(d)
        if isinstance(parts := content["parts"], str):
            content["parts"] = [parts]
        content["parts"] = [to_part(part) for part in content["parts"]]
        return protos.Content(content)
    elif is_part_dict(d):
        part = dict(d)
        if "inline_data" in part:
            part["inline_data"] = to_blob(part["inline_data"])
        if "file_data" in part:
            part["file_data"] = file_types.to_file_data(part["file_data"])
        return protos.Part(part)
    elif is_blob_dict(d):
        blob = d
        return protos.Blob(blob)
    else:
        raise KeyError(
            "Unable to determine the intended type of the `dict`. "
            "For `Content`, a 'parts' key is expected. "
            "For `Part`, either an 'inline_data' or a 'text' key is expected. "
            "For `Blob`, both 'mime_type' and 'data' keys are expected. "
            f"However, the provided dictionary has the following keys: {list(d.keys())}"
        )