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

PydanticUserError when instantiating SyntheticDataGenerator #24

Open
zakhikhan opened this issue Dec 10, 2024 · 1 comment
Open

PydanticUserError when instantiating SyntheticDataGenerator #24

zakhikhan opened this issue Dec 10, 2024 · 1 comment

Comments

@zakhikhan
Copy link

zakhikhan commented Dec 10, 2024

I am attempting to use this library to generate tabular synthetic data. Any time I try to instantiate the SyntheticDataGenerator class from tabular_synthetic_data (either through the create_openai_data_generator function or directly), I get an error like the below:

PydanticUserError: SyntheticDataGenerator is not fully defined; you should define BaseCache, then call SyntheticDataGenerator.model_rebuild().

For further information visit https://errors.pydantic.dev/2.10/u/class-not-fully-defined

versions (all installed via pip)
langchain_experimental: 0.3.3
langchain: 0.3.11
langchain_openai 0.2.12
langchain_core: 0.3.24

Here is simplified example code that causes the error:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import FewShotPromptTemplate
from langchain_experimental.tabular_synthetic_data.base import SyntheticDataGenerator
from pydantic import BaseModel

OPENAI_TEMPLATE = PromptTemplate(input_variables=["example"], template="{example}")

examples = [
    {"example": "toothbrush_name: Crest Brush, price: 10"},
    {"example": "toothbrush_name: Generic toothbrush, price: 5"}
]

class ToothbrushSchema(BaseModel):
    toothbrush_name: str
    price: int
    
output_schema=ToothbrushSchema

llm=ChatOpenAI(temperature=1)

prompt_template = FewShotPromptTemplate(
    examples=examples,
    suffix=("Generate synthetic data about {subject}."),
    input_variables=["subject"],
    example_prompt=OPENAI_TEMPLATE,
)

prompt=prompt_template

chain = llm.with_structured_output(ToothbrushSchema)

sdg = SyntheticDataGenerator(template=prompt_template,llm_chain=chain)
@zakhikhan
Copy link
Author

zakhikhan commented Dec 11, 2024

I got it to work by creating a dev install of langchain_experimental and editing line 27 of tabular_synthetic_data/base.py in the SyntheticDataGenerator class

BEFORE:
llm: Optional[BaseLanguageModel] = None

AFTER:
llm: Optional[BaseChatModel] = None

I am not sure why this worked. BaseLanguageModel is the parent class of BaseChatModel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant