I found out where the problem was.
I was appending my messages to the history but not the GPT answers so it was unable to understand that questions were already answered.
There's is only one line to add just before the "return" statement:
messages.append({'role': 'assistant', 'content': ai_msg.content})