Opt-in error handling for explicit model refusals.
Base middleware class for an agent.
Subclass this and implement any of the defined methods to customize agent behavior between steps in the main agent loop.
State schema for the agent.
Model request information for the agent.
Response from model execution including messages and optional structured output.
The result will usually contain a single AIMessage, but may include an additional
ToolMessage if the model used a tool for structured output.
Raised when structured output tool call arguments fail to parse according to the schema.
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.