Human in the Loop with Spring ... Note
Spring

Human in the Loop with Spring AI 2.1: Tool Calls That Survive a Restart

Spring AI 2.1 introduces tool call confirmation, allowing specific tools to require human approval before execution. This feature addresses the challenge of long wait times for approvals by pausing the automation loop and marking tools that need confirmation. Developers can mark tools using an annotation, properties for external tools, or a ToolCallConfirmationPolicy bean for argument-dependent rules. When a marked tool is requested, the system returns a ChatResponse indicating the calls awaiting decision, without executing them.The crucial aspect is that Spring AI does not maintain state during the pause; the prompt and the paused reply contain all necessary information for continuation. The new community project, Spring AI Session Extensions, complements this by managing the pause within the session log. It records the session's turn status, marking it as "parked" when awaiting confirmation and storing the question.To use Session Extensions, developers integrate a TurnAdvisor into their ChatClient configuration, which extends Spring AI's Session API. When a user requests a marked tool, the TurnAdvisor parks the session and logs the confirmation question. A separate process can then retrieve the session ID, access the pending question from the log, and present it for human approval.Upon receiving a decision (allow or deny), the approver's input is passed back to the system via the TurnStatusAdvisor. This advisor then executes allowed calls, records results in the session log, and allows the model to complete the turn. Denied calls are recorded with the approver's message, which the model can then use to inform the user.Key aspects include ensuring that confirmation questions are retrieved from secure storage, verifying approver permissions, and handling idempotency for tool executions. The demo illustrates this flow, showing how a mortgage payment collection tool pauses the process for approval, with the session status and question persisted in the log across JVM instances. This architecture ensures that sensitive operations are human-approved while maintaining a coherent conversation flow.