[PR #6580] fix(prebuilt): respect Command(goto='__end__') from tools in create_react_agent #5140

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

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/6580

State: closed
Merged: No


Thank you for contributing to LangGraph! Follow these steps to mark your pull request as ready for review. If any of these steps are not completed, your PR will not be considered for review.

  • PR title: Follows the format: {TYPE}({SCOPE}): {DESCRIPTION}
    • Examples:
      • feat(core): add multi-tenant support
      • fix(cli): resolve flag parsing error
      • docs(openai): update API usage examples
    • Allowed {TYPE} values:
      • feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert, release
    • Allowed {SCOPE} values (optional):
      • langgraph, docs, cli, checkpoint, checkpoint-postgres, checkpoint-sqlite, prebuilt, scheduler-kafka, sdk-py
    • Once you've written the title, please delete this checklist item; do not include it in the PR.

PR Description:

Description:

When a tool in create_react_agent returns a Command(goto="__end__"), the agent graph was not halting as expected. Instead, execution continued and the model node was called again, causing infinite loops or invalid message histories.

The fix:

  • Always use conditional edges from the "tools" node (instead of static edges) to allow Command routing to be respected
  • Update route_tool_responses to detect when the last message is an AIMessage (indicating a tool used Command to add it directly), and return END to halt the graph

This ensures that tools can properly terminate the agent loop by returning Command(goto="__end__").

Issue: Fixes #6578

Dependencies: None

Twitter handle: (optional - add yours if you'd like a shoutout)

  • Add tests and docs: If you're adding a new integration, you must include:

    1. A test for the integration, preferably unit tests that do not rely on network access,
    2. An example notebook showing its use. It lives in docs/docs/integrations directory.
  • Lint and test: Run make format, make lint and make test from the root of the package(s) you've modified. We will not consider a PR unless these three are passing in CI. See contribution guidelines for more.

Additional guidelines:

  • Make sure optional dependencies are imported within a function.
  • Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests.
  • Most PRs should not touch more than one package.
  • Changes should be backwards compatible.
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6580 **State:** closed **Merged:** No --- Thank you for contributing to LangGraph! Follow these steps to mark your pull request as ready for review. **If any of these steps are not completed, your PR will not be considered for review.** - [x] **PR title**: Follows the format: {TYPE}({SCOPE}): {DESCRIPTION} - Examples: - feat(core): add multi-tenant support - fix(cli): resolve flag parsing error - docs(openai): update API usage examples - Allowed `{TYPE}` values: - feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert, release - Allowed `{SCOPE}` values (optional): - langgraph, docs, cli, checkpoint, checkpoint-postgres, checkpoint-sqlite, prebuilt, scheduler-kafka, sdk-py - Once you've written the title, please delete this checklist item; do not include it in the PR. **PR Description:** **Description:** When a tool in `create_react_agent` returns a `Command(goto="__end__")`, the agent graph was not halting as expected. Instead, execution continued and the model node was called again, causing infinite loops or invalid message histories. The fix: - Always use conditional edges from the "tools" node (instead of static edges) to allow `Command` routing to be respected - Update `route_tool_responses` to detect when the last message is an `AIMessage` (indicating a tool used `Command` to add it directly), and return `END` to halt the graph This ensures that tools can properly terminate the agent loop by returning `Command(goto="__end__")`. **Issue:** Fixes #6578 **Dependencies:** None **Twitter handle:** *(optional - add yours if you'd like a shoutout)* - [x] **Add tests and docs**: If you're adding a new integration, you must include: 1. A test for the integration, preferably unit tests that do not rely on network access, 2. An example notebook showing its use. It lives in `docs/docs/integrations` directory. - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. We will not consider a PR unless these three are passing in CI. See [contribution guidelines](https://github.com/langchain-ai/langgraph/blob/main/CONTRIBUTING.md) for more. Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to `pyproject.toml` files (even optional ones) unless they are **required** for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible.
yindo added the pull-request label 2026-02-20 17:51:17 -05:00
yindo closed this issue 2026-02-20 17:51:17 -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#5140