diff --git a/docs/docs/how-tos/human_in_the_loop/wait-user-input.ipynb b/docs/docs/how-tos/human_in_the_loop/wait-user-input.ipynb
index 33daf4125..e34b91439 100644
--- a/docs/docs/how-tos/human_in_the_loop/wait-user-input.ipynb
+++ b/docs/docs/how-tos/human_in_the_loop/wait-user-input.ipynb
@@ -327,6 +327,19 @@
"We will use OpenAI and / or Anthropic's models and a fake tool (just for demo purposes)."
]
},
+ {
+ "cell_type": "markdown",
+ "id": "b3b8b7e5",
+ "metadata": {},
+ "source": [
+ "
\n",
+ "
Using Pydantic with LangChain
\n",
+ "
\n",
+ " This notebook uses Pydantic v2 BaseModel, which requires langchain-core >= 0.3. Using langchain-core < 0.3 will result in errors due to mixing of Pydantic v1 and v2 BaseModels.\n",
+ "
\n",
+ "
"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 47,
@@ -374,17 +387,7 @@
"model = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\")\n",
"model = ChatOpenAI(model=\"gpt-4o\")\n",
"\n",
- "# NOTE:\n",
- "# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
- "# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
- "from langchain_core import __version__ as core_version\n",
- "from packaging import version\n",
- "\n",
- "core_version = version.parse(core_version)\n",
- "if (core_version.major, core_version.minor) < (0, 3):\n",
- " from pydantic.v1 import BaseModel\n",
- "else:\n",
- " from pydantic import BaseModel\n",
+ "from pydantic import BaseModel\n",
"\n",
"# We are going \"bind\" all tools to the model\n",
"# We have the ACTUAL tools from above, but we also need a mock tool to ask a human\n",
@@ -400,11 +403,6 @@
"\n",
"# Define nodes and conditional edges\n",
"\n",
- "from langchain_core.messages import ToolMessage\n",
- "\n",
- "from langgraph.prebuilt import ToolInvocation\n",
- "\n",
- "\n",
"# Define the function that determines whether to continue or not\n",
"def should_continue(state):\n",
" messages = state[\"messages\"]\n",
diff --git a/docs/docs/how-tos/many-tools.ipynb b/docs/docs/how-tos/many-tools.ipynb
index 725ca7326..0016092f0 100644
--- a/docs/docs/how-tos/many-tools.ipynb
+++ b/docs/docs/how-tos/many-tools.ipynb
@@ -153,7 +153,7 @@
"outputs": [],
"source": [
"from langchain_core.documents import Document\n",
- "from langchain_core.vectorstores import InMemoryVectorStore, VectorStore\n",
+ "from langchain_core.vectorstores import InMemoryVectorStore\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"tool_documents = [\n",
@@ -195,7 +195,7 @@
"from langchain_openai import ChatOpenAI\n",
"from typing_extensions import TypedDict\n",
"\n",
- "from langgraph.graph import StateGraph, START, END\n",
+ "from langgraph.graph import StateGraph, START\n",
"from langgraph.graph.message import add_messages\n",
"from langgraph.prebuilt import ToolNode, tools_condition\n",
"\n",
@@ -343,6 +343,19 @@
"We implement this change below. For demonstration purposes, we simulate an error in the initial tool selection by adding a `hack_remove_tool_condition` to the `select_tools` node, which removes the correct tool on the first iteration of the node. Note that on the second iteration, the agent finishes the run as it has access to the correct tool."
]
},
+ {
+ "cell_type": "markdown",
+ "id": "985a5388",
+ "metadata": {},
+ "source": [
+ "\n",
+ "
Using Pydantic with LangChain
\n",
+ "
\n",
+ " This notebook uses Pydantic v2 BaseModel, which requires langchain-core >= 0.3. Using langchain-core < 0.3 will result in errors due to mixing of Pydantic v1 and v2 BaseModels.\n",
+ "
\n",
+ "
"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 8,
@@ -353,17 +366,7 @@
"from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage\n",
"from langgraph.pregel.retry import RetryPolicy\n",
"\n",
- "# NOTE:\n",
- "# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
- "# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
- "from langchain_core import __version__ as core_version\n",
- "from packaging import version\n",
- "\n",
- "core_version = version.parse(core_version)\n",
- "if (core_version.major, core_version.minor) < (0, 3):\n",
- " from pydantic.v1 import BaseModel, Field\n",
- "else:\n",
- " from pydantic import BaseModel, Field\n",
+ "from pydantic import BaseModel, Field\n",
"\n",
"\n",
"class QueryForTools(BaseModel):\n",
diff --git a/docs/docs/how-tos/map-reduce.ipynb b/docs/docs/how-tos/map-reduce.ipynb
index 5e6a5019f..e88fe61dc 100644
--- a/docs/docs/how-tos/map-reduce.ipynb
+++ b/docs/docs/how-tos/map-reduce.ipynb
@@ -84,6 +84,19 @@
"## Define the graph"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "66803b55",
+ "metadata": {},
+ "source": [
+ "\n",
+ "
Using Pydantic with LangChain
\n",
+ "
\n",
+ " This notebook uses Pydantic v2 BaseModel, which requires langchain-core >= 0.3. Using langchain-core < 0.3 will result in errors due to mixing of Pydantic v1 and v2 BaseModels.\n",
+ "
\n",
+ "
"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 3,
@@ -99,17 +112,7 @@
"from langgraph.constants import Send\n",
"from langgraph.graph import END, StateGraph, START\n",
"\n",
- "# NOTE:\n",
- "# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
- "# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
- "from langchain_core import __version__ as core_version\n",
- "from packaging import version\n",
- "\n",
- "core_version = version.parse(core_version)\n",
- "if (core_version.major, core_version.minor) < (0, 3):\n",
- " from pydantic.v1 import BaseModel, Field\n",
- "else:\n",
- " from pydantic import BaseModel, Field\n",
+ "from pydantic import BaseModel, Field\n",
"\n",
"# Model and prompts\n",
"# Define model and prompts we will use\n",
diff --git a/docs/docs/how-tos/pass-run-time-values-to-tools.ipynb b/docs/docs/how-tos/pass-run-time-values-to-tools.ipynb
index 09c0c219e..0710309de 100644
--- a/docs/docs/how-tos/pass-run-time-values-to-tools.ipynb
+++ b/docs/docs/how-tos/pass-run-time-values-to-tools.ipynb
@@ -87,6 +87,19 @@
"In this example we'll create a tool that returns Documents and then another tool that actually cites the Documents that justify a claim."
]
},
+ {
+ "cell_type": "markdown",
+ "id": "2f7e2c8d",
+ "metadata": {},
+ "source": [
+ "\n",
+ "
Using Pydantic with LangChain
\n",
+ "
\n",
+ " This notebook uses Pydantic v2 BaseModel, which requires langchain-core >= 0.3. Using langchain-core < 0.3 will result in errors due to mixing of Pydantic v1 and v2 BaseModels.\n",
+ "
\n",
+ "
"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 6,
@@ -101,17 +114,7 @@
"from langchain_core.tools import tool\n",
"from langgraph.prebuilt import InjectedState\n",
"\n",
- "# NOTE:\n",
- "# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
- "# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
- "from langchain_core import __version__ as core_version\n",
- "from packaging import version\n",
- "\n",
- "core_version = version.parse(core_version)\n",
- "if (core_version.major, core_version.minor) < (0, 3):\n",
- " from pydantic.v1 import BaseModel\n",
- "else:\n",
- " from pydantic import BaseModel\n",
+ "from pydantic import BaseModel\n",
"\n",
"\n",
"@tool(parse_docstring=True, response_format=\"content_and_artifact\")\n",
@@ -311,9 +314,6 @@
"metadata": {},
"outputs": [],
"source": [
- "from copy import deepcopy\n",
- "\n",
- "from langchain_core.messages import ToolMessage\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"from langgraph.prebuilt import ToolNode\n",
diff --git a/docs/docs/how-tos/state-context-key.ipynb b/docs/docs/how-tos/state-context-key.ipynb
index ec4f820b0..a6e983b31 100644
--- a/docs/docs/how-tos/state-context-key.ipynb
+++ b/docs/docs/how-tos/state-context-key.ipynb
@@ -189,6 +189,19 @@
"Here we're defining the context object as a pydantic model, which is created by the factory function decorated with @contextmanager. @contextmanager ensures any cleanup code you need can be run at the end of the execution"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "9e1428b9",
+ "metadata": {},
+ "source": [
+ "\n",
+ "
Using Pydantic with LangChain
\n",
+ "
\n",
+ " This notebook uses Pydantic v2 BaseModel, which requires langchain-core >= 0.3. Using langchain-core < 0.3 will result in errors due to mixing of Pydantic v1 and v2 BaseModels.\n",
+ "
\n",
+ "
"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 9,
@@ -200,17 +213,7 @@
"from contextlib import contextmanager\n",
"from langchain_core.runnables import RunnableConfig\n",
"\n",
- "# NOTE:\n",
- "# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
- "# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
- "from langchain_core import __version__ as core_version\n",
- "from packaging import version\n",
- "\n",
- "core_version = version.parse(core_version)\n",
- "if (core_version.major, core_version.minor) < (0, 3):\n",
- " from pydantic.v1 import BaseModel\n",
- "else:\n",
- " from pydantic import BaseModel\n",
+ "from pydantic import BaseModel\n",
"\n",
"\n",
"class AgentContext(BaseModel):\n",
diff --git a/docs/docs/how-tos/state-model.ipynb b/docs/docs/how-tos/state-model.ipynb
index de9acb5da..fe02507df 100644
--- a/docs/docs/how-tos/state-model.ipynb
+++ b/docs/docs/how-tos/state-model.ipynb
@@ -52,7 +52,7 @@
"metadata": {},
"outputs": [
{
- "name": "stdin",
+ "name": "stdout",
"output_type": "stream",
"text": [
"OPENAI_API_KEY: ········\n"
@@ -204,6 +204,19 @@
"Therefore, we will use a `pydantic.BaseModel` with one key (`messages`) and annotate it so that the `messages` attribute is treated as \"append-only\".\n"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "2d928f61",
+ "metadata": {},
+ "source": [
+ "\n",
+ "
Using Pydantic with LangChain
\n",
+ "
\n",
+ " This notebook uses Pydantic v2 BaseModel, which requires langchain-core >= 0.3. Using langchain-core < 0.3 will result in errors due to mixing of Pydantic v1 and v2 BaseModels.\n",
+ "
\n",
+ "
"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 6,
@@ -216,18 +229,7 @@
"\n",
"from langchain_core.messages import BaseMessage\n",
"\n",
- "# NOTE:\n",
- "# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
- "# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
- "from langchain_core import __version__ as core_version\n",
- "from packaging import version\n",
- "\n",
- "core_version = version.parse(core_version)\n",
- "if (core_version.major, core_version.minor) < (0, 3):\n",
- " from pydantic.v1 import BaseModel\n",
- "else:\n",
- " from pydantic import BaseModel\n",
- "\n",
+ "from pydantic import BaseModel\n",
"\n",
"class AgentState(BaseModel):\n",
" messages: Annotated[Sequence[BaseMessage], operator.add]"
@@ -271,11 +273,6 @@
"metadata": {},
"outputs": [],
"source": [
- "from langchain_core.messages import ToolMessage\n",
- "\n",
- "from langgraph.prebuilt import ToolInvocation\n",
- "\n",
- "\n",
"# Define the function that determines whether to continue or not\n",
"def should_continue(state):\n",
" messages = state.messages\n",
diff --git a/docs/docs/how-tos/tool-calling-errors.ipynb b/docs/docs/how-tos/tool-calling-errors.ipynb
index 0c97900ca..d715fc644 100644
--- a/docs/docs/how-tos/tool-calling-errors.ipynb
+++ b/docs/docs/how-tos/tool-calling-errors.ipynb
@@ -280,21 +280,9 @@
"source": [
"from langchain_core.output_parsers import StrOutputParser\n",
"\n",
- "# NOTE:\n",
- "# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
- "# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
- "from langchain_core import __version__ as core_version\n",
- "from packaging import version\n",
- "\n",
- "core_version = version.parse(core_version)\n",
- "if (core_version.major, core_version.minor) < (0, 3):\n",
- " from pydantic.v1 import BaseModel, conlist\n",
- " class HaikuRequest(BaseModel):\n",
- " topic: conlist(str, min_items=3, max_items=3)\n",
- "else:\n",
- " from pydantic import BaseModel, conlist\n",
- " class HaikuRequest(BaseModel):\n",
- " topic: conlist(str, min_length=3, max_length=3)\n",
+ "from pydantic import BaseModel, conlist\n",
+ "class HaikuRequest(BaseModel):\n",
+ " topic: conlist(str, min_length=3, max_length=3)\n",
"\n",
"\n",
"@tool\n",