Apparently, after a bunch of trial and error we figured out that code above is correct but one needs to have it running against python 3.11+. Older versions won't allow such dynamic definition of literals. I am not a real proper software developer so I can't comment on root reasons for that, but once we just tried the approach from my initial question in the new environment with latest python version it just worked.