Skip to content

feat: Add Multi-Agent Self-Correcting Workflow sample using LangGraph#2980

Open
geetaarora-google wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
geetaarora-google:feature/langgraph-sample
Open

feat: Add Multi-Agent Self-Correcting Workflow sample using LangGraph#2980
geetaarora-google wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
geetaarora-google:feature/langgraph-sample

Conversation

@geetaarora-google

@geetaarora-google geetaarora-google commented Jul 16, 2026

Copy link
Copy Markdown

Overview This PR introduces a new sample notebook demonstrating how to build a multi-agent, self-correcting workflow using langgraph, langchain-google-genai, and the Gemini Flash Latest model.

The fundamental problem with basic AI agents is that asking a single LLM to read a document, research the facts, rewrite the content, and review its own work often leads to hallucinations or skipped instructions. This sample solves that by breaking the workflow into strict, specialized personas that pass a shared state (TypedDict) between them.

Key Additions:
langgraph_multi-agent-rag-and-self-correction.ipynb: A comprehensive, step-by-step Jupyter notebook covering:
Specialized Agent Nodes: Defining strict personas (Researcher, Drafter, Editor, and Reviewer) to handle discrete tasks.
Deterministic Routing: Implementation of a reviewer_router that evaluates a boolean output from the Reviewer agent to determine if the workflow should proceed to the end or route back to the Drafter for self-correction.
State Management: Utilizing LangGraph's StateGraph and TypedDict to act as a shared whiteboard between agents.
Tool Binding: Initializing native search tools bound to Gemini Flash Latest.
multi-agent_auto-correcting_flowchart.png: An architectural diagram visualizing the LangGraph node routing and self-correction loop.

Changes/Fixes:

  • Replaced !pip with %pip for improved Jupyter Notebook compatibility during package installation.
  • Corrected minor spelling errors throughout the markdown documentation.

Motivation To provide developers with a robust, production-like pattern for chaining multiple Gemini calls into a cyclic, self-correcting graph rather than relying on a single zero-shot prompt.

Companion Medium Post: https://medium.com/@geetaarora_9514/if-youve-spent-any-time-building-with-llms-you-ve-likely-run-into-a-frustrating-wall-ask-a-39895adc8736

@geetaarora-google
geetaarora-google requested a review from a team as a code owner July 16, 2026 02:06

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We generally try to use raw mermaid diagrams for flowcharts instead of images. They're more portable and use less storage. I've added a suggestion for putting this inline in the notebook.

},
"outputs": [],
"source": [
"# Copyright 2024 Google LLC\n",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"# Copyright 2024 Google LLC\n",
"# Copyright 2026 Google LLC\n",

"id": "34aeaf2d"
},
"source": [
" Author(s) : [Geeta Arora](https://github.com/geetaarora-google) "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the notebook structure from the notebook template. Put the author below the title and links

https://github.com/GoogleCloudPlatform/generative-ai/blob/main/notebook_template.ipynb

"3. **Drafting:** The `Editor` agent receives the original document *and* the Ground Truth notes, rewriting the document strictly using the verified facts.\n",
"4. **Self-Correction:** The `Reviewer` acts as a harsh critic. If it spots a hallucination, it rejects the draft and **forces the workflow back to the Editor** until the draft is perfect.\n",
"\n",
"<img src=\"images/multi-agent_auto-correcting_flowchart.png\" width=\"200\" alt=\"Flowchart\">\n",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an in-line mermaid diagram instead of an embedded image

Example

flowchart LR
    %% Define the shapes
    Raw[Raw Document]
    Extractor(Extractor Node)
    Researcher(Researcher Node)
    Editor(Editor Node)
    Reviewer{Reviewer Node}
    END((END))

    %% Define the transitions and labels
    Raw --> Extractor
    Extractor --> Researcher
    Researcher -->|Tool: query_internal_wiki| Editor
    Editor --> Reviewer
    Reviewer -->|Approved: False| Editor
    Reviewer -->|Approved: True| END
Loading

Comment on lines +158 to +173
"Configure your Gemini API key. If you are running this in Colab, you can use the built-in secrets manager, or input it manually below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3de45f39"
},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"\n",
"if \"GOOGLE_API_KEY\" not in os.environ:\n",
" os.environ[\"GOOGLE_API_KEY\"] = getpass.getpass(\"Enter your Google Gemini API Key: \")"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repository is specifically for Gemini on Agent Platform. Please change this to use the Enterprise version. See auth setup in the notebook template.

https://github.com/GoogleCloudPlatform/generative-ai/blob/main/notebook_template.ipynb

Also set these environment variables

export GOOGLE_GENAI_USE_ENTERPRISE=true
export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='global'

"from langchain_google_genai import ChatGoogleGenerativeAI\n",
"from langchain_core.tools import tool\n",
"\n",
"# Initialize Gemini 1.5 Flash (using latest to ensure highest compatibility)\n",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"# Initialize Gemini 1.5 Flash (using latest to ensure highest compatibility)\n",
"# Initialize Gemini 3.5 Flash\n",

"from langchain_core.tools import tool\n",
"\n",
"# Initialize Gemini 1.5 Flash (using latest to ensure highest compatibility)\n",
"llm = ChatGoogleGenerativeAI(model=\"gemini-flash-latest\", temperature=0)\n",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"llm = ChatGoogleGenerativeAI(model=\"gemini-flash-latest\", temperature=0)\n",
"llm = ChatGoogleGenerativeAI(model=\"gemini-3.5-flash\")\n",

As far as I'm aware, latest doesn't work on Agent Platform. Updating to newest flash model.

Comment on lines +283 to +293
"def extractor_node(state: AgentState):\n",
" print(\"▶ [Extractor] Finding claims...\")\n",
" prompt = f\"Extract fact-checkable claims from this text. Output ONLY a valid JSON array of strings. Do not output any markdown formatting. Text: {state['original_document']}\"\n",
" response = llm.invoke([SystemMessage(content=\"You are the Extractor.\"), HumanMessage(content=prompt)])\n",
" content_str = _get_text(response).replace(\"```json\", \"\").replace(\"```\", \"\").strip()\n",
" try:\n",
" claims = json.loads(content_str)\n",
" if not isinstance(claims, list): claims = [str(claims)]\n",
" except:\n",
" claims = [content_str]\n",
" return {\"extracted_claims\": claims}\n",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants