Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: metaclass conflict when importing dspy LangChainPredict #1265

Open
aishwaryap opened this issue Jul 10, 2024 · 5 comments
Open

TypeError: metaclass conflict when importing dspy LangChainPredict #1265

aishwaryap opened this issue Jul 10, 2024 · 5 comments

Comments

@aishwaryap
Copy link

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
Copy link

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
Copy link
Author

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
Copy link

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

image

@rhamnett
Copy link

@CShorten any ideas on this pls?

@rhamnett
Copy link

Looks like this will fix it:

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


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants