Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_tagging_chain_pydantic() pydantic schema validation errors #14159

Closed
4 of 14 tasks
jc7k opened this issue Dec 2, 2023 · 2 comments · Fixed by #14165
Closed
4 of 14 tasks

create_tagging_chain_pydantic() pydantic schema validation errors #14159

jc7k opened this issue Dec 2, 2023 · 2 comments · Fixed by #14165
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules

Comments

@jc7k
Copy link

jc7k commented Dec 2, 2023

System Info

pydantic_chain_test_code.txt
error_log.txt

Who can help?

@agola11 @hwchase17

The attached code is adopted from the official example from https://python.langchain.com/docs/use_cases/tagging. I am using the AzureChatOpenAI API, but I don't think the cause is related to that. I added an example prompt to ensure that the llm is working properly.

It appears that even when passing in a confirmed subclass of BaseModel, which is pydantic_schema, create_tagging_chain_pydantic(Tags, llm) is triggering validation errors. Please see the attached test code and error log.

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

Run the attached code and match it with the attached error log, which shows the version numbers of Python, Langchain and Pydantic. I am running on WIndows 11.:

Python version: 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
Langchain version: 0.0.344
Pydantic version: 2.5.2

Expected behavior

Expected behavior is as shown in the last example on https://python.langchain.com/docs/use_cases/tagging

Tags(sentiment='sad', aggressiveness=5, language='spanish')
Copy link

dosubot bot commented Dec 2, 2023

🤖

Hello @jc7k!

I'm Dosu, a friendly bot here to help you with LangChain. While we're waiting for a human maintainer to join us, feel free to ask me any questions or if you need help with anything related to LangChain. I'm here to provide support and help you get to the bottom of this issue.

Just so you know, I can help you understand bugs, answer questions, and even guide you on becoming a contributor. Looking forward to working with you!

Based on the information you've provided and the error log, it seems like the issue might be related to a compatibility issue between Pydantic v1 and v2. LangChain internally uses Pydantic v1, so when extending via inheritance or passing objects to LangChain, Pydantic v1 should be used.

If Pydantic v2 is installed, LangChain will use it due to a fallback mechanism, which can lead to the TypeError you're encountering. To avoid this, ensure that Pydantic v1 is installed and not v2. This was discussed in a similar issue: Cannot have models with BaseLanguageModel in pydantic 2: TypeError: BaseModel.validate() takes 2 positional arguments but 3 were given.

Here are examples of how to correctly extend via inheritance and pass objects to LangChain using Pydantic v1:

from pydantic.v1 import root_validator, validator

class CustomTool(BaseTool): # BaseTool is v1 code
    x: int = Field(default=1)

    def _run(*args, **kwargs):
        return "hello"

    @validator('x') # v1 code
    @classmethod
    def validate_x(cls, x: int) -> int:
        return 1
    

CustomTool(
    name='custom_tool',
    description="hello",
    x=1,
)
from langchain.tools.base import Tool
from pydantic.v1 import BaseModel, Field # <-- Uses v1 namespace

class CalculatorInput(BaseModel):
    question: str = Field()

Tool.from_function( # <-- tool uses v1 namespace
    func=lambda question: 'hello',
    name="Calculator",
    description="useful for when you need to answer questions about math",
    args_schema=CalculatorInput
)

In your case, LangChain tries to import from Pydantic v1 first, and if it fails, it imports from Pydantic v2. This is why the TypeError might be seen if Pydantic v2 is installed. To avoid this, ensure that Pydantic v1 is installed and available for import.

If you've already ensured that you're using Pydantic v1 and you're still encountering the issue, it would be helpful to have more specific information about the validation errors you're encountering. Please provide more details about the errors you're seeing and the Pydantic model you're using.

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.

@dosubot dosubot bot added Ɑ: models Related to LLMs or chat model modules 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Dec 2, 2023
@169
Copy link
Contributor

169 commented Dec 2, 2023

@jc7k You can use from pydantic.v1 import BaseModel, Field instead of from pydantic import BaseModel, Field

169 added a commit to 169/langchain that referenced this issue Dec 2, 2023
@169 169 mentioned this issue Dec 2, 2023
hwchase17 pushed a commit that referenced this issue Dec 4, 2023
### Description

Fix: #14159

Use `from pydantic.v1 import BaseModel, Field` instead of `from pydantic
import BaseModel, Field`

### [lin_bob57617](https://twitter.com/lin_bob57617)
aymeric-roucher pushed a commit to andrewrreed/langchain that referenced this issue Dec 11, 2023
### Description

Fix: langchain-ai#14159

Use `from pydantic.v1 import BaseModel, Field` instead of `from pydantic
import BaseModel, Field`

### [lin_bob57617](https://twitter.com/lin_bob57617)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants