fix(prebuilt): retry create_react_agent on MALFORMED_FUNCTION_CALL finish_reason#6986
Open
Saakshi Gupta (saakshigupta2002) wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
…nish_reason When using create_react_agent with Google Gemini, the agent would silently terminate without making any tool calls when the LLM returned finish_reason: MALFORMED_FUNCTION_CALL. The should_continue router only checked for tool_calls presence and routed to END when none were found, giving no indication of the failure. This change detects retryable finish_reason values (currently MALFORMED_FUNCTION_CALL) in the should_continue conditional edge and routes back to the agent node for a retry attempt. A warning is logged for observability. LangGraph's built-in recursion limit prevents infinite retry loops. Closes langchain-ai#6574
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description: When using
create_react_agentwith Google Gemini models, the agent silently terminates without making any tool calls when the LLM returnsfinish_reason: MALFORMED_FUNCTION_CALL. Theshould_continueconditional edge only checks fortool_callspresence on theAIMessage— when Gemini fails to produce valid JSON for a tool call, it returns anAIMessagewith notool_callsand setsfinish_reason: MALFORMED_FUNCTION_CALLinresponse_metadata. The router sees no tool calls and routes toEND, with no indication of the failure.This PR adds detection of retryable
finish_reasonvalues in theshould_continuerouter. When a retryable finish reason is detected (currentlyMALFORMED_FUNCTION_CALL), the agent logs a warning and routes back to the entrypoint node for a retry attempt. LangGraph's built-in recursion limit (default 25) prevents infinite retry loops.Key changes:
_RETRYABLE_FINISH_REASONSmodule-level constant inchat_agent_executor.py— an extensible set of finish reasons that trigger retryshould_continueto inspectresponse_metadata.finish_reasonwhen no tool calls are present; retryable reasons route back to the agent entrypointentrypointtoagent_pathsso the conditional edge accepts the retry targetFakeToolCallingModelin tests withresponse_metadata_listsupportpre_model_hookDesign rationale: retry (instead of raising) aligns with Google's own recommendation for handling
MALFORMED_FUNCTION_CALL, and is backwards compatible since the only behavioral change is that previously-broken flows now recover instead of silently failing.Closes #6574
Issue: #6574
Dependencies: None