Skip to content

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
Kcstring:fix/langchain-stale-structured-response
Closed

fix(langchain): clear stale structured_response from checkpoint on each new turn#36965
Kai Jiang (Kcstring) wants to merge 1 commit intolangchain-ai:masterfrom
Kcstring:fix/langchain-stale-structured-response

Conversation

@Kcstring
Copy link
Copy Markdown

Closes #36957


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.

Root cause: _build_commands only wrote structured_response to 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_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.

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 value
  • test_second_turn_with_validation_error_retry: verifies that a validation failure in turn 2 triggers a retry rather than returning the stale turn-1 value

This contribution was developed with AI-agent assistance.

…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.
@github-actions github-actions Bot added fix For PRs that implement a fix langchain `langchain` package issues & PRs size: S 50-199 LOC labels Apr 23, 2026
@github-actions
Copy link
Copy Markdown

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:

  1. Comment on the linked issue to request assignment from a maintainer
  2. Once assigned, your PR will be reopened automatically

Maintainers: reopen this PR or remove the missing-issue-link label to bypass this check.

@github-actions github-actions Bot closed this Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external fix For PRs that implement a fix langchain `langchain` package issues & PRs missing-issue-link new-contributor size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create_agent: stale structured_response from checkpoint causes premature exit on next turn

1 participant