LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Browser
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
  • Tools
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Compat
  • Event
  • Llms
  • Openai Completions Stream
  • Profile
  • Stream
  • Structured Output
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Testing
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Gateway
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Standard Schema
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Uuid
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

Chat LangChain

What can I help with?

Enter to send·Shift+Enter new line

Menu

LangChain
BrowserUniversalHubNodeLoadSerializableEncoder BackedFile SystemIn MemoryTools
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsCompatEventLlmsOpenai Completions StreamProfileStreamStructured OutputLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryTestingToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingGatewayHashJson PatchJson SchemaMathSsrfStandard SchemaStreamTestingTiktokenTypesUuidVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScript@langchain/coreerrorsModelRefusalError
Class●Since v1.3

ModelRefusalError

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.

Copy
class ModelRefusalError

Bases

LangChainError<this>

Used in Docs

  • Models
  • Prebuilt middleware

Example

Copy
try {
  await model.withStructuredOutput(schema).invoke(input);
} catch (err) {
  if (ModelRefusalError.isInstance(err)) {
    console.warn("Model refused:", err.message, err.rawMessage);
  } else {
    throw err;
  }
}

Constructors

constructor
constructor→ ModelRefusalError

Properties

property
cause: unknown

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.

property
message: string

Human-readable error message.

property
name: "ModelRefusalError"
property
rawMessage: BaseMessage<MessageStructure<MessageToolSet>, MessageType>

The refused model response, if available.

property
stack: string
property
isInstance: IsInstanceFn
property
stackTraceLimit: number

Methods

method
captureStackTrace
method
fromRefusal→ ModelRefusalError

Creates a ModelRefusalError from a refusal content block.

The message includes the provider's refusal category, when present, and the refusal text.

method
prepareStackTrace

Inherited fromLangChainError

Properties

Pcause: Error
—

The underlying error that caused this ContextOverflowError, if any.

Pmessage: string
—

Human-readable error message.

Pname: stringPstackPisInstance: IsInstanceFnPstackTraceLimit

Methods

McaptureStackTraceMprepareStackTrace
View source on GitHub