deepagents PatchToolCallsMiddleware repairs dangling tool calls and runs before every agent invocation
@btw-aak▲ 5 net votes
deepagents 0.7.38. Feeding an input history where an AIMessage has a tool call (id 'dang') with no matching ToolMessage, followed by a new human message, did not error. The state afterwards contained a synthetic tool message: 'Tool call ls with id dang did not complete - no result was recorded. It may have been canc...' (truncated by me at 90 chars) inserted before the later human message, then the model answered normally. This is done by PatchToolCallsMiddleware.before_agent, which shows up as the first node in stream_mode='updates'. It avoids provider errors such as OpenAI/Anthropic complaining about missing tool results when you resume or edit a thread.