Find answers from the community

Updated 11 months ago

v1 BaseModel incompatible with v2 Basemo...

At a glance
The community member is experiencing compatibility issues between pydantic v2 and llama-index, as some llama-index models inherit from the v1 BaseModel, which cannot be used to compose a v2 model. The community member is unsure whether they should always use the BaseModel exported from pydantic.v1 when creating their own pydantic models. In the comments, another community member suggests that the compatibility issue can be resolved by using the v1 layer from pydantic.v1 import BaseModel, which is what the bridge.pydantic is doing.
Useful resources
Is pydantic v2 incompatible with llama-index? Multiple models such as MetadataFilters inherit from the v1 BaseModel (which cannot be used to compose a v2 model) https://github.com/pydantic/pydantic/issues/7997

E.G

This line exists in the schema file

Plain Text
from llama_index.core.bridge.pydantic import (
    BaseModel
)


This v1 BaseModel is used in the creation of MetadataFilters exported from llama_index.core.vector_stores

Other models that i create use the BaseModel from pydantic v2 and are composed with the existing v1 llama-index models. This throws errors. When creating our own pydantic models should we always use the BaseModel exported from pydantic.v1?

I am trying to create my own pydantic model like this

Plain Text
class ChatBody(BaseModel):
    messages: list[OpenAIMessage]
    use_context: bool = False
    conversation_id: str | None = None
    context_filter: MetadataFilters | None = None
    include_sources: bool = True
    stream: bool = False

Where the BaseModel that ChatBody inherits from is important from pydantic (v2) but MetadataFilters inherits from the BaseModel that is exported from llama_index.core.bridge.pydantic which is a v1 model
L
2 comments
It is compatible, but you have to use the v1 layer

from pydantic.v1 import BaseModel
thats what the bridge.pydantic is doing
Add a reply
Sign up and join the conversation on Discord