llama_index
llama_index copied to clipboard
[Bug]: PydanticUserError: The `__modify_schema__` method is not supported in Pydantic v2. Use `__get_pydantic_json_schema__` instead in class `SecretStr`.
Bug Description
In this code's depenency,:
from llama_index.core import Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
Settings.embed_model = OllamaEmbedding(
model_name='nomic-embed-text',
base_url="http://localhost:11434",
ollama_additional_kwargs={"mirostat": 0},
)
# Choose your LLM...
Settings.llm = Ollama(model='mistral'
There is
from llama_index.core.bridge.langchain import Embeddings as LCEmbeddings
this occurs in the OllamaEmbedding as well as i believe the base embedding. I assume it occurred during a transition from pydantic 1 and pydantic 2, and this code has not been updated.
Version
0.11.7
Steps to Reproduce
run the code above.
Relevant Logs/Tracbacks
No response
I couldn't find a specific issue related to the PydanticUserError you're encountering, but there is a related issue regarding Pydantic v2 changes. In Pydantic v2, the __modify_schema__ method is not supported, and you should use the __get_pydantic_json_schema__ method instead. This method is used to generate a JSON schema for a Pydantic model and includes customizations for the schema [1][2].
For the SecretStr class, ensure that any schema modifications are done using the __get_pydantic_json_schema__ method. This change is part of the transition from Pydantic v1 to v2, which may have affected your code.
Additionally, there were several changes made in the llama_index package to support Pydantic v2, such as fixing validation errors and handling nested models [3]. You might want to check if these changes are relevant to your issue.
If the problem persists, consider reviewing the implementation of the SecretStr class and related schema methods to ensure compatibility with Pydantic v2.
To continue talking to Dosu, mention @dosu.
I'm not able to reproduce. Did you try updating?
pip install -U llama-index-core llama-index-embeddings-ollama llama-index-llms-ollama ?
Sanity check here:
https://colab.research.google.com/drive/1fS12KqTpsxkiMxDJOiAXb6uadQgQxKrp?usp=sharing
I get the same error when I try to create indexes using the function below:
def build_automerging_index(document,LLM):
chunk_sizes = [2048, 512, 128]
node_parser = HierarchicalNodeParser.from_defaults(chunk_sizes=chunk_sizes)
nodes = node_parser.get_nodes_from_documents(document)
leaf_nodes = get_leaf_nodes(nodes)
Settings.llm = AzureOpenAI(model = "gpt-4o-mini",
deployment_name = "deployment",
api_key=azure_openai_api_key,
azure_endpoint=azure_openai_endpoint,
api_version=azure_openai_api_version)
docstore = SimpleDocumentStore()
docstore.add_documents(nodes)
storage_context = StorageContext.from_defaults(docstore=docstore)
automerging_index = VectorStoreIndex(leaf_nodes, storage_context=storage_context)
return automerging_index
I got the same error, when doing this -
import os
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage, ToolMessage
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
#from get_llm import get_llm
Error -
PydanticUserError: The `__modify_schema__` method is not supported in Pydantic v2. Use `__get_pydantic_json_schema__` instead in class `SecretStr`.
For further information visit https://errors.pydantic.dev/2.9/u/custom-json-schema
Same error here:
File "/Users/ywsung/Desktop/Neosearch/neosearch/../neosearch/settings/openai.py", line 20, in init_openai
Settings.embed_model = OpenAIEmbedding(
^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/llama_index/core/settings.py", line 74, in embed_model
self._embed_model = resolve_embed_model(embed_model)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/llama_index/core/embeddings/utils.py", line 39, in resolve_embed_model
from llama_index.core.bridge.langchain import Embeddings as LCEmbeddings
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/llama_index/core/bridge/langchain.py", line 66, in <module>
from langchain_community.chat_models import (
File "<frozen importlib._bootstrap>", line 1231, in _handle_fromlist
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/langchain_community/chat_models/__init__.py", line 271, in __getattr__
module = importlib.import_module(_module_lookup[name])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/langchain_community/chat_models/anyscale.py", line 30, in <module>
class ChatAnyscale(ChatOpenAI):
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 224, in __new__
complete_model_class(
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 577, in complete_model_class
schema = cls.__get_pydantic_core_schema__(cls, handler)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/main.py", line 671, in __get_pydantic_core_schema__
return handler(source)
^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_schema_generation_shared.py", line 83, in __call__
schema = self._handler(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 655, in generate_schema
schema = self._generate_schema_inner(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 924, in _generate_schema_inner
return self._model_schema(obj)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 739, in _model_schema
{k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 739, in <dictcomp>
{k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 1115, in _generate_md_field_schema
common_field = self._common_field_schema(name, field_info, decorators)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 1308, in _common_field_schema
schema = self._apply_annotations(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 2107, in _apply_annotations
schema = get_inner_schema(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_schema_generation_shared.py", line 83, in __call__
schema = self._handler(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 2091, in inner_handler
metadata_js_function = _extract_get_pydantic_json_schema(obj, schema)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ywsung/Desktop/Neosearch/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 2447, in _extract_get_pydantic_json_schema
raise PydanticUserError(
pydantic.errors.PydanticUserError: The `__modify_schema__` method is not supported in Pydantic v2. Use `__get_pydantic_json_schema__` instead in class `SecretStr`.
For further information visit https://errors.pydantic.dev/2.9/u/custom-json-schema
I've similar problem with:
from langchain_openai import OpenAIEmbeddings
Error is as follows:
Exception has occurred: PydanticUserError The modify_schemamethod is not supported in Pydantic v2. Useget_pydantic_json_schemainstead in classSecretStr`.
For further information visit https://errors.pydantic.dev/2.9/u/custom-json-schema
File "D:\advdmfg\advdmfg\settings.py", line 9, in __modify_schema__ method is not supported in Pydantic v2. Use __get_pydantic_json_schema__ instead in class SecretStr.
For further information visit https://errors.pydantic.dev/2.9/u/custom-json-schema`
I've similar problem with:
from langchain_openai import OpenAIEmbeddingsError is as follows:
Exception has occurred: PydanticUserError Themodify_schemamethod is not supported in Pydantic v2. Useget_pydantic_json_schemainstead in classSecretStr`.For further information visit https://errors.pydantic.dev/2.9/u/custom-json-schema File "D:\advdmfg\advdmfg\settings.py", line 9, in from langchain_openai import OpenAIEmbeddings File "D:\advdmfg\manage.py", line 18, in main execute_from_command_line(sys.argv) File "D:\advdmfg\manage.py", line 22, in main() pydantic.errors.PydanticUserError: The
__modify_schema__method is not supported in Pydantic v2. Use__get_pydantic_json_schema__instead in classSecretStr.For further information visit https://errors.pydantic.dev/2.9/u/custom-json-schema`
@daniyalk20 try this:
pip install --upgrade langchain-openai
even if I did pip install --upgrade langchain-openai it still is the same error.
even if I did pip install --upgrade langchain-openai it still is the same error.
Try newest version.when I upgrade langchain-openai:0.3.16,it works