{ "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!" ] }, { "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", "class InputState(TypedDict):\n", " question: str\n", "\n", "class OutputState(TypedDict):\n", " answer: str\n", "\n", "def answer_node(state: InputState):\n", " return {\"answer\": \"bye\"}\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." ] }, { "cell_type": "code", "execution_count": null, "id": "b952a554-f2a4-4be3-81ab-2e08f0f441c2", "metadata": {}, "outputs": [], "source": [] } ], "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 }