- LangGraph’s execution model and how a typed state flows through a node.
- How nodes return partial updates that are merged into a global state.
- How to wire a real model call cleanly and composably.
Ensure you have an OpenAI API key and the required packages installed. The examples below use the modern OpenAI Responses API via the OpenAI Python client and LangGraph.
Quick setup
Install the dependencies (uncomment if running in a fresh environment):OPENAI_API_KEY— requiredOPENAI_MODEL— optional (defaults togpt-4in the examples below)
OPENAI_API_KEY in your shell or let the script prompt for it at runtime.
Protect your
OPENAI_API_KEY. Using LLMs incurs cost — monitor usage and model selection (OPENAI_MODEL) to control billing.Initialize the client and constants
The example below creates an OpenAI client (readingOPENAI_API_KEY from the environment) and sets a default MODEL. The helper will prompt for the key if it is not already set.
Define a typed state
LangGraph operates on structured, typed states instead of raw strings. Define the state shape withTypedDict. This example uses a two-field state: question (input) and answer (output).
Node: call the Responses API
Each node receives the current typed state and returns a partial update (a dictionary with only the fields it produced). Here is a single node,answer_question, which calls the OpenAI Responses API using the question and returns only the answer field.
Build the StateGraph and set the entry point
Register the node in aStateGraph, set the entry point, then compile the graph into a callable app.
Invoke the graph
Invoke the compiled graph with an initial state that provides thequestion. LangGraph runs the entry node, merges the returned partial update, and returns the final state that includes both the original input and the generated answer.
Why this pattern matters
- Typed state: makes data flow explicit and type-safe across nodes.
- Nodes return partial updates: enables small, composable units of work that can be merged by the orchestrator.
- Graph defines orchestration: lets you add routing, tool-calling, validation, retries, or memory without changing node internals.
Next steps and extensions
From here you can extend the basic graph with:- Branching and routing (conditional node execution)
- Tool integration (external APIs, search, databases)
- Retries and error handling
- Observability and metrics for each node
- Persistent memory for multi-step dialogs
Links and references
- LangGraph: https://github.com/langgraph/langgraph
- OpenAI Responses API: https://platform.openai.com/docs/api-reference/responses
- OpenAI Python client: https://github.com/openai/openai-python