Error from 'warnings' module in workflow code in python-sdk

Hello!

I am trying to use the sp.optimize.minimize method from the scipy module in workflow code. I have already marked scipy as a passthrough module.

The following exception is being thrown:

  File "/.venv/lib/python3.10/site-packages/scipy/optimize/_minimize.py", line 708, in minimize
    res = _minimize_bfgs(fun, x0, args, jac, callback, **options)
  File "/.venv/lib/python3.10/site-packages/scipy/optimize/_optimize.py", line 1398, in _minimize_bfgs
    _line_search_wolfe12(f, myfprime, xk, pk, gfk,
  File "/.venv/lib/python3.10/site-packages/scipy/optimize/_optimize.py", line 1146, in _line_search_wolfe12
    with warnings.catch_warnings():
  File "/.pyenv/versions/3.10.12/lib/python3.10/warnings.py", line 446, in __init__
    self._module = sys.modules['warnings'] if module is None else module
  File "/.venv/lib/python3.10/site-packages/temporalio/worker/workflow_sandbox/_importer.py", line 393, in __getitem__
    return self.current[key]
KeyError: 'warnings'

The relevant code in scipy which tries to use the ‘warnings’ module:

    if ret[0] is None:
        # line search failed: try different one.
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', LineSearchWarning)

And the code in the catch_warnings class in the ‘warnings’ module which fails:

    def __init__(self, *, record=False, module=None):
        """Specify whether to record warnings and if an alternative module
        should be used other than sys.modules['warnings'].

        For compatibility with Python 3.0, please consider all arguments to be
        keyword-only.

        """
        self._record = record
        self._module = sys.modules['warnings'] if module is None else module
        self._entered = False

I saw in the python sdk docs that some change is made to sys.modules, seemed to me it may have something to do with the problem. Would appreciate help troubleshooting the issue.

Thank you!

Hrmm. Can you confirm the Python SDK version? Does this occur when you register the workflow or when a workflow is executed? (I am guessing the latter)

This may be a bug in the sandbox when a passthrough module imports another passthrough module which was never loaded outside of the sandbox. If easy enough, can you provide a standalone replication (e.g. a simple workflow file making one scipy call)? You may be able to workaround by importing scipy (or warnings) yourself outside the workflow file that runs before the workflow (e.g. in the “app” layer) to get it into sys.modules.

Python SDK version is 1.6.0.

I struggled to reproduce the example, for some reason it isn’t reproducing when I simplify the workflow. It still reproduces in my bigger application. I may have time to get back to it later this week.
I also noticed that in this previous thread I opened, and in this linked github issue the error is very similar, so maybe related.
I also didn’t exactly understand what you meant with your suggestion to try and workaround - do you mean to import scipy or warnings in the “run_worker” file which creates the workflow?
Thanks