fix(langchain): clear stale structured_response from checkpoint on each new turn#36965
Closed
Kai Jiang (Kcstring) wants to merge 1 commit intolangchain-ai:masterfrom
Closed
Conversation
…n each new turn
When a checkpointer is used, \`structured_response\` written to state in
turn N is restored from the checkpoint at the start of turn N+1. The
routing edges in \`_make_model_to_tools_edge\` and
\`_make_model_to_model_edge\` tested \`"structured_response" in state\`,
which is truthy for the restored value even before the model runs for
the new turn. This caused the agent to exit immediately with the
previous turn's answer instead of invoking the model.
Fix: \`_build_commands\` now always writes \`structured_response\` to
state (including \`None\` when the model produced no structured output),
so the stale checkpoint value is overwritten at the end of every model
node execution. The routing edges are updated to test
\`state.get("structured_response") is not None\` instead of key
presence.
Closes langchain-ai#36957
This contribution was developed with AI-agent assistance.
|
This PR has been automatically closed because you are not assigned to the linked issue. External contributors must be assigned to an issue before opening a PR for it. Please:
Maintainers: reopen this PR or remove the |
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.
Closes #36957
When a checkpointer is used,
structured_responsewritten to state in turn N is restored from the checkpoint at the start of turn N+1. The routing edges in_make_model_to_tools_edgeand_make_model_to_model_edgetested"structured_response" in state, which is truthy for the restored value even before the model runs for the new turn. This caused the agent to exit immediately with the previous turn's answer instead of invoking the model.Root cause:
_build_commandsonly wrotestructured_responseto state when it was non-None. On the next turn, the stale value from the checkpoint was still present in state, and the routing edges treated it as a valid exit condition.Fix:
_build_commandsnow always writesstructured_responseto state (includingNonewhen the model produced no structured output), so the stale checkpoint value is overwritten at the end of every model node execution. The routing edges are updated to teststate.get("structured_response") is not Noneinstead of key presence.Two regression tests are added to
TestCheckpointStaleStructuredResponse:test_second_turn_not_short_circuited_by_stale_checkpoint: verifies that turn 2 produces a fresh response, not the stale turn-1 valuetest_second_turn_with_validation_error_retry: verifies that a validation failure in turn 2 triggers a retry rather than returning the stale turn-1 valueThis contribution was developed with AI-agent assistance.