#10516·dspy

[Bug] 3.4.0: importing litellm after dspy fails with circular import in openai (lazy openai proxy) when numpy is installed and litellm < 1.102

Author: Rakhil-HyperworksCreated Sep 28, 2026Updated Sep 28, 2026

What happened?

With DSPy 3.4.0, importing litellm after dspy fails with a circular-import error inside openai, when numpy is installed and litellm is older than 1.102.0. DSPy's own lazy LiteLLM load hits the same error, so ordinary LiteLLM-backed usage fails too (no network needed to trigger it).

The same environment works on DSPy 3.3.1.

Affected range I observed (numpy installed, openai 2.54.0, Python 3.11):

litellm import dspy; import litellm
1.65.8 (DSPy's declared minimum), 1.79.3, 1.89.1, 1.93.0, 1.96.0, 1.98.0, 1.99.0, 1.100.0, 1.101.0, 1.101.2 ImportError
1.102.0, 1.103.0 OK

Without numpy installed, the same versions import fine. The same litellm 1.101.0 + numpy with dspy 3.3.1 imports fine. I also reproduced it on current main.

Steps to reproduce

bash
python -m venv venv && . venv/bin/activate    # I used Python 3.11
pip install "dspy==3.4.0" "litellm==1.101.0" numpy
python -c "import dspy; import litellm"

Public-API variant (no network):

python
import dspy

lm = dspy.LM("openai/gpt-4o-mini", engine="litellm", api_key="x")
lm.supports_function_calling   # ImportError while DSPy lazily loads litellm

Workaround: import litellm (or openai._models) before dspy.

Traceback:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../site-packages/litellm/__init__.py", line 1279, in <module>
    from litellm.types.secret_managers.main import KeyManagementSettings
  File ".../site-packages/litellm/types/secret_managers/main.py", line 4, in <module>
    from litellm.types.llms.base import LiteLLMPydanticObjectBase
  File ".../site-packages/litellm/types/llms/base.py", line 3, in <module>
    from openai._models import BaseModel as OpenAIObject
  File ".../site-packages/openai/_models.py", line 43, in <module>
    from ._types import (
  File ".../site-packages/dspy/utils/lazy_import.py", line 125, in __setattr__
    setattr(self._load(), attr, value)
            ^^^^^^^^^^^^
  File ".../site-packages/dspy/utils/lazy_import.py", line 112, in _load
    spec.loader.exec_module(module)
  File ".../site-packages/openai/__init__.py", line 9, in <module>
    from . import types
  File ".../site-packages/openai/types/__init__.py", line 5, in <module>
    from .batch import Batch as Batch
  File ".../site-packages/openai/types/batch.py", line 6, in <module>
    from .._models import BaseModel
ImportError: cannot import name 'BaseModel' from partially initialized module 'openai._models' (most likely due to a circular import) (.../site-packages/openai/_models.py)

What I think is going on

This is my reading of the traceback, happy to be corrected:

  1. import dspy reaches require("openai") (via dspy/clients/openai.py), which puts a _LazyModule proxy in sys.modules["openai"] because openai isn't imported yet.
  2. Older litellm does from openai._models import BaseModel at import time. Python imports the submodule through the proxy's __path__, and while binding submodules onto the parent package it calls setattr on the proxy.
  3. _LazyModule.__setattr__ then materializes the real openai package, whose __init__ imports openai.types → openai._models, which is still partially initialized → circular ImportError.

Why numpy matters (I believe): when numpy is absent, require("numpy") takes the _MissingModule branch, which calls inspect.stack(). That walks sys.modules and happens to materialize the openai proxy early, which hides the bug. With numpy installed, nothing materializes it before litellm is imported. litellm >= 1.102.0 presumably no longer imports openai._models this way at import time, but I haven't confirmed why.

Possible directions (just suggestions): not installing a proxy for packages that are commonly imported by submodule path (openai), or letting _LazyModule.__setattr__ store submodule attributes without materializing; alternatively raising the litellm floor to >=1.102.0.

Environment / what I checked

  • dspy 3.4.0 (also current main), openai 2.54.0, numpy 2.x, clean venvs with only these packages.
  • Reproduced on Python 3.11 and in a separate 3.14 environment. Only tested on Windows; I haven't verified Linux/macOS, though nothing in the traceback looks OS-specific.
  • I searched existing issues/PRs and didn't find a report of this; apologies if I missed one.

DSPy version

3.4.0