Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safeguards for infinite loops and unresolvable tasks #28441

Closed
wants to merge 33 commits into from

Conversation

Matthewi1012
Copy link

Description

In the issue referenced below, the agent generates it's own "User Input" prompts during execution. Rather than using provided tools it requests input data from the user whick it cannot receive. This results in Inifinite loops, ultimately reaching the iteration limit, and provides the following error message. "Agent stopped due to iteration limit". In this PR we provided a proposed solution with associated unit and integration tests, for the specific instances of infinite loops from user input.

Issue: #25696

Dependencies:
No additional dependencies

Twitter Handle:
None

Ahonanhin added 4 commits November 27, 2024 17:05
	modified:   libs/core/langchain_core/prompts/prompt.py
	modified:   libs/langchain/langchain/agents/react/agent.py

- Updated the prompt template to include explicit termination instructions:
  1. Avoid generating its own "User Input."
  2. Gracefully terminate when progress cannot be made.
  3. Use a fallback message for incomplete tasks.

- Enhanced the output parser:
  - Detects repetitive reasoning and circular logic.
  - Handles invalid outputs and iteration limits.
  - Returns appropriate error messages for unresolvable scenarios.

- Configured the AgentExecutor:
  - Set a maximum iteration limit to prevent infinite loops.
  - Ensures the agent stops reasoning after reaching the limit.
…ct agent

- Updated `output_parsers/base.py` to detect and handle repetitive reasoning.
- Enhanced `prompts/prompt.py` with constraints to avoid infinite loops and user input generation.
- Improved `agents/react/agent.py` with logic for graceful termination and fallback handling.
	deleted:    libs/community/langchain_community/tools/fallback.py
	new file:   libs/core/tests/integration_tests/test_react_agent.py
	new file:   libs/core/tests/unit_tests/output_parsers/test_base.py
	new file:   libs/core/tests/unit_tests/output_parsers/test_prompt.py
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Dec 2, 2024
Copy link

vercel bot commented Dec 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 4, 2024 0:00am

@dosubot dosubot bot added Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Dec 2, 2024
	modified:   libs/core/langchain_core/prompts/prompt.py
	modified:   libs/core/tests/unit_tests/output_parsers/test_prompt.py
Ahonanhin added 2 commits December 3, 2024 18:14
	modified:   libs/core/langchain_core/output_parsers/base.py
	modified:   libs/core/langchain_core/prompts/prompt.py
Ahonanhin added 4 commits December 3, 2024 18:34
	modified:   libs/core/langchain_core/agents.py
	modified:   libs/core/langchain_core/output_parsers/base.py
	modified:   libs/core/langchain_core/prompts/prompt.py
	modified:   libs/core/langchain_core/agents.py
	modified:   libs/core/langchain_core/output_parsers/base.py
	modified:   libs/core/langchain_core/prompts/prompt.py
@efriis
Copy link
Member

efriis commented Dec 10, 2024

hey there! I believe this would be better implemented by the user in a langgraph agent: https://langchain-ai.github.io/langgraph/tutorials/introduction/

This out of the box AgentExecutor does have shortcomings, hence the greater degree of control in langgraph. I don't think this change to AgentExecutor and supporting data objects is desirable, so I will close this PR

@efriis efriis closed this Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants