[PR #468] [MERGED] Tool Validator Node #1492

Closed
opened 2026-02-20 17:44:52 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/langgraph/pull/468
Author: @hinthornw
Created: 5/15/2024
Status: Merged
Merged: 5/15/2024
Merged by: @hinthornw

Base: mainHead: wfh/tool_validator


📝 Commits (10+)

📊 Changes

7 files changed (+330 additions, -10 deletions)

View changed files

📝 Makefile (+1 -1)
📝 docs/docs/concepts/index.md (+1 -1)
📝 docs/docs/reference/graphs.md (+1 -1)
📝 docs/docs/reference/prebuilt.md (+16 -6)
📝 langgraph/prebuilt/__init__.py (+2 -0)
langgraph/prebuilt/tool_validator.py (+235 -0)
📝 tests/test_prebuilt.py (+74 -1)

📄 Description

Basically, for generating validated structured output in a conversational setting.

Example:

from typing import Literal

from langchain_anthropic import ChatAnthropic
from langchain_core.pydantic_v1 import BaseModel, validator

from langgraph.graph import END, START, MessageGraph
from langgraph.prebuilt import ValidationNode


class SelectNumber(BaseModel):
    a: int

    @validator("a")
    def a_must_be_meaningful(cls, v):
        if v != 37:
            raise ValueError("Only 37 is allowed")
        return v


builder = MessageGraph()
llm = ChatAnthropic(model="claude-3-haiku-20240307").bind_tools([SelectNumber])
builder.add_node("model", llm)
builder.add_node("validation", ValidationNode([SelectNumber]))
builder.add_edge(START, "model")


def should_validate(state: list) -> Literal["validation", "__end__"]:
    if state[-1].tool_calls:
        return "validation"
    return END


builder.add_conditional_edges("model", should_validate)


def should_reprompt(state: list) -> Literal["model", "__end__"]:
    for msg in state[::-1]:
        # None of the tool calls were errors
        if msg.type == "ai":
            return END
        if msg.additional_kwargs.get("is_error"):
            return "model"
    return END


builder.add_conditional_edges("validation", should_reprompt)


def get_ai_message(state: list):
    for msg in state[::-1]:
        if msg.type == "ai":
            return msg
    raise ValueError("No AI message found")


graph = builder.compile()
res = graph.invoke(("user", "Select a number, any number"))
res[-2].pretty_print()

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/langgraph/pull/468 **Author:** [@hinthornw](https://github.com/hinthornw) **Created:** 5/15/2024 **Status:** ✅ Merged **Merged:** 5/15/2024 **Merged by:** [@hinthornw](https://github.com/hinthornw) **Base:** `main` ← **Head:** `wfh/tool_validator` --- ### 📝 Commits (10+) - [`dbfd58a`](https://github.com/langchain-ai/langgraph/commit/dbfd58a45bea621d34779fca996b1e8214fb8d9c) ohoho - [`89968ea`](https://github.com/langchain-ai/langgraph/commit/89968ea292c045c4e5cbb626aaf3df5b53975d45) Merge branch 'main' into wfh/tool_validator - [`2f73955`](https://github.com/langchain-ai/langgraph/commit/2f7395547d3abf42cac7e849028b217e0a8e99f4) Tool Validator - [`7c6fc50`](https://github.com/langchain-ai/langgraph/commit/7c6fc50da45cac737bbb8d973d5dfd881eb2c102) Fmt - [`b5fcc7a`](https://github.com/langchain-ai/langgraph/commit/b5fcc7a149d7acefe5285b86f23882a2bdb65540) admonish the world - [`872ca8e`](https://github.com/langchain-ai/langgraph/commit/872ca8ef8d21872343212b48a15d4ffc04847f76) validate - [`23a5e81`](https://github.com/langchain-ai/langgraph/commit/23a5e8106be49373eae427810b6f311ae4566baa) fix - [`d32cf32`](https://github.com/langchain-ai/langgraph/commit/d32cf32d1a07355942c46ba3ef7ec772091687fa) link - [`fa200a8`](https://github.com/langchain-ai/langgraph/commit/fa200a8c6e76f042601b7ded6dd6db3c9bd73836) type - [`571a8f6`](https://github.com/langchain-ai/langgraph/commit/571a8f699d5e915c00361ad23a8038bb755ea8a1) spacing ### 📊 Changes **7 files changed** (+330 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `Makefile` (+1 -1) 📝 `docs/docs/concepts/index.md` (+1 -1) 📝 `docs/docs/reference/graphs.md` (+1 -1) 📝 `docs/docs/reference/prebuilt.md` (+16 -6) 📝 `langgraph/prebuilt/__init__.py` (+2 -0) ➕ `langgraph/prebuilt/tool_validator.py` (+235 -0) 📝 `tests/test_prebuilt.py` (+74 -1) </details> ### 📄 Description Basically, for generating validated structured output in a conversational setting. Example: ```python from typing import Literal from langchain_anthropic import ChatAnthropic from langchain_core.pydantic_v1 import BaseModel, validator from langgraph.graph import END, START, MessageGraph from langgraph.prebuilt import ValidationNode class SelectNumber(BaseModel): a: int @validator("a") def a_must_be_meaningful(cls, v): if v != 37: raise ValueError("Only 37 is allowed") return v builder = MessageGraph() llm = ChatAnthropic(model="claude-3-haiku-20240307").bind_tools([SelectNumber]) builder.add_node("model", llm) builder.add_node("validation", ValidationNode([SelectNumber])) builder.add_edge(START, "model") def should_validate(state: list) -> Literal["validation", "__end__"]: if state[-1].tool_calls: return "validation" return END builder.add_conditional_edges("model", should_validate) def should_reprompt(state: list) -> Literal["model", "__end__"]: for msg in state[::-1]: # None of the tool calls were errors if msg.type == "ai": return END if msg.additional_kwargs.get("is_error"): return "model" return END builder.add_conditional_edges("validation", should_reprompt) def get_ai_message(state: list): for msg in state[::-1]: if msg.type == "ai": return msg raise ValueError("No AI message found") graph = builder.compile() res = graph.invoke(("user", "Select a number, any number")) res[-2].pretty_print() ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-20 17:44:52 -05:00
yindo closed this issue 2026-02-20 17:44:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#1492