UnboundLocalError for geometry optimization of Li4 with python module psi4

When running the following code, the geometry optimization for Li4 fails due to an UnboundLocalError.

I am using psi4 as a python module with
python 3.12.8
psi4 1.9.1

import psi4

#psi4.set_memory('800 MB')

Li2 = psi4.geometry("""
Li    -2.04006   -0.01759   -0.00000
Li     1.04348   -0.01759    0.00000
""")

psi4.set_options({'reference': 'rhf'})
psi4.optimize('scf/6-311G*', molecule=Li2)

Li4 = psi4.geometry("""
    Li    -1.77550    0.22110    0.00000
    Li    -0.12630    2.35160    0.00000
    Li     0.77100   -0.18870    0.00000
    Li    -0.87810   -2.31930    0.00000
""")

psi4.energy('scf/6-311G*')
psi4.optimize('scf/6-311G*', molecule=Li4)

The energy calculation and geometry optimization for Li2 work and the energy calculation for Li4 works, but at the very beginning of the geometry optimization I get following error:

Traceback (most recent call last):
  File "... ./test.py", line 21, in <module>
    psi4.optimize('scf/6-311G*', molecule=Li4)
  File "... /lib/python3.12/site-packages/psi4/driver/driver.py", line 1212, in optimize
    opt_object = optking.opt_helper.CustomHelper(molecule, params=optimizer_params)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ ... /lib/python3.12/site-packages/optking/opt_helper.py", line 400, in __init__
    self.build_coordinates()
  File "... /lib/python3.12/site-packages/optking/opt_helper.py", line 147, in build_coordinates
    make_internal_coords(self.molsys, self.params)
  File ".../lib/python3.12/site-packages/optking/optimize.py", line 664, in make_internal_coords
    o_molsys.fragments[0].add_intcos_from_connectivity(connectivity)
  File "... /lib/python3.12/site-packages/optking/frag.py", line 146, in add_intcos_from_connectivity
    addIntcos.add_intcos_from_connectivity(connectivity, self._intcos, self._geom)
  File "... /lib/python3.12/site-packages/optking/addIntcos.py", line 136, in add_intcos_from_connectivity
    add_oofp_from_connectivity(C, intcos, geom)
  File "... lib/python3.12/site-packages/optking/addIntcos.py", line 409, in add_oofp_from_connectivity
    if errors:
       ^^^^^^
UnboundLocalError: cannot access local variable 'errors' where it is not associated with a value

The only similar topic I found is Optimizing a P4 molecule causes error "local variable 'errors'..." . Unfortunately, because of particular reasons, I cannot change any code in the psi4 python package. Is there another workaround?

Similar issues arise with other molecules as well.

Please post your conda list. This looks like a problem with a bad version of pyoptking.

Thank you for the quick response. I use optking 0.3.0.

Here is the content of the environment I use:

Name Version Build Channel

_libgcc_mutex 0.1 conda_forge conda-forge
_openmp_mutex 4.5 2_kmp_llvm conda-forge
annotated-types 0.7.0 pyhd8ed1ab_1 conda-forge
bzip2 1.0.8 h4bc722e_7 conda-forge
ca-certificates 2024.12.14 hbcca054_0 conda-forge
dkh 1.2 h168aa07_2 conda-forge
flexcache 0.3 pyhd8ed1ab_1 conda-forge
flexparser 0.4 pyhd8ed1ab_1 conda-forge
gau2grid 2.0.7 h4bc722e_4 conda-forge
ld_impl_linux-64 2.43 h712a8e2_2 conda-forge
libblas 3.9.0 20_linux64_mkl conda-forge
libcblas 3.9.0 20_linux64_mkl conda-forge
libecpint 1.0.7 h5c03a75_11 conda-forge
libexpat 2.6.4 h5888daf_0 conda-forge
libffi 3.4.2 h7f98852_5 conda-forge
libgcc 14.2.0 h77fa898_1 conda-forge
libgcc-ng 14.2.0 h69a702a_1 conda-forge
libgfortran 14.2.0 h69a702a_1 conda-forge
libgfortran5 14.2.0 hd5240d6_1 conda-forge
libgomp 14.2.0 h77fa898_1 conda-forge
libhwloc 2.11.2 default_h0d58e46_1001 conda-forge
libiconv 1.17 hd590300_2 conda-forge
libint 2.9.0 h23769fb_2 conda-forge
liblapack 3.9.0 20_linux64_mkl conda-forge
liblzma 5.6.3 hb9d3cd8_1 conda-forge
libnsl 2.0.1 hd590300_0 conda-forge
libpcm 1.2.3 hdca9e5a_14 conda-forge
libsqlite 3.48.0 hee588c1_1 conda-forge
libstdcxx 14.2.0 hc0a3c3a_1 conda-forge
libstdcxx-ng 14.2.0 h4852527_1 conda-forge
libuuid 2.38.1 h0b41bf4_0 conda-forge
libxc-c 7.0.0 cpu_h20a523f_2 conda-forge
libxcrypt 4.4.36 hd590300_1 conda-forge
libxml2 2.13.5 h0d44e9d_1 conda-forge
libzlib 1.3.1 hb9d3cd8_2 conda-forge
llvm-openmp 19.1.7 h024ca30_0 conda-forge
mkl 2023.2.0 h84fe81f_50496 conda-forge
msgpack-python 1.1.0 py312h68727a3_0 conda-forge
ncurses 6.5 h2d0b736_2 conda-forge
networkx 3.4.2 pyh267e887_2 conda-forge
numpy 2.2.2 py312h72c5963_0 conda-forge
openssl 3.4.0 h7b32b05_1 conda-forge
optking 0.3.0 pyhd8ed1ab_0 conda-forge
pcmsolver 1.2.3 py_14 conda-forge
pint 0.24.4 pyhd8ed1ab_1 conda-forge
pip 25.0 pyh8b19718_0 conda-forge
platformdirs 4.3.6 pyhd8ed1ab_1 conda-forge
psi4 1.9.1 py312ha9da0b5_8 conda-forge
psutil 6.1.1 py312h66e93f0_0 conda-forge
pugixml 1.14 h59595ed_0 conda-forge
py-cpuinfo 9.0.0 pyhd8ed1ab_1 conda-forge
pybind11-abi 4 hd8ed1ab_3 conda-forge
pydantic 2.10.6 pyh3cfb1c2_0 conda-forge
pydantic-core 2.27.2 py312h12e396e_0 conda-forge
python 3.12.8 h9e4cc4f_1_cpython conda-forge
python_abi 3.12 5_cp312 conda-forge
pyyaml 6.0.2 py312h178313f_2 conda-forge
qcelemental 0.29.0 pyhd8ed1ab_0 conda-forge
qcengine 0.31.0 pyhd8ed1ab_0 conda-forge
readline 8.2 h8228510_1 conda-forge
scipy 1.15.1 py312h180e4f1_0 conda-forge
setuptools 75.8.0 pyhff2d567_0 conda-forge
tbb 2021.13.0 hceb3a55_1 conda-forge
tk 8.6.13 noxft_h4845f30_101 conda-forge
typing-extensions 4.12.2 hd8ed1ab_1 conda-forge
typing_extensions 4.12.2 pyha770c72_1 conda-forge
tzdata 2025a h78e105d_0 conda-forge
wheel 0.45.1 pyhd8ed1ab_1 conda-forge
yaml 0.2.5 h7f98852_2 conda-forge