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

Most upvoted comments

🤖

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 the create_tagging_chain_pydantic function. The error message indicates that the pydantic_schema argument is expected to be a subclass of BaseModel 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:

def create_tagging_chain_pydantic(
    pydantic_schema: Any,
    llm: BaseLanguageModel,
    prompt: Optional[ChatPromptTemplate] = None,
    **kwargs: Any
) -> Chain:
    """Creates a chain that extracts information from a passage
     based on a pydantic schema.

    Args:
        pydantic_schema: The pydantic schema of the entities to extract.
        llm: The language model to use.

    Returns:
        Chain (LLMChain) that can be used to extract information from a passage.
    """
    openai_schema = pydantic_schema.schema()
    function = _get_tagging_function(openai_schema)
    prompt = prompt or ChatPromptTemplate.from_template(_TAGGING_TEMPLATE)
    output_parser = PydanticOutputFunctionsParser(pydantic_schema=pydantic_schema)
    llm_kwargs = get_llm_kwargs(function)
    chain = LLMChain(
        llm=llm,
        prompt=prompt,
        llm_kwargs=llm_kwargs,
        output_parser=output_parser,
        **kwargs,
    )
    return chain

From the error message, it seems like the Tags class you’re passing as the pydantic_schema argument might not be a subclass of BaseModel or might not be a valid dictionary.

Could you please check the definition of your Tags class? It should look something like this:

from pydantic import BaseModel, Field

class Tags(BaseModel):
    sentiment: str = Field(..., enum=["happy", "neutral", "sad"])
    aggressiveness: int = Field(
        ...,
        description="describes how aggressive the statement is, the higher the number the more aggressive",
        enum=[1, 2, 3, 4, 5],
    )
    language: str = Field(
        ..., enum=["spanish", "english", "french", "german", "italian"]
    )

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.

Downgrading to pydantic 1.10.10 worked for me, as I was getting an error when trying to use a Pydantic schema as an args_schema parameter value on a @tool decorator, following the DeepLearning.AI course.

My code was:

from pydantic import BaseModel, Field

class SearchInput(BaseModel):
    query: str = Field(description="Thing to search for")

@tool(args_schema=SearchInput)
def search(query: str) -> str:
    """Searches for weather online"""
    return "21c"

And before downgrading Pydantic I got this error:

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

try: from langchain.pydantic_v1 import BaseModel?

same issue:

langchain==0.0.314 pydantic==2.4.2

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

from pydantic.v1 import BaseModel, Field

Fixed my issues. In langchain_core it looks like pydantic.v1is 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

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

Then added the following print statements

print(f"OrgsList: {OrgsList}")
print(f"OrgsList is of subclass BaseModel: {issubclass(OrgsList, BaseModel)}")

which gave

OrgsList: <class '__mp_main__.OrgsList'>
OrgsList is of subclass BaseModel: True

Eventually, changing my import from from pydantic import BaseModel to from langchain.pydantic_v1 import BaseModel as suggested by @keshiriotinto solved the issue.