-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langgraph: use create_model_v2 and fallback on create_model (#1708)
* langgraph: use create_model_v2 and fallback on create_model * fix * code review * fix
- Loading branch information
Showing
3 changed files
with
45 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Any, Dict, Optional, Union | ||
|
||
from pydantic import BaseModel | ||
from pydantic.v1 import BaseModel as BaseModelV1 | ||
|
||
|
||
def create_model( | ||
model_name: str, | ||
*, | ||
field_definitions: Optional[Dict[str, Any]] = None, | ||
root: Optional[Any] = None, | ||
) -> Union[BaseModel, BaseModelV1]: | ||
"""Create a pydantic model with the given field definitions. | ||
Args: | ||
model_name: The name of the model. | ||
field_definitions: The field definitions for the model. | ||
root: Type for a root model (RootModel) | ||
""" | ||
try: | ||
# for langchain-core >= 0.3.0 | ||
from langchain_core.runnables.pydantic import create_model_v2 | ||
|
||
return create_model_v2( | ||
model_name, | ||
field_definitions=field_definitions, | ||
root=root, | ||
) | ||
except ImportError: | ||
# for langchain-core < 0.3.0 | ||
from langchain_core.runnables.utils import create_model | ||
|
||
v1_kwargs = {} | ||
if root is not None: | ||
v1_kwargs["__root__"] = root | ||
|
||
return create_model(model_name, **v1_kwargs, **(field_definitions or {})) |