Genesys Python SDK token refresh loop and 401s on long-running tasks

We’ve got a Python service running on AWS Lambda that uses the Genesys Cloud Platform SDK to fetch interaction history for our custom agent desktop. The goal is to keep this thing running for up to 15 minutes to process large batches. I’m hitting a wall where the client seems to stop refreshing the access token automatically, even though the docs imply the SDK handles this transparently.

Here’s the setup:

  • SDK: genesys-cloud-py-client v2.1.4
  • Init: genesyscloud.Auth.get_oauth_client(client_id, client_secret, grant_type='client_credentials')
  • Task: Iterating through 5000 interactions, calling conversation_api.get_conversation_details repeatedly.

The code works fine for the first few hundred calls. Then, suddenly, I start getting 401 Unauthorized errors on random subsequent calls. It’s not a clean failure at the 60-minute mark. It happens sporadically around the 10-minute mark of execution.

I’ve tried explicitly calling oauth_client.refresh_token() before each API call, but that returns a 200 OK with a new token, yet the next API call still fails with a 401. It feels like the underlying HTTP session or the internal token cache in the SDK is getting out of sync with what the API expects.

I checked the network traffic using a proxy. The SDK is sending the old token in the header for the failing requests. The refresh_token method seems to update the internal state, but the actual request object being passed to the API client isn’t picking up the new token immediately.

Is there a known issue with the Python SDK’s automatic refresh logic in long-running processes? Should I be instantiating a new ApiClient object after every refresh, or is there a specific method I’m missing to force the client to use the latest token? The docs are a bit vague on the internal mechanics of the refresh loop.