Add logging to find it
Print statements, but placed deliberately.
I need to find where this goes wrong: <paste the code and describe the symptom> Add logging that would identify the failure point. For each line you add, tell me what a wrong value there would prove. Log the shape and content of data at each boundary — going in, coming out — not just "reached here". I need to see where the data stops being what I expect. Keep it to <6> log lines. I want the smallest set that narrows it, not a trace of everything.
Log values, not milestones. "Got here" tells you the path; the actual value tells you what went wrong, and usually why.