{"id":600,"date":"2025-11-05T07:00:00","date_gmt":"2025-11-04T23:00:00","guid":{"rendered":"https:\/\/smartaiunion.com\/?p=600"},"modified":"2025-11-03T14:28:24","modified_gmt":"2025-11-03T06:28:24","slug":"langchain-v1langgraph-v1%e4%b8%ad%e7%9a%84runtime%e5%92%8ctoolruntime","status":"publish","type":"post","link":"https:\/\/smartaiunion.com\/index.php\/2025\/11\/05\/600\/","title":{"rendered":"langchain v1&amp;langgraph v1\u4e2d\u7684Runtime\u548cToolRuntime"},"content":{"rendered":"\n<p>langchain\u5df2\u4e8e10\u6708\u5e95\u53d1\u5e03\u4e86\u6b63\u5f0f\u7248\u7684v1\u7248\u672c\u3002\u672c\u6b21\u6211\u4eec\u4e3b\u8981\u8ba8\u8bba\u7684\u662f\u5176\u4e2d\u7684\u4e24\u4e2a\u6bd4\u8f83\u5bb9\u6613\u6df7\u6dc6\u7684\u7ec4\u4ef6\u3002\u5b83\u4eec\u90fd\u5e26\u6709runtime\u8fd9\u4e2a\u5b57\u773c\u3002\u6839\u636e\u76ee\u524d\u7684\u60c5\u51b5\u6765\u770b\uff0c\u5f88\u591a\u4eba\u4f1a\u628a\u8fd9\u4e24\u4e2a\u5185\u5bb9\u6df7\u6dc6\u4e86\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u4e0a\u5b98\u7f51\u7684\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u7ec4\u4ef6\u7684\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<p><strong>Runtime:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>langchain<\/strong>:\u00a0<a href=\"https:\/\/link.juejin.cn\/?target=https%3A%2F%2Fdocs.langchain.com%2Foss%2Fpython%2Flangchain%2Fruntime\" target=\"_blank\" rel=\"noreferrer noopener\">docs.langchain.com\/oss\/python\/\u2026<\/a><\/li>\n\n\n\n<li><strong>langgraph<\/strong>:\u00a0<a href=\"https:\/\/link.juejin.cn\/?target=http%3A%2F%2Fdocs.langchain.com%2Foss%2Fpython%2Flanggraph%2Fgraph-api%23runtime-context\" target=\"_blank\" rel=\"noreferrer noopener\">docs.langchain.com\/oss\/python\/\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>ToolRuntime<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/link.juejin.cn\/?target=https%3A%2F%2Fdocs.langchain.com%2Foss%2Fpython%2Flangchain%2Ftools%23toolruntime\" target=\"_blank\" rel=\"noreferrer noopener\">docs.langchain.com\/oss\/python\/\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u5176\u5b9e\u662f\u6bd4\u8f83\u4e71\u7684\uff0c\u5c24\u5176\u662fRuntime\u5206\u6563\u5728langchain\u548clanggraph\u4e24\u4e2a\u6587\u6863\u4e2d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Runtime<\/h2>\n\n\n\n<p>\u9996\u5148\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u4e2a<strong>Runtime<\/strong>\u3002 \u4e25\u683c\u6765\u8bf4\uff0c\u8fd9\u4e2aRuntime\u662f\u5728langgraph 0.6\u7248\u672c\u5c31\u5f15\u5165\u4e86\u7684\u3002\u4f46\u662f\u5728v1\u7248\u672c\u4e2dlangchain\u5bf9\u5176\u5e95\u5c42\u8fdb\u884c\u4e86\u91cd\u5199\uff0c\u5177\u4f53\u662fagent\u6a21\u5757\u91c7\u7528langgraph\u8fdb\u884c\u7f16\u6392\u3002\u6545Runtime\u4e5f\u80fd\u591f\u5728langchain\u4e2d\u4f7f\u7528\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u4e00\u822c\u662f\u7528\u5728middleware\u4e2d\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u662f\u552f\u4e00\u4e00\u4e2a\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u7075\u6d3b\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9\u3002 Runtime\u603b\u5171\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u5c5e\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>context<\/li>\n\n\n\n<li>store<\/li>\n\n\n\n<li>stream_writer<\/li>\n\n\n\n<li>previous<\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u9762\u4e00\u4e2a\u4e00\u4e2a\u4ecb\u7ecd\uff0c\u9996\u5148\u662f\u8fd9\u4e2a\u5927\u5bb6\u90fd\u4e0d\u592a\u4e86\u89e3\u4f46\u662f\u4f5c\u7528\u975e\u5e38\u5927\u7684Context\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Context<\/h3>\n\n\n\n<p>Context \u4e3b\u8981\u7528\u4e8e\u5728\u56fe\u8fd0\u884c\u65f6\u4f20\u9012\u9759\u6001\u6570\u636e\uff0c\u5176\u6838\u5fc3\u5b9a\u4f4d\u662f\u66ff\u4ee3\u539f\u6709\u7684&nbsp;<code>RunnableConfig<\/code>&nbsp;\u673a\u5236\u3002\u5728\u6b64\u4e4b\u524d\u8981\u5411\u56fe\u4f20\u9012\u4e00\u4e9b\u6570\u636e\u6bd4\u8f83\u9ebb\u70e6\uff0c\u5e38\u89c1\u7684\u65b9\u6848\u5b58\u5728\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u76f4\u63a5\u4f7f\u7528\u00a0<code>state<\/code><\/strong>\uff1a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u00a0<code>state<\/code>\u00a0\u901a\u5e38\u9700\u8981\u5728\u540e\u7eed\u56fe\u8fd0\u884c\u4e2d\u88ab\u5904\u7406<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u00a0RunnableConfig\u7684<code>configurable<\/code>\u00a0\u5c5e\u6027<\/strong>\uff1a\u5b58\u5728\u7c7b\u578b\u5b89\u5168\u9690\u60a3\u3002\u7531\u4e8e\u00a0<code>configurable<\/code>\u00a0\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5728 Python \u8fd9\u79cd\u52a8\u6001\u8bed\u8a00\u4e2d\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u4f1a\u5bfc\u81f4\u00a0<code>KeyError<\/code>\uff0c\u5373\u4f7f\u7528\u00a0<code>get()<\/code>\u00a0\u65b9\u6cd5\u907f\u514d\u5f02\u5e38\uff0c\u4e5f\u4f1a\u56e0\u4e3a\u8fd9\u4e2a\u952e\u503c\u4e0d\u5b58\u5728\u800c\u6709\u522b\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u83b7\u53d6\u5230\u7684\u503c\u7c7b\u578b\u672a\u77e5\uff0c\u7f3a\u4e4f\u7f16\u8f91\u5668\u7684\u9759\u6001\u7c7b\u578b\u68c0\u67e5<\/li>\n\n\n\n<li>\u65e0\u6cd5\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Context \u7684\u4f18\u52bf<\/strong><\/p>\n\n\n\n<p>&nbsp;<code>Runtime<\/code>&nbsp;\u672c\u8d28\u662f\u4e00\u4e2a\u6cdb\u578b\u7c7b\uff0c\u4f7f\u7528&nbsp;<code>Runtime[Type]<\/code>&nbsp;\u7684\u5f62\u5f0f\u6765\u6807\u6ce8\u7279\u5b9a\u7c7b\u578b\u7684\u8fd0\u884c\u65f6\u5b9e\u4f8b\u3002\u8fd9\u91cc\u7684&nbsp;<code>Type<\/code>&nbsp;\u901a\u5e38\u662f\u4e00\u4e2a Python \u6570\u636e\u7c7b\uff08dataclass\uff09\uff0c\u4ee3\u8868\u7684\u5c31\u662f\u8fd9\u4e2aContext\u3002\u5373\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u662f<code>Runtime[Type[Context]]<\/code>\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u6cdb\u578b\uff0c\u6545\u7f16\u8f91\u5668\u53ef\u4ee5\u9760\u8fd9\u4e2a\u63a8\u65ad\u7c7b\u578b\u3002\u540c\u65f6\u4f60\u4e5f\u53ef\u4ee5\u4ece\u8fd9\u91cc\u77e5\u9053\uff0c\u8981\u5b9a\u4e49\u4e00\u4e2aContext\u5176\u5b9e\u5c31\u662f\u5199\u4e00\u4e2aPython\u6570\u636e\u7c7b\uff08\u5176\u5b9ePydantic\u7684\u6a21\u578b\u7c7b\u5e94\u8be5\u662f\u53ef\u4ee5\u7684\uff0c\u4e0d\u8fc7\u5b98\u7f51\u4e00\u76f4\u7528\u7684\u662f\u6570\u636e\u7c7b\uff09\u3002\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u6570\u636e\u7c7b\uff0c\u56e0\u6b64\u6700\u7ec8\u5b9e\u4f8b\u5316\u7684\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u6570\u636e\u7c7b\u5b9e\u4f8b\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5c5e\u6027\u7684\u7c7b\u578b\u7f16\u8f91\u5668\u662f\u5b8c\u5168\u53ef\u4ee5\u77e5\u9053\u7684\u8fd9\u662f\u6570\u636e\u7c7b\u5bf9\u8c61\u7684\u7279\u70b9\u3002\u6545\u53ef\u4ee5\u6709\u6548\u7684\u89e3\u51b3\u7c7b\u578b\u7684\u95ee\u9898\u3002\u5728\u540e\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u4f1a\u77e5\u9053\u5982\u4f55\u7075\u6d3b\u7684\u4f7f\u7528<code>Context<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Store<\/h3>\n\n\n\n<p>store\u662f\u7528\u4e8e\u5b58\u50a8\u6301\u4e45\u5316\u7684\u5185\u5bb9\u7684\uff0c\u5373\u4f60compile\u4e00\u4e2agraph\u65f6\u5019\u53ef\u4ee5\u9009\u62e9\u6027\u7684\u4f20\u5165\u7684\u90a3\u4e2astore\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">stream_writer<\/h3>\n\n\n\n<p>stream_write\u5219\u662f\u5728\u6d41\u5f0f\u7684\u65f6\u5019\u4f20\u9012\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u54cd\u5e94\u6570\u636e\u7684\u3002\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u8bbe\u7f6estream_mode\u53d6\u503c\u6709&#8221;custom&#8221;\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Previous<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u5c5e\u6027\u7528\u5904\u4e0d\u5927\uff0c\u662f\u8bb0\u5f55\u4e0a\u4e00\u4e2anode\u8fd0\u884c\u7684\u7ed3\u679c\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ToolRuntime<\/h2>\n\n\n\n<p>\u9605\u8bfb\u5230\u8fd9\uff0c\u76f8\u4fe1\u5f88\u591a\u8bfb\u8005\u4f1a\u6709\u4e00\u4e2a\u95ee\u9898\uff0cRuntime\u5f88\u5f3a\u5927\uff0c\u4f46\u662f\u4e3a\u5565\u5b98\u65b9\u8fd8\u8981\u5b9a\u4e49\u4e00\u4e2aToolRuntime\uff0c\u5b83\u4eec\u4e24\u4e4b\u95f4\u6709\u5565\u533a\u522b\uff1f \u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5148\u60f3\u4e00\u4e0b\u8fd9\u4e2aRuntime\u76f4\u63a5\u7528\u5230tools\u4e2d\u4f1a\u6709\u5565\u95ee\u9898\u3002\u7b2c\u4e00\u4e2a\u95ee\u9898\uff0c\u6ca1\u6709\u529e\u6cd5\u83b7\u53d6state\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u8fd9\u4e2a\u5c5e\u6027\uff0c\u7b2c\u4e8c\u4e2a\u95ee\u9898\u6ca1\u6709\u529e\u6cd5\u83b7\u53d6\u8fd9\u4e2atool_call_id\u3002\u6545\u9488\u5bf9\u8fd9\u4e9b\u95ee\u9898\u5b98\u65b9\u624d\u641e\u4e86\u4e00\u4e2aToolRuntime\uff0c\u987e\u540d\u601d\u4e49\uff0c\u5c31\u662f\u7ed9tools\u4e2d\u4f7f\u7528\u7684\u3002 \u8fd9\u4e2aToolRuntime\u9664\u4e86\u6709\u4e0a\u9762\u6240\u8bf4\u7684Context\u3001Store\u3001Stream_write\u4ee5\u5916\u8fd8\u6709\u4e24\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u5c5e\u6027\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">State<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u662flanggraph\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5728node\u4e2d\u5462\uff0c\u5f80\u5f80\u6211\u4eec\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u8fd9\u4e2astate\uff0c\u800ctools\u4e00\u822c\u524d\u9762\u51e0\u4e2a\u53c2\u6570\u662f\u7ed9\u5927\u6a21\u578b\u51c6\u5907\u7684\uff0c\u6709\u5b83\u4eec\u4f20\u9012\uff0c\u5728\u6ca1\u6709ToolRuntime\u7684\u65f6\u5019\uff0c\u8981\u83b7\u53d6state\u5f88\u9ebb\u70e6\uff0c\u8981\u4f7f\u7528Inject\u6ce8\u89e3\u83b7\u53d6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ToolCallID<\/h3>\n\n\n\n<p>\u6bcf\u4e2a\u5de5\u5177\u8c03\u7528\u90fd\u4f1a\u6709\u5bf9\u5e94\u7684ID\uff0c\u5728\u6ca1\u6709ToolRuntime\u7684\u65f6\u5019\uff0c\u8981\u83b7\u53d6ToolCallID\u4e5f\u5f88\u9ebb\u70e6\uff0c\u8981\u4e48\u662f\u904d\u5386state\u4e2d\u7684messages\u5217\u8868\uff0c\u8981\u4e48\u662f\u4f7f\u7528Inject\u6ce8\u89e3\u83b7\u53d6\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\u5982\u679c\u4f60\u8981\u540c\u65f6\u83b7\u53d6\u8fd9\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5185\u5bb9\uff0c\u90a3\u4e48\u4e00\u822c\u4f60\u9700\u8981\u5199\u4e24\u4e2aInject\uff0c\u8fd9\u6837\u975e\u5e38\u9ebb\u70e6\u3002\u4e8e\u662f\u5728v1\u7248\u672c\uff0c\u5b98\u65b9\u7ed9\u4f60\u52a0\u4e86\u4e2aToolRuntime\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u793a\u4f8b<\/h2>\n\n\n\n<p>\u76f8\u4fe1\uff0c\u8bfb\u5230\u8fd9\u91cc\u4f60\u5bf9\u8fd9\u4e24\u4e2a\u65b0\u7684API\u5df2\u7ecf\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u4e86\u5427\uff0c\u4e8e\u662f\u6211\u4eec\u5f00\u59cb\u4e00\u4e2a\u793a\u4f8b\uff0c\u719f\u6089\u5176\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u5728\u9605\u8bfb\u4e0b\u9762\u7684\u793a\u4f8b\u7684\u5185\u5bb9\u4f60\u9700\u8981\u6ce8\u610f2\u70b9<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u672c\u6b21\u7684\u793a\u4f8b\u4f7f\u7528\u7684\u5927\u6a21\u578b\u662fqwen\uff0c\u901a\u8fc7<code>langchain-dev-utils<\/code>(<a href=\"https:\/\/link.juejin.cn\/?target=https%3A%2F%2Ftbice123123.github.io%2Flangchain-dev-utils-docs%2Fzh%2F\" target=\"_blank\" rel=\"noreferrer noopener\">tbice123123.github.io\/langchain-d\u2026<\/a>)\u63d0\u4f9b\u7684\u65b9\u6cd5\u63a5\u5165\u3002<\/li>\n\n\n\n<li>\u8bf7\u786e\u4fddlanggraph\u662f\u6700\u65b0\u7684\u7248\u672c\uff081.0.2\uff09\u5426\u5219\u4f1a\u6709ToolRuntime\u65e0\u6cd5\u4f7f\u7528\u7684\u95ee\u9898\uff0c\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b98\u65b9\u5df2\u7ecf\u89e3\u51b3(<a href=\"https:\/\/link.juejin.cn\/?target=https%3A%2F%2Fgithub.com%2Flangchain-ai%2Flanggraph%2Fissues%2F6318\" target=\"_blank\" rel=\"noreferrer noopener\">github.com\/langchain-a\u2026<\/a>)<\/li>\n<\/ol>\n\n\n\n<p>\u9996\u5148\u653e\u4e00\u4e0b\u6838\u5fc3\u7684\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python\u4f53\u9a8cAI\u4ee3\u7801\u52a9\u624b\u4ee3\u7801\u89e3\u8bfb\u590d\u5236\u4ee3\u7801<code>import json\nfrom typing import Literal\nfrom dataclasses import dataclass\nfrom langchain.tools import ToolRuntime, tool\nfrom langchain_core.messages import HumanMessage, SystemMessage\nfrom langgraph.graph.message import MessagesState\nfrom langgraph.graph.state import StateGraph\nfrom langgraph.prebuilt import ToolNode\nfrom langgraph.runtime import Runtime\nfrom langchain_dev_utils.chat_models import register_model_provider, load_chat_model\nfrom dotenv import load_dotenv\nfrom langgraph.types import Command\nfrom langchain_dev_utils.tool_calling import has_tool_calling\nload_dotenv()\n\nregister_model_provider(\n&nbsp; &nbsp; \"dashscope\",\n&nbsp; &nbsp; \"openai-compatible\",\n)\n\nclass State(MessagesState):\n&nbsp; &nbsp; pass\n\n\ndata = [\n&nbsp; &nbsp; {\"id\": 1, \"book_name\": \"\u300a\u5927\u6a21\u578b\u5e94\u7528\u5f00\u53d1\u300b\", \"user_id\": 1, \"price\": 100},\n&nbsp; &nbsp; {\"id\": 2, \"book_name\": \"\u300a\u6df1\u5ea6\u5b66\u4e60\u300b\", \"user_id\": 2, \"price\": 120},\n&nbsp; &nbsp; {\"id\": 3, \"book_name\": \"\u300a\u7efc\u5408\u4eba\u5de5\u667a\u80fd\u300b\", \"user_id\": 3, \"price\": 200},\n&nbsp; &nbsp; {\"id\": 4, \"book_name\": \"\u300a\u4eba\u5de5\u667a\u80fd\u300b\", \"user_id\": 1, \"price\": 30},\n&nbsp; &nbsp; {\"id\": 5, \"book_name\": \"\u300a\u5c0f\u738b\u5b50\u300b\", \"user_id\": 1, \"price\": 50},\n]\n\n\n@dataclass\nclass User:\n&nbsp; &nbsp; user_id: int = 1\n&nbsp; &nbsp; user_name: str = \"\u5f20\u4e09\"\n\n  \n@tool\ndef get_book_with_price_range(\n&nbsp; &nbsp; low: int,\n&nbsp; &nbsp; high: int,\n&nbsp; &nbsp; runtime: ToolRuntime[User],\n):\n\n&nbsp; &nbsp; \"\"\"\u6839\u636e\u4ef7\u683c\u8303\u56f4\u548c\u7528\u6237id\uff08\u65e0\u9700\u63d0\u4f9b\uff09\u67e5\u8be2\u4e66\u7c4d\"\"\"\n&nbsp; &nbsp; user_id = runtime.context.user_id\n&nbsp; &nbsp; return json.dumps(\n&nbsp; &nbsp; &nbsp; &nbsp; [\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; book\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for book in data\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if low &lt;= book[\"price\"] &lt;= high and book[\"user_id\"] == user_id\n&nbsp; &nbsp; &nbsp; &nbsp; ]\n&nbsp; &nbsp; )\n\n  \nasync def call_model(\n&nbsp; &nbsp; state: State, runtime: Runtime[User]\n) -&gt; Command[Literal[\"tool_node\", \"__end__\"]]:\n&nbsp; &nbsp; model = load_chat_model(\"dashscope:qwen-flash\").bind_tools(\n&nbsp; &nbsp; &nbsp; &nbsp; tools=[get_book_with_price_range]\n&nbsp; &nbsp; )\n&nbsp; &nbsp; user = runtime.context.user_name\n&nbsp; &nbsp; response = await model.ainvoke(\n&nbsp; &nbsp; &nbsp; &nbsp; [\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemMessage(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content=f\"\u4f60\u662f\u4e00\u4e2a\u6570\u636e\u5e93\u7ba1\u7406\u5458\uff0c\u53ef\u4ee5\u9488\u5bf9\u7528\u6237**{user}**\u9700\u6c42\u8fdb\u884c\u67e5\u8be2\"\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *state[\"messages\"],\n&nbsp; &nbsp; &nbsp; &nbsp; ]\n&nbsp; &nbsp; )\n&nbsp; &nbsp; if has_tool_calling(response):\n&nbsp; &nbsp; &nbsp; &nbsp; return Command(goto=\"tool_node\", update={\"messages\": [response]})\n&nbsp; &nbsp; return Command(goto=\"__end__\", update={\"messages\": [response]})\n\n  \n  \ntool_node = ToolNode([get_book_with_price_range])\n\ngraph = StateGraph(State, context_schema=User)\ngraph.add_node(\"call_model\", call_model)\ngraph.add_node(\"tool_node\", tool_node)\ngraph.set_entry_point(\"call_model\")\ngraph.add_edge(\"tool_node\", \"call_model\")\n  \n\nagent = graph.compile()\n\n  \n \n\nasync def main():\n&nbsp; &nbsp; response = await agent.ainvoke(\n&nbsp; &nbsp; &nbsp; &nbsp; {\"messages\": [HumanMessage(content=\"\u6211\u53eb\u5565\u540d\u5b57\")]},\n&nbsp; &nbsp; &nbsp; &nbsp; context=User(),\n&nbsp; &nbsp; )\n&nbsp; &nbsp; print(response)\n\n\n\nif __name__ == \"__main__\":\n\n&nbsp; &nbsp; import asyncio\n&nbsp; &nbsp; asyncio.run(main())\n<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u4e0a\u8ff0\u4ee3\u7801\u4f60\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python\u4f53\u9a8cAI\u4ee3\u7801\u52a9\u624b\u4ee3\u7801\u89e3\u8bfb\u590d\u5236\u4ee3\u7801<code>async def call_model(\n&nbsp; &nbsp; state: State, runtime: Runtime[User]\n) -&gt; Command[Literal[\"tool_node\", \"__end__\"]]:\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86Runtime\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u662f\u5728\u8282\u70b9\u4e2d\u5b9a\u4e49\u7684\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u63d0\u9192\u4e00\u4e0b\uff0c\u6700\u597d\u8fd9\u4e2a\u53d8\u91cf\u540d\u79f0\u5c31\u53eb\u505aruntime\uff0c\u4e0d\u8981\u53eb\u505a\u522b\u7684\u3002\u56e0\u4e3a\u5f53\u6211\u4eec\u53eb\u505a\u522b\u7684\u6bd4\u5982\u8bf4<code>run_time<\/code>\uff0c\u7f16\u8f91\u5668\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/p3-xtjj-sign.byteimg.com\/tos-cn-i-73owjymdk6\/eeafa8bcd6074d9dafd4e9188604afa9~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6ZOB5YawaWNl5ZGA:q75.awebp?rk3s=f64ab15b&amp;x-expires=1762441299&amp;x-signature=fQsKIB6nlN2f8rlwuu98BqkzzN4%3D\" alt=\"image.png\"\/><\/figure>\n\n\n\n<p>\u4e14\u4f60\u8fd0\u884c\u4e5f\u4f1a\u62a5\u9519\u7684\u3002 TypeError: call_model() missing 1 required positional argument: &#8216;run_time&#8217; During task with name &#8216;call_model&#8217; and id &#8216;b49d3528-71ac-c8c1-c2ab-9b43c7710af6&#8217; \u5f53\u7136\u5177\u4f53\u4e3a\u5565\u5f97\u4ed4\u7ec6\u770b\u6e90\u7801\uff0c\u4e0d\u8fc7langgraph\u7684\u6e90\u7801\u6bd4\u8f83\u590d\u6742\u6709\u70b9\u96be\u770b\u61c2\uff0c\u6240\u4ee5\u5efa\u8bae\u6700\u597d\u5c31\u547d\u540d\u4e3aruntime\uff0c\u4f60\u770b\u5b98\u65b9\u6587\u6863\u4e5f\u662f\u8fd9\u6837\u5199\u7684\u3002 \u4e0d\u8fc7\u7c7b\u578b\u62a5\u9519\u7684\u539f\u56e0\u5f88\u597d\u89e3\u91ca\uff0c\u56e0\u4e3a\u8fd9\u4e2anode\u7684\u7c7b\u578b\u662f\u4e00\u4e2a\u534f\u8bae\u7c7b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python\u4f53\u9a8cAI\u4ee3\u7801\u52a9\u624b\u4ee3\u7801\u89e3\u8bfb\u590d\u5236\u4ee3\u7801<code>class _NodeWithRuntime(Protocol[NodeInputT_contra, ContextT]):\n    def __call__(\n        self, state: NodeInputT_contra, *, runtime: Runtime[ContextT]\n    ) -&gt; Any: ...\n<\/code><\/pre>\n\n\n\n<p>\u6545\u4e00\u822c\u7b2c\u4e00\u4e2a\u540d\u79f0\u662fstate\uff0c\u7b2c\u4e8c\u4e2a\u540d\u79f0\u662fruntime\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u65f6\u5019\u62a5\u9519\u5c31\u5f88\u79bb\u8c31\uff0c\u6309\u7406\u6765\u8bf4\u5b83\u662f\u6709\u8bc6\u522b\u5177\u4f53\u7c7b\u578b\u7684\u4ee3\u7801\u7684\u3002<\/p>\n\n\n\n<p>\u540c\u6837\u5bf9\u4e8eToolRuntime\u4e5f\u5c3d\u53ef\u80fd\u7684\u547d\u540d\u4e3aruntime\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python\u4f53\u9a8cAI\u4ee3\u7801\u52a9\u624b\u4ee3\u7801\u89e3\u8bfb\u590d\u5236\u4ee3\u7801<code>def get_book_with_price_range(\n&nbsp; &nbsp; low: int,\n&nbsp; &nbsp; high: int,\n&nbsp; &nbsp; runtime: ToolRuntime[User],\n):\n<\/code><\/pre>\n\n\n\n<p>\u5176\u5b9e\u4ece\u6e90\u7801\u6765\u770b\u597d\u50cf\u5b83\u662f\u53ef\u4ee5\u8bc6\u522b\u7c7b\u578b\u7684\uff0c\u4f46\u662f\u786e\u5b9e\u5b9a\u4e49\u4e3a\u522b\u7684\u540d\u5b57\u4e5f\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python\u4f53\u9a8cAI\u4ee3\u7801\u52a9\u624b\u4ee3\u7801\u89e3\u8bfb\u590d\u5236\u4ee3\u7801<code>full_schema = tool.get_input_schema()\n    for name, type_ in get_all_basemodel_annotations(full_schema).items():\n        # Check if the parameter name is \"runtime\" (regardless of type)\n        if name == \"runtime\":\n            return name\n        # Check if the type itself is ToolRuntime (direct usage)\n        if _is_injection(type_, ToolRuntime):\n            return name\n        # Check if ToolRuntime is in Annotated args\n        injections = [\n            type_arg\n            for type_arg in get_args(type_)\n            if _is_injection(type_arg, ToolRuntime)\n        ]\n        if len(injections) &gt; 1:\n            msg = (\n                \"A tool argument should not be annotated with ToolRuntime more than \"\n                f\"once. Received arg {name} with annotations {injections}.\"\n            )\n            raise ValueError(msg)\n        if len(injections) == 1:\n            return name\n<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e09\u70b9\uff0c\u5c31\u662f\u9ed8\u8ba4\u503c\u95ee\u9898\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u6211\u4eec\u4f7f\u7528\u4e86\u4e24\u4e2a\u9759\u6001\u6570\u636e\uff0cuser_name\u548cuser_id\uff0cuser_name\u5728node\u4e2d\u88ab\u4f5c\u4e3asystem_message\u4f20\u9012\u8fdb\u53bb\uff0c\u800cuser_id\u5219\u662f\u5728\u5de5\u5177\u4e2d\u4f7f\u7528\u3002 \u4f46\u662f\u4f60\u53ef\u4ee5\u53d1\u73b0\u7684\u662f\u6211\u4f20\u9012\u7684\u662fUser()\u6ca1\u6709\u4f20\u5165\u4efb\u4f55\u5177\u4f53\u7684\u540d\u5b57\u548cid\uff0c\u8fd9\u4e2a\u5176\u5b9e\u4e0d\u662fContext\u5e26\u6765\u7684\u597d\u5904\uff0c\u800c\u662f\u6570\u636e\u7c7b\u672c\u8eab\u7684\u597d\u5904\u3002 \u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/p3-xtjj-sign.byteimg.com\/tos-cn-i-73owjymdk6\/a109b7b27051493f8d045205bc7adfaa~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6ZOB5YawaWNl5ZGA:q75.awebp?rk3s=f64ab15b&amp;x-expires=1762441299&amp;x-signature=GabrQ0DdJmXQR5NwmLNpHx%2FT2%2BU%3D\" alt=\"image.png\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/p3-xtjj-sign.byteimg.com\/tos-cn-i-73owjymdk6\/524542bfc70b49eeada3e7366dd5cdad~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6ZOB5YawaWNl5ZGA:q75.awebp?rk3s=f64ab15b&amp;x-expires=1762441299&amp;x-signature=A3%2BDG8uxvvTi5fPslESe8vZiItQ%3D\" alt=\"image.png\"\/><\/figure>\n\n\n\n<p>\u5728langsmith studio\u4e2d\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/p3-xtjj-sign.byteimg.com\/tos-cn-i-73owjymdk6\/5e32f0b756074bf18f934285d136e857~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6ZOB5YawaWNl5ZGA:q75.awebp?rk3s=f64ab15b&amp;x-expires=1762441299&amp;x-signature=kzB9lGawyeL%2FvsvpWeFjPKtfsf0%3D\" alt=\"image.png\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/p3-xtjj-sign.byteimg.com\/tos-cn-i-73owjymdk6\/452e01248a604683a2df8db3aedde129~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6ZOB5YawaWNl5ZGA:q75.awebp?rk3s=f64ab15b&amp;x-expires=1762441299&amp;x-signature=0aI8Y8QrM8qpnsx%2F0WG6CI%2FnNcY%3D\" alt=\"image.png\"\/><\/figure>\n\n\n\n<p><br>\u6587\u7ae0\u94fe\u63a5\uff1ahttps:\/\/juejin.cn\/post\/7566825225259925547<br><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>langchain\u5df2\u4e8e10\u6708\u5e95\u53d1\u5e03\u4e86\u6b63\u5f0f\u7248\u7684v1\u7248\u672c\u3002\u672c\u6b21\u6211\u4eec\u4e3b\u8981\u8ba8&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":343,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-600","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tools"],"_links":{"self":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts\/600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":1,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":601,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts\/600\/revisions\/601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/media\/343"}],"wp:attachment":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}