langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
95.59k stars 15.52k forks source link

__modify_schema__ Method Unsupported in Pydantic v2 Compatibility with Langchain #27687

Open DavidNavarroSaiz opened 1 month ago

DavidNavarroSaiz commented 1 month ago

Checked other resources

Example Code

Steps to Reproduce Set up a MongoDB Atlas Vector Store using the MongoDBAtlasVectorSearch and OpenAIEmbeddings from Langchain libraries.

Attempt to run a similarity search with the following code snippet:


        db_name = os.getenv("VECTOR_DB_NAME")
        collection_name = os.getenv("VECTOR_DB_COLLECTION_NAME")
        index_name = os.getenv("VECTOR_DBVECTOR_DB_INDEX_NAMENAME")
        # Initialize MongoDB client
        self.client = MongoClient(mongodb_uri)
        self.db = self.client[db_name]
        self.collection = self.db[collection_name]
        self.index_name = index_name

        # Initialize embeddings model
        self.embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

        # Initialize the vector store
        self.vector_store = MongoDBAtlasVectorSearch(
            collection=self.collection,
            embedding=self.embeddings,
            index_name=self.index_name,
            text_key="page_content",
            relevance_score_fn="cosine",
        )
query = "cual empresa es demandada?"
 results = self.vector_store.similarity_search(query=query)

Error Message and Stack Trace (if applicable)

pydantic.errors.PydanticUserError: The __modify_schema__ method is not supported in Pydantic v2. Use __get_pydantic_json_schema__ instead in class SecretStr.

complete trace:

C:\Users\ASUS\anaconda3\envs\abogacia\Lib\site-packages\langchain_openai\chat_models__init__.py:1: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain_core.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.

For example, replace imports like: from langchain_core.pydantic_v1 import BaseModel with: from pydantic import BaseModel or the v1 compatibility namespace if you are working in a code base that has not been fully upgraded to pydantic 2 yet. from pydantic.v1 import BaseModel

from langchain_openai.chat_models.azure import AzureChatOpenAI C:\Users\ASUS\anaconda3\envs\abogacia\Lib\site-packages\pydantic_internal_config.py:341: UserWarning: Valid config keys have changed in V2:

For further information visit https://errors.pydantic.dev/2.8/u/custom-json-schema PS C:\Users\ASUS\Desktop\projects\Abogacy-AI> & C:/Users/ASUS/anaconda3/envs/abogacia/python.exe c:/Users/ASUS/Desktop/projects/Abogacy-AI/src/atlasVectorDB.py C:\Users\ASUS\anaconda3\envs\abogacia\Lib\site-packages\langchain_openai\chat_models__init__.py:1: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain_core.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.

For example, replace imports like: from langchain_core.pydantic_v1 import BaseModel with: from pydantic import BaseModel or the v1 compatibility namespace if you are working in a code base that has not been fully upgraded to pydantic 2 yet. from pydantic.v1 import BaseModel

from langchain_openai.chat_models.azure import AzureChatOpenAI C:\Users\ASUS\anaconda3\envs\abogacia\Lib\site-packages\pydantic_internal_config.py:341: UserWarning: Valid config keys have changed in V2:

For further information visit https://errors.pydantic.dev/2.8/u/custom-json-schema PS C:\Users\ASUS\Desktop\projects\Abogacy-AI> & C:/Users/ASUS/anaconda3/envs/abogacia/python.exe c:/Users/ASUS/Desktop/projects/Abogacy-AI/src/atlasVectorDB.py C:\Users\ASUS\anaconda3\envs\abogacia\Lib\site-packages\langchain_openai\chat_models__init__.py:1: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain_core.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.

For example, replace imports like: from langchain_core.pydantic_v1 import BaseModel with: from pydantic import BaseModel or the v1 compatibility namespace if you are working in a code base that has not been fully upgraded to pydantic 2 yet. from pydantic.v1 import BaseModel

from langchain_openai.chat_models.azure import AzureChatOpenAI C:\Users\ASUS\anaconda3\envs\abogacia\Lib\site-packages\pydantic_internal_config.py:341: UserWarning: Valid config keys have changed in V2:

Description

When running a similarity search using langchain_openai, I'm encountering issues related to Pydantic compatibility with Langchain. It appears the library still relies on Pydantic v1 imports and methods, specifically the modify_schema method, which is deprecated in Pydantic v2. The error suggests replacing modify_schema with __get_pydantic_json_schema__, as the method is not supported in Pydantic v2.

System Info

this is the package info:

aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 asgiref==3.8.1 asttokens==2.4.1 attrs==23.2.0 backcall==0.2.0 backoff==2.2.1 bcrypt==4.1.3 beautifulsoup4==4.12.3 bleach==6.1.0 build==1.2.1 cachetools==5.3.3 certifi==2024.6.2 cffi==1.16.0 charset-normalizer==3.3.2 chroma-hnswlib==0.7.3 chromadb==0.5.3 click==8.1.7 colorama==0.4.6 coloredlogs==15.0.1 cryptography==43.0.0 dataclasses-json==0.6.7 decorator==5.1.1 defusedxml==0.7.1 Deprecated==1.2.14 distro==1.9.0 dnspython==2.6.1 docopt==0.6.2 docx2txt==0.8 email_validator==2.2.0 executing==2.0.1 fastapi==0.115.4 fastapi-cli==0.0.4 fastjsonschema==2.20.0 filelock==3.15.4 flatbuffers==24.3.25 frozenlist==1.4.1 fsspec==2024.6.1 gitdb==4.0.11 GitPython==3.1.43 google-auth==2.31.0 googleapis-common-protos==1.63.2 greenlet==3.0.3 grpcio==1.64.1 h11==0.14.0 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 huggingface-hub==0.23.4 humanfriendly==10.0 idna==3.7 importlib_metadata==7.1.0 importlib_resources==6.4.0 ipython==8.12.3 jedi==0.19.1 Jinja2==3.1.4 jsonpatch==1.33 jsonpointer==3.0.0 jsonschema==4.23.0 jsonschema-specifications==2023.12.1 jupyter_client==8.6.2 jupyter_core==5.7.2 jupyterlab_pygments==0.3.0 kubernetes==30.1.0 langchain==0.3.4 langchain-community==0.3.3 langchain-core==0.3.13 langchain-experimental==0.3.2 langchain-mongodb==0.1.6 langchain-openai==0.1.14 langchain-text-splitters==0.3.0 langsmith==0.1.137 markdown-it-py==3.0.0 MarkupSafe==2.1.5 marshmallow==3.21.3 matplotlib-inline==0.1.7 mdurl==0.1.2 mistune==3.0.2 mmh3==4.1.0 monotonic==1.6 mpmath==1.3.0 multidict==6.0.5 mypy-extensions==1.0.0 nbclient==0.10.0 nbconvert==7.16.4 nbformat==5.10.4 neo4j==5.24.0 numpy==1.26.4 oauthlib==3.2.2 onnxruntime==1.18.1 openai==1.35.10 opentelemetry-api==1.25.0 opentelemetry-exporter-otlp-proto-common==1.25.0 opentelemetry-exporter-otlp-proto-grpc==1.25.0 opentelemetry-instrumentation==0.46b0 opentelemetry-instrumentation-asgi==0.46b0 opentelemetry-instrumentation-fastapi==0.46b0 opentelemetry-proto==1.25.0 opentelemetry-sdk==1.25.0 opentelemetry-semantic-conventions==0.46b0 opentelemetry-util-http==0.46b0 orjson==3.10.6 outcome==1.3.0.post0 overrides==7.7.0 packaging==24.1 pandocfilters==1.5.1 parso==0.8.4 pdfminer.six==20240706 pickleshare==0.7.5 pipreqs==0.5.0 platformdirs==4.2.2 posthog==3.5.0 prompt_toolkit==3.0.47 protobuf==4.25.3 pure_eval==0.2.3 pyasn1==0.6.0 pyasn1_modules==0.4.0 pycparser==2.22 pydantic==2.9.2 pydantic-settings==2.6.0 pydantic_core==2.23.4 Pygments==2.18.0 pymongo==4.8.0 PyMuPDF==1.24.7 PyMuPDFb==1.24.6 pypdf==4.2.0 PyPika==0.48.9 pyproject_hooks==1.1.0 pyreadline3==3.4.1 PySocks==1.7.1 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 python-multipart==0.0.9 pytz==2024.1 pywin32==306 PyYAML==6.0.1 pyzmq==26.1.0 referencing==0.35.1 regex==2024.5.15 requests==2.32.3 requests-oauthlib==2.0.0 requests-toolbelt==1.0.0 rich==13.7.1 rpds-py==0.20.0 rsa==4.9 selenium==4.21.0 setuptools==69.5.1 shellingham==1.5.4 six==1.16.0 smmap==5.0.1 sniffio==1.3.1 sortedcontainers==2.4.0 soupsieve==2.5 SQLAlchemy==2.0.31 stack-data==0.6.3 starlette==0.41.2 sympy==1.12.1 tenacity==8.5.0 tiktoken==0.7.0 tinycss2==1.3.0 tokenizers==0.19.1 tornado==6.4.1 tqdm==4.66.4 traitlets==5.14.3 trio==0.25.1 trio-websocket==0.11.1 typer==0.12.3 typing-inspect==0.9.0 typing_extensions==4.12.2 ujson==5.10.0 urllib3==2.2.1 uvicorn==0.30.1 watchfiles==0.22.0 wcwidth==0.2.13 webdriver-manager==4.0.1 webencodings==0.5.1 websocket-client==1.8.0 websockets==12.0 wheel==0.43.0 wrapt==1.16.0 wsproto==1.2.0 yarg==0.1.9 yarl==1.9.4 zipp==3.19.2

keenborder786 commented 1 month ago

Hello, Can you try the following and see if it helps, basically initalizes using from_connection_string


from langchain_openai.embeddings import OpenAIEmbeddings
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from pymongo import MongoClient
db_name = "test"
collection_name = "test_collection"
index_name = "test_index"

# Initialize embeddings model
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vector_store = MongoDBAtlasVectorSearch.from_connection_string("<your_connection_string>", "sample_mflix.embedded_movies", embeddings, index_name="_id_",
    text_key="page_content",
    relevance_score_fn="cosine")
query = "Plots"
results = vector_store.similarity_search(query=query)