---
title: 'Basic Chatbot in LangGraph'
url: 'https://mcavdar.com/blog/basic-chatbot-in-langgraph'
markdown: 'https://mcavdar.com/blog/basic-chatbot-in-langgraph.md'
date: '2026-07-28'
description: 'Requirements: pip install langgraph langchain_ollama Here is the complete code for building a chatbot that uses Ollama as its provider: from typing import Annotated from typing_extensions import TypedDict from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_me…'
taxonomy:
  tag:
    - ollama
    - langgraph
    - chatbot
    - llm
    - python
    - langchain
---

## [Basic Chatbot in LangGraph](https://mcavdar.com/blog/basic-chatbot-in-langgraph)

    28th Jul 2026   [ollama](https://mcavdar.com/tag:ollama#body-wrapper) [langgraph](https://mcavdar.com/tag:langgraph#body-wrapper) [chatbot](https://mcavdar.com/tag:chatbot#body-wrapper) [llm](https://mcavdar.com/tag:llm#body-wrapper) [python](https://mcavdar.com/tag:python#body-wrapper) [langchain](https://mcavdar.com/tag:langchain#body-wrapper)  

Requirements:

```bash
pip install langgraph langchain_ollama 
```

Here is the complete code for building a chatbot that uses Ollama as its provider:

```python
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_ollama import ChatOllama

class State(TypedDict):
    # Messages have the type "list". The `add_messages` function
    # in the annotation defines how this state key should be updated
    # (in this case, it appends messages to the list, rather than overwriting them)
    messages: Annotated[list, add_messages]

graph_builder = StateGraph(State)
llm = ChatOllama(model="deepseek-r1")

def chatbot(state: State):
    return {"messages": [llm.invoke(state["messages"])]}

# The first argument is the unique node name
# The second argument is the function or object that will be called whenever
# the node is used.
graph_builder.add_node("chatbot", chatbot)

graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile()

def stream_graph_updates(user_input: str):
    for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}):
        for value in event.values():
            print("Assistant:", value["messages"][-1].content)

while True:
    try:
        user_input = input("User: ")
        if user_input.lower() in ["quit", "exit", "q"]:
            print("Goodbye!")
            break
        stream_graph_updates(user_input)
    except:
        # fallback if input() is not available
        user_input = "What do you know about LangGraph?"
        print("User: " + user_input)
        stream_graph_updates(user_input)
        break

```

Reference: [Langgraph](https://langchain-ai.github.io/langgraph/tutorials/introduction/#part-1-build-a-basic-chatbot)

 [ Previous Post](https://mcavdar.com/blog/bookmarks) [Next Post ](https://mcavdar.com/blog/automate-ubuntu-installation)

#### Syndicate

 [ Atom 1.0](https://mcavdar.com/blog.atom) [ RSS](https://mcavdar.com/blog.rss)

---

## Navigation

- Parent: [Blog](https://mcavdar.com/index.md)
- Previous: [Automate Ubuntu Installation](https://mcavdar.com/blog/automate-ubuntu-installation.md)
- Next: [Bookmarks](https://mcavdar.com/blog/bookmarks.md)
