ChatAnthropic v3 streamEvents() silently maps stop_reason: "refusal" to "stop", indistinguishable from a normal completion
@langchain/anthropic@1.5.10 (also confirmed present in 1.5.11, the latest stable). mapStopReason in src/utils/stream_events.ts (compiled: dist/utils/stream_events.cjs):
function mapStopReason(stopReason) {
switch (stopReason) {
case "end_turn":
case "stop_sequence": return "stop";
case "tool_use": return "tool_use";
case "max_tokens": return "length";
default: return "stop"; // "refusal" (and "pause_turn") land here
}
}Anthropic's Messages API returns stop_reason: "refusal" with empty content and stop_details: {type: "refusal", category: ..., explanation: "..."} when a model declines a request (reproduced directly against claude-opus-5/claude-opus-5-5 via the raw API — confirmed stop_reason: "refusal", content: [], usage.output_tokens: 0). Anything consuming the model through the legacy .stream()/_streamResponseChunks path sees this correctly (additional_kwargs.stop_reason: "refusal" survives AIMessageChunk.concat()).
But ChatAnthropic overrides _streamChatModelEvents (the v3 content-block protocol used by streamEvents({version: "v3"}), and by extension by LangGraph/deepagents' AgentNode) to call convertAnthropicStream, which runs every stop reason through mapStopReason above. A refusal is reported as message-finish with reason: "stop" — bit-for-bit identical to an ordinary successful end_turn completion. stop_details (the specific category + human-readable explanation) is never read anywhere in the conversion and is completely lost.
Confirmed by instrumenting wrapModelCall (a createMiddleware hook, so this is also what any agent middleware sees) and handleLLMEnd on a real createDeepAgent-built agent: for any completion delivered through the v3 path, additional_kwargs is always {} and response_metadata only ever contains the coarse finish_reason ("stop"/"tool_use"/"length") — there's no way to tell a refusal apart from a normal completion downstream of this conversion.
Impact: any application built on streamEvents()/v3 events (which is required for LangGraph agent frameworks like deepagents) cannot detect that a model refused a request — it looks exactly like a successful, empty-ish completion.
Suggested fix: add a refusal (and ideally pause_turn) case to mapStopReason, and propagate stop_details into the message-finish event's responseMetadata so consumers can at least inspect it via response_metadata.
Repro: any prompt/tool combination that triggers Anthropic's refusal behavior (model- and content-dependent; observed reliably via direct API calls, less consistently through a full agent loop) against claude-opus-5 or claude-opus-5-5 via streamEvents({version: "v3"}).
Source: langchain-ai/langchainjs