langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.06k stars 14.96k forks source link

Bug: model Field Missing Error in Replicate Class Due to Misidentification in Validator #26937

Open Pooyash1998 opened 1 week ago

Pooyash1998 commented 1 week ago

Checked other resources

Example Code

from langchain_community.llms import Replicate

replicate = Replicate( model="meta/meta-llama-3-405b-instruct", model_kwargs={"temperature": 0.7} )

Error Message and Stack Trace (if applicable)

pydantic_core._pydantic_core.ValidationError: 1 validation error for Replicate model Field required [type=missing, input_value=..., input_type=..., ...]

Description

When initializing the Replicate class, the model field is incorrectly treated as an extra field and moved into model_kwargs, resulting in a validation error. This happens due to the way the build_extra validator processes the field aliases. Suggested Fix: In the build_extra method, replace the use of field.alias with field.name to ensure proper recognition of all fields: `@model_validator(mode="before") @classmethod def build_extra(cls, values: Dict[str, Any]) -> Any: all_required_field_names = {field.name for field in get_fields(cls).values()}

Remaining logic...`

Or defining "model" as an alias in a Field can also solve the problem : class Replicate(LLM): model: str = Field(..., alias="model")

System Info

System Information

OS: Linux OS Version: #1 SMP Fri Mar 29 23:14:13 UTC 2024 Python Version: 3.12.6 (main, Sep 10 2024, 00:05:17) [GCC 11.4.0]

Package Information

langchain_core: 0.3.6 langchain: 0.3.1 langchain_community: 0.3.1 langsmith: 0.1.129 langchain_chroma: 0.1.4 langchain_huggingface: 0.1.0 langchain_ollama: 0.2.0 langchain_openai: 0.2.0 langchain_text_splitters: 0.3.0 langserve: 0.3.0

Optional packages not installed

langgraph

Other Dependencies

aiohttp: 3.10.5 async-timeout: 4.0.3 chromadb: 0.5.7 dataclasses-json: 0.6.7 fastapi: 0.114.2 httpx: 0.27.2 huggingface-hub: 0.24.7 jsonpatch: 1.33 numpy: 1.26.4 ollama: 0.3.3 openai: 1.45.1 orjson: 3.10.7 packaging: 24.1 pydantic: 2.9.1 pydantic-settings: 2.5.2 PyYAML: 6.0.2 requests: 2.32.3 sentence-transformers: 3.1.0 SQLAlchemy: 2.0.34 sse-starlette: 1.8.2 tenacity: 8.5.0 tiktoken: 0.7.0 tokenizers: 0.19.1 transformers: 4.44.2 typing-extensions: 4.12.2

devneel commented 4 days ago

Having this same issue. Copy pasting the example from https://python.langchain.com/docs/integrations/llms/replicate/ doesn't work. I get the model Field Missing Error

ken-vat commented 3 days ago

same here, even with model included in kwargs - llm = Replicate( model_kwargs={"model": "meta/meta-llama-3-70b-instruct", "temperature": 0, "max_length": 300, "top_p": 1, "stop": []}, )

Pooyash1998 commented 2 days ago

A temporary fix is to copy replicate.py from langchain library, modify it by adding the alias to "model" like this : class Replicate(LLM): model: str = Field(..., alias="model") and then import the Replicate class directly from your project and not from langchain library