Python SDK token refresh failing mid-batch for WFM data export

Hey everyone,

I’m running a Python script to pull agent adherence data for our WFM team. It works fine for small datasets, but when I hit larger batches, the script crashes halfway through.

The issue seems to be the access token expiring. I’m using the `` Python SDK and initializing the client like this:

from .platform.client import Client

def get_client():
 client = Client(
 environment='mypurecloud.com',
 client_id='my-client-id',
 client_secret='my-client-secret'
 )
 return client

I call get_client() once at the start of the script and reuse the instance for multiple API calls to /api/v2/analytics/wfmsummary/details/query. After about 30-40 requests, I start getting 401 Unauthorized errors. The SDK doesn’t seem to automatically refresh the token in the middle of the loop.

I’ve tried catching the exception and re-initializing the client, but that feels like a hack. Is there a proper way to handle token refresh within the SDK, or do I need to implement my own retry logic with token expiry checks?

Here’s the error snippet:

{"code":"unauthorized","message":"The access token is expired or invalid"}

Any pointers on best practices for long-running scripts with this SDK?