Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

LangChain

This project is a hands-on LangGraph learning repo. It contains small Python examples that build from basic graph concepts into memory, human-in-the-loop workflows, tool calling, and multi-agent systems. The later examples show practical patterns such as reducers, message state, summarization, checkpointing, dynamic parallelism, subgraphs, and routing work across multiple agents.

Quick Start

quick_start.py is a self-contained introductory example. It builds a minimal LangGraph agent backed by Claude that can add, multiply, and divide numbers using tool calls. Run it from the repository root:

python quick_start.py

RAG

The RAG/ folder contains the part 6 LangGraph + LlamaIndex example. It builds a retrieval-augmented agent that reads source documents from RAG/data/, stores a local LlamaIndex vector index in RAG/storage/, and exposes retrieval through a LangGraph tool.

Setup from the repository root:

pip install -r requirement.txt

Add your OpenAI key to RAG/.env:

OPENAI_API_KEY=your_api_key_here

Run the interactive RAG agent from inside the RAG directory:

cd RAG
python main.py

See RAG/Readme.md for full instructions, including the smoke test and how to rebuild the persisted index after changing documents.

Solved Issues

part_4_shopbot.py: Orphan Tool Message After Summarization

  • Before: Summarization kept only the last two messages, which could split apart an assistant tool call and its tool response.

    messages_to_delete = [
        RemoveMessage(id=m.id) for m in state["messages"][:-2]
    ]
  • After: Summarization keeps the latest complete user turn, preserving tool-call message order.

    last_human_index = 0
    for i, message in enumerate(state["messages"]):
        if isinstance(message, HumanMessage):
            last_human_index = i
    
    messages_to_delete = [
        RemoveMessage(id=m.id) for m in state["messages"][:last_human_index]
    ]
  • Error: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.

  • Cause: summarize_node kept only the last two messages after summarization. In conversations that used tools, this could leave a ToolMessage in state without the preceding AIMessage that created the tool call.

  • Solution: Summarization now deletes messages before the latest user turn instead of keeping a fixed number of messages. This preserves valid message groups such as HumanMessage -> AIMessage(tool_calls) -> ToolMessage -> AIMessage.

part_5_5_multi_agents.py: Invalid LangGraph Branch Target

  • Before: The graph tried to route to initiate_research as if it were a registered node.

    def should_continue_to_research(state: ResearchGraphState):
        if state.get("human_analyst_feedback"):
            return "create_analysts"
        return "initiate_research"
    
    outer_builder.add_conditional_edges(
        "human_feedback",
        should_continue_to_research,
        {"create_analysts": "create_analysts", "initiate_research": "initiate_research"}
    )
  • After: The graph routes through route_after_human_feedback, which either regenerates analysts or sends work to the interview subgraphs.

    def route_after_human_feedback(state: ResearchGraphState):
        if state.get("human_analyst_feedback"):
            return "create_analysts"
        return initiate_research(state)
    
    outer_builder.add_conditional_edges(
        "human_feedback",
        route_after_human_feedback,
        ["create_analysts", "interview"]
    )
  • Error: At 'human_feedback' node, 'should_continue_to_research' branch found unknown target 'initiate_research'.

  • Cause: initiate_research was used as a branch target, but it is a routing function that returns Send(...) objects. It was not registered as a graph node.

  • Solution: human_feedback now routes through route_after_human_feedback. That router either loops back to create_analysts when feedback exists, or calls initiate_research(state) to launch parallel interview subgraphs.

part_5_5_multi_agents.py: Non-Interactive Input Crash

  • Before: The script always waited for keyboard input, even when stdin was unavailable.

    feedback = input("\nProvide feedback to refine analysts (or press Enter to approve): ")
  • After: The script asks for feedback only in an interactive terminal and auto-approves otherwise.

    if sys.stdin.isatty():
        feedback = input("\nProvide feedback to refine analysts (or press Enter to approve): ")
    else:
        feedback = ""
        print("\nNo interactive input available; approving generated analysts.")
  • Error: EOFError: EOF when reading a line.

  • Cause: The script always called input() for human analyst feedback. This fails when the file is run from a non-interactive command session.

  • Solution: The script now checks sys.stdin.isatty(). If interactive input is unavailable, it automatically approves the generated analysts and continues.

part_5_5_multi_agents.py: Tool Call Without Tool Response

  • Before: The analyst question node could produce tool calls, but those calls were not routed to ToolNode.

    response = interview_llm.invoke(messages)
  • After: Only the expert answer node can produce tool calls, and those calls are routed through ToolNode before the next model call.

    response = llm.invoke(messages)
  • Error: An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'.

  • Cause: generate_question() used the tool-bound interview model, so the analyst node could emit an AIMessage with tool_calls. The graph then routed that message directly to generate_answer() instead of interview_tools, so no matching ToolMessage was created.

  • Solution: generate_question() now uses the plain LLM. Tool calls are reserved for generate_answer(), whose route sends tool-calling messages through ToolNode before the model sees the conversation again.

Reference:

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages