[GH-ISSUE #1435] [langchain]: The “after agent guardrails” example doesn’t work properly #206

Closed
opened 2026-02-17 17:19:24 -05:00 by yindo · 0 comments
Owner

Originally created by @mcavdar on GitHub (Nov 15, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/1435

Type of issue

issue / bug

Language

Python

Description

Hi, I'm running the “After Agent Guardrails” example from the LangChain documentation using the Class Syntax version.

When the judge LLM returns “UNSAFE”, the agent enters an infinite loop.
I believe the code needs an update, specifically in this section of the example.

        if "UNSAFE" in result.content:
            return {
                "messages": [{
                    "role": "assistant",
                    "content": "I cannot provide that response. Please rephrase your request."
                }],
                "jump_to": "end"
            }

ChatGPT says

Your after_agent middleware is triggering AGAIN AND AGAIN in a loop

This happens because:

after_agent runs every time the agent produces a message.

Your middleware adds a new assistant message when unsafe.

That added assistant message makes the graph think another agent turn is required.

The agent runs again → produces new message → middleware runs again → loops forever.

Originally created by @mcavdar on GitHub (Nov 15, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/1435 ### Type of issue issue / bug ### Language Python ### Description Hi, I'm running the “After Agent Guardrails” [example](https://docs.langchain.com/oss/python/langchain/guardrails#after-agent-guardrails) from the LangChain documentation using the Class Syntax version. When the judge LLM returns “UNSAFE”, the agent enters an **infinite loop**. I believe the code needs an update, specifically in this section of the example. ``` if "UNSAFE" in result.content: return { "messages": [{ "role": "assistant", "content": "I cannot provide that response. Please rephrase your request." }], "jump_to": "end" } ``` ChatGPT says > Your after_agent middleware is triggering AGAIN AND AGAIN in a loop > > This happens because: > > after_agent runs every time the agent produces a message. > > Your middleware adds a new assistant message when unsafe. > > That added assistant message makes the graph think another agent turn is required. > > The agent runs again → produces new message → middleware runs again → loops forever.
yindo added the langchain label 2026-02-17 17:19:24 -05:00
yindo closed this issue 2026-02-17 17:19:24 -05:00
yindo changed title from [langchain]: The “after agent guardrails” example doesn’t work properly to [GH-ISSUE #1435] [langchain]: The “after agent guardrails” example doesn’t work properly 2026-06-05 17:25:38 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#206