1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Resolve no validator found for <class '__main__.Resume'>, see...

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    Getting the no validator found error with the following code. Here are the Library versions:

    LangChain version: 0.0.284
    Pydantic version: 2.8.2


    Code:

    from typing import Optional
    from pydantic import BaseModel, Field, ValidationError
    from config import set_environment
    from langchain.chains import create_extraction_chain_pydantic
    from langchain.chat_models import ChatOpenAI
    from langchain.document_loaders import PyPDFLoader

    class Resume(BaseModel):
    first_name: Optional[str] = Field(None, description="First Name")
    last_name: Optional[str] = Field(None, description="Last Name")
    email: Optional[str] = Field(None, description="Email Address")
    phone: Optional[str] = Field(None, description="Phone Number")

    class Config:
    arbitrary_types_allowed = True


    set_environment()

    # Sample Data
    sample_data = {
    "first_name": "John",
    "last_name": "Doe",
    "email": "john.doe@example.com",
    "phone": "123-456-7890"
    }

    # Validate resume
    try:
    resume = Resume(**sample_data)
    print("Validation successful:", resume)
    except ValidationError as e:
    print("Validation error:", e)

    pdf_file_path = "laverne-resume.pdf"
    pdf_loader = PyPDFLoader(pdf_file_path)
    docs = pdf_loader.load_and_split()

    chat_model = ChatOpenAI(model_name="gpt-3.5-turbo")
    chain = create_extraction_chain_pydantic(pydantic_schema=Resume, llm=chat_model)
    resp = chain.run(docs)
    print(resp)


    Error:

    Traceback (most recent call last):
    File "/ch4/information_extraction.py", line 56, in <module>
    chain = create_extraction_chain_pydantic(pydantic_schema=Resume, llm=chat_model)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "ws/gen-ai/venv/lib/python3.12/site-packages/langchain/chains/openai_functions/extraction.py", line 97, in create_extraction_chain_pydantic
    class PydanticSchema(BaseModel):
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/main.py", line 197, in __new__
    fields[ann_name] = ModelField.infer(
    ^^^^^^^^^^^^^^^^^
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 504, in infer
    return cls(
    ^^^^
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 434, in __init__
    self.prepare()
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 550, in prepare
    self._type_analysis()
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 756, in _type_analysis
    self.sub_fields = [self._create_sub_type(self.type_, '_' + self.name)]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 806, in _create_sub_type
    return self.__class__(
    ^^^^^^^^^^^^^^^
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 434, in __init__
    self.prepare()
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 555, in prepare
    self.populate_validators()
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/fields.py", line 829, in populate_validators
    *(get_validators() if get_validators else list(find_validators(self.type_, self.model_config))),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "ws/gen-ai/venv/lib/python3.12/site-packages/pydantic/v1/validators.py", line 765, in find_validators
    raise RuntimeError(f'no validator found for {type_}, see `arbitrary_types_allowed` in Config')
    RuntimeError: no validator found for <class '__main__.Resume'>, see `arbitrary_types_allowed` in Config

    Continue reading...

Compartilhe esta Página