Skip to content

AttributeError when handling Klaviyo API responses - inconsistent return types (object vs dict) #91

@juliocabrerizo

Description

@juliocabrerizo

Problem Description

We're encountering inconsistencies in the data type returned by the Klaviyo API client. At the beginning of the integration we are developing, the response is a Pydantic model object that can be accessed using dot notation (.attributes.updated), but now it's a dictionary that needs to be accessed using dictionary methods (.get("attributes", {}).get("updated")).

This inconsistency leads to AttributeError: 'dict' object has no attribute 'attributes' when our code expects an object but receives a dictionary.

Reproduction Steps

  1. Make calls to the Klaviyo API (e.g., create_profile, update_profile, get_profiles)
  2. Try to access response data with dot notation

Current Workaround

We've implemented a try/except pattern to handle both cases:

Object-style Response (Pydantic Model):

POST_PROFILE_RESPONSE = PostProfileResponse(
    data=PostProfileResponseData(
        type=ProfileEnum.PROFILE,
        id="01JM21MJPKEV69YZK25DG0WAOW",
        attributes=PostProfileResponseDataAttributes(
            email="example@example.com",
            phone_number="+12345678901",
            created=datetime(2025, 2, 14, 10, 56, 26, 837280, tzinfo=None),
            updated=datetime(2025, 2, 14, 10, 56, 26, 837311, tzinfo=None),
            # ...other attributes
        ),
        # ...other fields
    )
)

Dictionary-style Response:

response = [
    {
        "id": "01JM21MJPKEV69YZK25DG0WAOW",
        "attributes": {
            "email": "example@example.com",
            "phone_number": "+12345678901",
            "created": "2025-02-14T10:56:26.837280",
            "updated": "2025-02-14T10:56:26.837311",
            # ...other attributes
        },
        # ...other fields
    }
]

Request

Could you please:

  1. Standardize the response format (either always return Pydantic models or always return dictionaries)
  2. Document the expected response types for each API method
  3. Provide a helper method to convert between the formats if both are needed for backward compatibility

Environment

  • Klaviyo API client version: [17.0.0]

Thank you for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions