Files
langgraph/examples/input_output_schema.ipynb
T
db306cd01b doc updates (#1639)
---------

Co-authored-by: vbarda <vadym@langchain.dev>
2024-09-08 16:55:52 +00:00

3.4 KiB

How to define input/output schema for your graph

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.

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!

Setup

First, let's install the required packages

In [ ]:
%%capture --no-stderr
%pip install -U langgraph

Set up LangSmith for LangGraph development

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 here.

Define and use the graph

In [12]:
from langgraph.graph import StateGraph, START, END
from typing import TypedDict


class InputState(TypedDict):
    question: str


class OutputState(TypedDict):
    answer: str


def answer_node(state: InputState):
    return {"answer": "bye"}


graph = StateGraph(input=InputState, output=OutputState)
graph.add_node(answer_node)
graph.add_edge(START, "answer_node")
graph.add_edge("answer_node", END)
graph = graph.compile()

graph.invoke({"question": "hi"})
Out [12]:
{'answer': 'bye'}

Notice that the output of invoke only includes the output schema.