stanfordnlp / dspy

DSPy: The framework for programming—not prompting—foundation models
https://dspy-docs.vercel.app/
MIT License
18.6k stars 1.43k forks source link

TypeError: metaclass conflict when importing dspy LangChainPredict #1265

Open aishwaryap opened 4 months ago

aishwaryap commented 4 months ago

I am trying to use DSPy with LangChain and just trying to run the example here.

At the step importing modules interacting with LangChain ie

from dspy.predict.langchain import LangChainPredict, LangChainModule

Note that I get this error even if I have run nothing else first.

I get the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 2
      1 # From DSPy, import the modules that know how to interact with LangChain LCEL.
----> 2 from dspy.predict.langchain import LangChainPredict, LangChainModule

File [~/Documents/venvs/dspy_env/lib/python3.10/site-packages/dspy/predict/langchain.py:34](http://localhost:8888/lab/tree/~/Documents/venvs/dspy_env/lib/python3.10/site-packages/dspy/predict/langchain.py#line=33)
     30     def __getattr__(self, item): return getattr(self.obj, item)
     31     def __deepcopy__(self, memo): return ShallowCopyOnly(copy.copy(self.obj))
---> 34 class LangChainPredict(Predict, Runnable): #, RunnableBinding):
     35     class Config: extra = Extra.allow  # Allow extra attributes that are not defined in the model
     37     def __init__(self, prompt, llm, **config):

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Some searching has shown up this issue which suggests that LangChain needs pydantic v1 and suggests downgrading pydantic to 1.10.12 but if I. do that, I get the error mentioned in this issue for which the fix seems to be upgrade to pydantic v2.

I am using Python Python 3.10.2, dspy-ai==2.4.12, langchain==0.2.7 and pydantic==2.8.2. Any suggestions how to import those modules?

drmwilke commented 4 months ago

Hi,

experienced the same issue today. I don't have a proper solution but it might unblock you:

In the file \lib\site-packages\dspy\predict\langchain.py:L40 You can change the metaclass information:

class LangChainPredictMetaClass(type(Predict), type(Runnable)):
    pass

class LangChainPredict(Predict, Runnable, metaclass=LangChainPredictMetaClass): #, RunnableBinding):

I tried downgrading pydantic and dspy to random versions but it didn't work out with other packages that I need to use. So I did not follow that approach further. Adding the metaclass obviously breaks as soon as this file is updated by pip but it's a workaround till there is an actual solution.

aishwaryap commented 4 months ago

It turns out that downgrading to dspy_ai==2.1.4 also works. However, I think the issue should remain open so that the authors can integrate the correction from @drmwilke 's response to retain support for LangChain.

atursunov commented 3 months ago

I had similar issue. Can someone on DSPy side please fix this?

image
rhamnett commented 3 months ago

@CShorten any ideas on this pls?

rhamnett commented 3 months ago

Looks like this will fix it:

# Define a common metaclass
class CombinedMeta(type(Predict), type(Runnable)):
    pass

class LangChainPredict(Predict, Runnable, metaclass=CombinedMeta):