Handle new refusal
stop reason from Claude 4 models (#31217)
This PR adds support for handling the new [`refusal` stop reason](https://docs.anthropic.com/en/docs/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals) from Claude 4 models. <img width="409" alt="Screenshot 2025-05-22 at 4 31 56 PM" src="https://github.com/user-attachments/assets/707b04f5-5a52-4a19-95d9-cbd2be2dd86f" /> Release Notes: - Added handling for `"stop_reason": "refusal"` from Claude 4 models.
This commit is contained in:
parent
ad4645c59b
commit
5c0b161563
6 changed files with 18 additions and 0 deletions
|
@ -1348,6 +1348,7 @@ impl AgentDiff {
|
|||
ThreadEvent::NewRequest
|
||||
| ThreadEvent::Stopped(Ok(StopReason::EndTurn))
|
||||
| ThreadEvent::Stopped(Ok(StopReason::MaxTokens))
|
||||
| ThreadEvent::Stopped(Ok(StopReason::Refusal))
|
||||
| ThreadEvent::Stopped(Err(_))
|
||||
| ThreadEvent::ShowError(_)
|
||||
| ThreadEvent::CompletionCanceled => {
|
||||
|
|
|
@ -1693,6 +1693,16 @@ impl Thread {
|
|||
project.set_agent_location(None, cx);
|
||||
});
|
||||
}
|
||||
StopReason::Refusal => {
|
||||
thread.project.update(cx, |project, cx| {
|
||||
project.set_agent_location(None, cx);
|
||||
});
|
||||
|
||||
cx.emit (ThreadEvent::ShowError(ThreadError::Message {
|
||||
header: "Language model refusal".into(),
|
||||
message: "Model refused to generate content for safety reasons.".into(),
|
||||
}));
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
thread.project.update(cx, |project, cx| {
|
||||
|
|
|
@ -2204,6 +2204,7 @@ impl AssistantContext {
|
|||
StopReason::ToolUse => {}
|
||||
StopReason::EndTurn => {}
|
||||
StopReason::MaxTokens => {}
|
||||
StopReason::Refusal => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -231,6 +231,10 @@ impl ExampleContext {
|
|||
Ok(StopReason::MaxTokens) => {
|
||||
tx.try_send(Err(anyhow!("Exceeded maximum tokens"))).ok();
|
||||
}
|
||||
Ok(StopReason::Refusal) => {
|
||||
tx.try_send(Err(anyhow!("Model refused to generate content")))
|
||||
.ok();
|
||||
}
|
||||
Err(err) => {
|
||||
tx.try_send(Err(anyhow!(err.clone()))).ok();
|
||||
}
|
||||
|
|
|
@ -100,6 +100,7 @@ pub enum StopReason {
|
|||
EndTurn,
|
||||
MaxTokens,
|
||||
ToolUse,
|
||||
Refusal,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
|
@ -825,6 +825,7 @@ impl AnthropicEventMapper {
|
|||
"end_turn" => StopReason::EndTurn,
|
||||
"max_tokens" => StopReason::MaxTokens,
|
||||
"tool_use" => StopReason::ToolUse,
|
||||
"refusal" => StopReason::Refusal,
|
||||
_ => {
|
||||
log::error!("Unexpected anthropic stop_reason: {stop_reason}");
|
||||
StopReason::EndTurn
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue