mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-30 11:49:38 +02:00
125 lines
3.4 KiB
Plaintext
125 lines
3.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f262985e-e973-4a27-9c9e-dbb3a06a35b7",
|
|
"metadata": {},
|
|
"source": [
|
|
"# How to define input/output schema for your graph\n",
|
|
"\n",
|
|
"By default, `StateGraph` takes in a single schema and all nodes are expected to communicate with that schema. However, it is also possible to define explicit input and output schemas for a graph. This is helpful if you want to draw a distinction between input and output keys.\n",
|
|
"\n",
|
|
"In this notebook we'll walk through an example of this. At a high level, in order to do this you simply have to pass in `input=..., output=...` when defining the graph. Let's see an example below!\n",
|
|
"\n",
|
|
"## Setup\n",
|
|
"\n",
|
|
"First, let's install the required packages"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "678286f2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%%capture --no-stderr\n",
|
|
"%pip install -U langgraph"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "16aad512",
|
|
"metadata": {},
|
|
"source": [
|
|
"<div class=\"admonition tip\">\n",
|
|
" <p class=\"admonition-title\">Set up <a href=\"https://smith.langchain.com\">LangSmith</a> for LangGraph development</p>\n",
|
|
" <p style=\"padding-top: 5px;\">\n",
|
|
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started <a href=\"https://docs.smith.langchain.com\">here</a>. \n",
|
|
" </p>\n",
|
|
"</div> "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "72689b3d",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Define and use the graph"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "6ec0eb77-874e-443e-8c73-93125b515106",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'answer': 'bye'}"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from langgraph.graph import StateGraph, START, END\n",
|
|
"from typing import TypedDict\n",
|
|
"\n",
|
|
"\n",
|
|
"class InputState(TypedDict):\n",
|
|
" question: str\n",
|
|
"\n",
|
|
"\n",
|
|
"class OutputState(TypedDict):\n",
|
|
" answer: str\n",
|
|
"\n",
|
|
"\n",
|
|
"def answer_node(state: InputState):\n",
|
|
" return {\"answer\": \"bye\"}\n",
|
|
"\n",
|
|
"\n",
|
|
"graph = StateGraph(input=InputState, output=OutputState)\n",
|
|
"graph.add_node(answer_node)\n",
|
|
"graph.add_edge(START, \"answer_node\")\n",
|
|
"graph.add_edge(\"answer_node\", END)\n",
|
|
"graph = graph.compile()\n",
|
|
"\n",
|
|
"graph.invoke({\"question\": \"hi\"})"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6a68836f-98e1-4684-a8a6-c1473c73460c",
|
|
"metadata": {},
|
|
"source": [
|
|
"Notice that the output of invoke only includes the output schema."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.1"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|