Ok, let me say this plain and clear, for the thousandth time:
There is no FROM address in the bitcoin system. You are asking a question that is without meaning in the system. The software you are writing will not do what you think it does, even if it appears to work more-or-less correctly most of the time.
Can you give me an example when this wont work if the transaction is sent from a client like bitcoin-qt?
It depends what you mean by "work". I haven't read your script in detail, but I'm willing to take it on faith that it is capable of finding one or all of the pubkeys that previously controlled one or all of the inputs of the transaction that was sent. In that sense, it can probably "work".
On the other hand, no one cares about that. They want a return address where winnings or refunds can be sent. And the bitcoin system has no concept for that. Transactions do not identify people or entities, only keys. And keys do not identify people or entities either. In this sense, this won't "work" when the bitcoin-qt that the payment is sent from is not owned by the person nominally making the payment, for example if I make the payment
for someone else.