OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

pydantic error: subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)

  • Thread starter Thread starter Nishant
  • Start date Start date
N

Nishant

Guest
For the below given code i am getting pydantic error:

Code:
from langchain.chains import LLMChain
from langchain.output_parsers import PydanticOutputParser
from langchain.prompts import PromptTemplate
from pydantic import BaseModel, Field

query = "Do you offer vegetarian food?"

class LineList(BaseModel):
    lines: list[str] = Field(description="Lines of text")

class LineListOutputParser(PydanticOutputParser):
    def __init__(self) -> None:
        super().__init__(pydantic_object=LineList)

    def parse(self, text: str) -> list[str]:
        lines = text.strip().split("\n")
        return lines


output_parser = LineListOutputParser()

QUERY_PROMPT = PromptTemplate(
    input_variables=["question"],
    template="""You are an AI language model assistant. Your task is to generate five
    different versions of the given user question to retrieve relevant documents from a vector
    database. By generating multiple perspectives on the user question, your goal is to help
    the user overcome some of the limitations of the distance-based similarity search.
    Provide these alternative questions separated by newlines. Only provide the query, no numbering.
    Original question: {question}""",
)

llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser)
queries = llm_chain.invoke(query)

I am getting the below error:

Code:
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[93], line 20
     16         lines = text.strip().split("\n")
     17         return lines
---> 20 output_parser = LineListOutputParser()
     22 QUERY_PROMPT = PromptTemplate(
     23     input_variables=["question"],
     24     template="""You are an AI language model assistant. Your task is to generate five
   (...)
     29     Original question: {question}""",
     30 )
     32 llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser)

Cell In[93], line 13, in LineListOutputParser.__init__(self)
     12 def __init__(self) -> None:
---> 13     super().__init__(pydantic_object=LineList)

File ~\anaconda3\Lib\site-packages\langchain_core\load\serializable.py:120, in Serializable.__init__(self, **kwargs)
    119 def __init__(self, **kwargs: Any) -> None:
--> 120     super().__init__(**kwargs)
    121     self._lc_kwargs = kwargs

File ~\anaconda3\Lib\site-packages\pydantic\v1\main.py:341, in BaseModel.__init__(__pydantic_self__, **data)
    339 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
    340 if validation_error:
--> 341     raise validation_error
    342 try:
    343     object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 1 validation error for LineListOutputParser
pydantic_object
  subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)

I am using pydantic-2.5.3
<p>For the below given code i am getting pydantic error:</p>
<pre><code>from langchain.chains import LLMChain
from langchain.output_parsers import PydanticOutputParser
from langchain.prompts import PromptTemplate
from pydantic import BaseModel, Field

query = "Do you offer vegetarian food?"

class LineList(BaseModel):
lines: list[str] = Field(description="Lines of text")

class LineListOutputParser(PydanticOutputParser):
def __init__(self) -> None:
super().__init__(pydantic_object=LineList)

def parse(self, text: str) -> list[str]:
lines = text.strip().split("\n")
return lines


output_parser = LineListOutputParser()

QUERY_PROMPT = PromptTemplate(
input_variables=["question"],
template="""You are an AI language model assistant. Your task is to generate five
different versions of the given user question to retrieve relevant documents from a vector
database. By generating multiple perspectives on the user question, your goal is to help
the user overcome some of the limitations of the distance-based similarity search.
Provide these alternative questions separated by newlines. Only provide the query, no numbering.
Original question: {question}""",
)

llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser)
queries = llm_chain.invoke(query)
</code></pre>
<p>I am getting the below error:</p>
<pre><code>---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
Cell In[93], line 20
16 lines = text.strip().split("\n")
17 return lines
---> 20 output_parser = LineListOutputParser()
22 QUERY_PROMPT = PromptTemplate(
23 input_variables=["question"],
24 template="""You are an AI language model assistant. Your task is to generate five
(...)
29 Original question: {question}""",
30 )
32 llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser)

Cell In[93], line 13, in LineListOutputParser.__init__(self)
12 def __init__(self) -> None:
---> 13 super().__init__(pydantic_object=LineList)

File ~\anaconda3\Lib\site-packages\langchain_core\load\serializable.py:120, in Serializable.__init__(self, **kwargs)
119 def __init__(self, **kwargs: Any) -> None:
--> 120 super().__init__(**kwargs)
121 self._lc_kwargs = kwargs

File ~\anaconda3\Lib\site-packages\pydantic\v1\main.py:341, in BaseModel.__init__(__pydantic_self__, **data)
339 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
340 if validation_error:
--> 341 raise validation_error
342 try:
343 object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 1 validation error for LineListOutputParser
pydantic_object
subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)
</code></pre>
<p>I am using pydantic-2.5.3</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top