langchain: ValidationError: 2 validation errors for PydanticOutputFunctionsParser
System Info
I was just trying to run the Tagging tutorial (no code modification on colab). https://python.langchain.com/docs/use_cases/tagging
And on the below code part,
chain = create_tagging_chain_pydantic(Tags, llm)
I got this error.
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
[<ipython-input-8-4724aee0c891>](https://localhost:8080/#) in <cell line: 1>()
----> 1 chain = create_tagging_chain_pydantic(Tags, llm)
2 frames
[/usr/local/lib/python3.10/dist-packages/pydantic/v1/main.py](https://localhost:8080/#) in __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: 2 validation errors for PydanticOutputFunctionsParser
pydantic_schema
subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)
pydantic_schema
value is not a valid dict (type=type_error.dict)
Is this a bug?
langchain version
!pip show langchain
Name: langchain
Version: 0.0.274
Summary: Building applications with LLMs through composability
Home-page: https://github.com/langchain-ai/langchain
Author:
Author-email:
License: MIT
Location: /usr/local/lib/python3.10/dist-packages
Requires: aiohttp, async-timeout, dataclasses-json, langsmith, numexpr, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity
Required-by:
Who can help?
No response
Information
- The official example notebooks/scripts
- My own modified scripts
Related Components
- LLMs/Chat Models
- Embedding Models
- Prompts / Prompt Templates / Prompt Selectors
- Output Parsers
- Document Loaders
- Vector Stores / Retrievers
- Memory
- Agents / Agent Executors
- Tools / Toolkits
- Chains
- Callbacks/Tracing
- Async
Reproduction
Just by running the colab notebook on the tagging tutorial. No modification applied. https://python.langchain.com/docs/use_cases/tagging
Expected behavior
Finishing running the notebook without any issues.
About this issue
- Original URL
- State: open
- Created 10 months ago
- Reactions: 4
- Comments: 20
🤖
Hello,
Thank you for reaching out and providing detailed information about the issue you’re facing. The error message you’re seeing is a
ValidationError
from Pydantic, which is a Python library for data validation using Python type annotations.The error message suggests that there’s an issue with the
pydantic_schema
argument you’re passing to thecreate_tagging_chain_pydantic
function. The error message indicates that thepydantic_schema
argument is expected to be a subclass ofBaseModel
and a valid dictionary.The
create_tagging_chain_pydantic
function in LangChain is used to create a chain that extracts information from a passage based on a Pydantic schema. The Pydantic schema defines the structure of the data to be extracted from the text.Here’s the function definition for reference:
From the error message, it seems like the
Tags
class you’re passing as thepydantic_schema
argument might not be a subclass ofBaseModel
or might not be a valid dictionary.Could you please check the definition of your
Tags
class? It should look something like this:If the
Tags
class is defined correctly and you’re still facing the issue, it might be a bug. In that case, please provide more details about your environment (Python version, other installed packages, etc.) so we can further investigate the issue.I hope this helps! Let me know if you have any other questions.
Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
try:
from langchain.pydantic_v1 import BaseModel
?In my case, langchain==0.0.320 and pydantic==2.4.2, it raised the error.I try to install an older version of pydantic like pip install pydantic==1.10, it raised another error.Finally, it goes work when using the version 1.10.10.
I also came into this issue. I found it maybe due to the pydantic version problem. When I use the pydantic version 2.x the error shows, while using an older version 1.10 error gone. Try install an older version of pydantic (1.10) seems work. Remember to restart your runtime if you are running in Colab notebooks.
!pip install pydantic==1.10
In case anyone using notebook, don’t forget to restart kernel after downgrade to pydantic
1.10.10
Changing my code to use
Fixed my issues. In langchain_core it looks like
pydantic.v1
is being used if you have a 2.x version. There’s a comment there saying it’s a hack lol.Here’s pydantic’s docs: https://docs.pydantic.dev/latest/migration/ for using v1 features so there’s no need to downgrade.
Kept getting
Then added the following print statements
which gave
Eventually, changing my import from
from pydantic import BaseModel
tofrom langchain.pydantic_v1 import BaseModel
as suggested by @keshiriotinto solved the issue.