概要
langchainのcreate_structured_output_chain
を実行すると以下のエラーが出ました。
RuntimeError: no validator found for , see `arbitrary_types_allowed` in Config
issueを参考にpydanticのバージョンを1.10.8にすると解決しました。
pip install pydantic==1.10.8
20230821追記
以下のdocsにてv0.0.267以降、pydanticのv1とv2の両方に対応した旨が発表されていました(ということに気づきました)。
v2の場合は
from pydantic.v1 import BaseModel, Field
とすれば動きます。v1をinstallしなおすよりも上記の方が簡単です。
追記ここまで
経緯
OpenAIのfunction callingをlangchainから使うため、チュートリアルのコードを実行したところ、エラーになってしまいました。
環境
% python -V
Python 3.10.2
% pip list | grep langchain
langchain 0.0.268
% pip list | grep pydantic
pydantic 2.2.1
pydantic_core 2.6.1
コード
from typing import Optional
from langchain.chains.openai_functions import (
create_openai_fn_chain,
create_structured_output_chain,
)
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.schema import HumanMessage, SystemMessage
from pydantic import BaseModel, Field
class Person(BaseModel):
"""Identifying information about a person."""
name: str = Field(..., description="The person's name")
age: int = Field(..., description="The person's age")
fav_food: Optional[str] = Field(None, description="The person's favorite food")
# If we pass in a model explicitly, we need to make sure it supports the OpenAI function-calling API.
llm = ChatOpenAI(model="gpt-4", temperature=0)
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a world class algorithm for extracting information in structured formats."),
("human", "Use the given format to extract information from the following input: {input}"),
("human", "Tip: Make sure to answer in the correct format"),
]
)
chain = create_structured_output_chain(Person, llm, prompt, verbose=True)
chain.run("Sally is 13")
実行するとchain = create_structured_output_chain(Person, llm, prompt, verbose=True)
でエラーが出ます。
解決方法
Error when importing ChatOutputParser: no validator found for というissueで類似のエラーが報告されており、pydanticのバージョンを1.10.8にしたら直ったという投稿がありました。
自環境でも同じ方法で解決しました。
pip install pydantic==1.10.8