mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-06 09:47:51 +02:00
docs: remove pydantic v1 fallback from how-tos (#1711)
* docs: remove pydantic v1 fallback from how-tos * sample callout * update to use callout
This commit is contained in:
@@ -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": [
|
||||
"<div class=\"admonition note\">\n",
|
||||
" <p class=\"admonition-title\">Using Pydantic with LangChain</p>\n",
|
||||
" <p>\n",
|
||||
" This notebook uses Pydantic v2 <code>BaseModel</code>, which requires <code>langchain-core >= 0.3</code>. Using <code>langchain-core < 0.3</code> will result in errors due to mixing of Pydantic v1 and v2 <code>BaseModels</code>.\n",
|
||||
" </p>\n",
|
||||
"</div> "
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
|
||||
@@ -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": [
|
||||
"<div class=\"admonition note\">\n",
|
||||
" <p class=\"admonition-title\">Using Pydantic with LangChain</p>\n",
|
||||
" <p>\n",
|
||||
" This notebook uses Pydantic v2 <code>BaseModel</code>, which requires <code>langchain-core >= 0.3</code>. Using <code>langchain-core < 0.3</code> will result in errors due to mixing of Pydantic v1 and v2 <code>BaseModels</code>.\n",
|
||||
" </p>\n",
|
||||
"</div> "
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
|
||||
@@ -84,6 +84,19 @@
|
||||
"## Define the graph"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "66803b55",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div class=\"admonition note\">\n",
|
||||
" <p class=\"admonition-title\">Using Pydantic with LangChain</p>\n",
|
||||
" <p>\n",
|
||||
" This notebook uses Pydantic v2 <code>BaseModel</code>, which requires <code>langchain-core >= 0.3</code>. Using <code>langchain-core < 0.3</code> will result in errors due to mixing of Pydantic v1 and v2 <code>BaseModels</code>.\n",
|
||||
" </p>\n",
|
||||
"</div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
|
||||
@@ -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": [
|
||||
"<div class=\"admonition note\">\n",
|
||||
" <p class=\"admonition-title\">Using Pydantic with LangChain</p>\n",
|
||||
" <p>\n",
|
||||
" This notebook uses Pydantic v2 <code>BaseModel</code>, which requires <code>langchain-core >= 0.3</code>. Using <code>langchain-core < 0.3</code> will result in errors due to mixing of Pydantic v1 and v2 <code>BaseModels</code>.\n",
|
||||
" </p>\n",
|
||||
"</div> "
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
|
||||
@@ -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": [
|
||||
"<div class=\"admonition note\">\n",
|
||||
" <p class=\"admonition-title\">Using Pydantic with LangChain</p>\n",
|
||||
" <p>\n",
|
||||
" This notebook uses Pydantic v2 <code>BaseModel</code>, which requires <code>langchain-core >= 0.3</code>. Using <code>langchain-core < 0.3</code> will result in errors due to mixing of Pydantic v1 and v2 <code>BaseModels</code>.\n",
|
||||
" </p>\n",
|
||||
"</div> "
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
|
||||
@@ -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": [
|
||||
"<div class=\"admonition note\">\n",
|
||||
" <p class=\"admonition-title\">Using Pydantic with LangChain</p>\n",
|
||||
" <p>\n",
|
||||
" This notebook uses Pydantic v2 <code>BaseModel</code>, which requires <code>langchain-core >= 0.3</code>. Using <code>langchain-core < 0.3</code> will result in errors due to mixing of Pydantic v1 and v2 <code>BaseModels</code>.\n",
|
||||
" </p>\n",
|
||||
"</div> "
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user