ModelRefusalMiddleware()AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]Stream transformer factories registered by the middleware.
Logic to run before the agent execution starts.
Async logic to run before the agent execution starts.
Logic to run before the model is called.
Async logic to run before the model is called.
Logic to run after the model is called.
Async logic to run after the model is called.
Logic to run after the agent execution completes.
Async logic to run after the agent execution completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.
Raise ModelRefusalError for responses containing a standard refusal block.
Add this middleware to create_agent to treat a refusal as an error instead of
a successful final answer. Model calls without this middleware continue to
return messages, including refusal content.
List it after ModelFallbackMiddleware so the fallback can catch the error.
Streaming content may already have been emitted before the refusal is known.
This middleware does not infer refusals from text or retry blocked requests.