Error class representing a model refusal.
Providers report a refusal, such as a safety-policy refusal, as a successful response rather than an error. LangChain raises this error where a refusal cannot be treated as an answer, for example when structured output has nothing to parse, or when an agent uses refusal middleware.
The ModelRefusalError extends the LangChainError base class with
the marker "model-refusal". Retrying the same request is unlikely to succeed,
so the error is marked as not retryable.
class ModelRefusalErrorLangChainError<this>try {
await model.withStructuredOutput(schema).invoke(input);
} catch (err) {
if (ModelRefusalError.isInstance(err)) {
console.warn("Model refused:", err.message, err.rawMessage);
} else {
throw err;
}
}The underlying error that caused this ContextOverflowError, if any.
This property is optionally set when wrapping a lower-level error using ContextOverflowError.fromError. It allows error handlers to access or inspect the original error that led to the context overflow.
Human-readable error message.
The refused model response, if available.