NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'

I have a python package wherein I have the rpy2 code to run my R package in python. I've tested this package in a notebook and the function that I use to run the R code works without any issues. However, when I try to run the function through an API that I'm building, it gives me Not implemented error. I'm testing everything on my local including the API, so I'm not sure why would rpy2 work only in a notebook. My python package has the following structure

from rpy2.robjects import r
from rpy2.robjects.packages import importr
myrpackage = importr('myrpackage')
def myfunc():
r_code = ("1+1")
return r(r_code)
🔴 No definitive solution yet