fix: handle Anthropic 'refusal' stop reason instead of retry-looping#3640
Open
jeffbryner wants to merge 2 commits into
Open
fix: handle Anthropic 'refusal' stop reason instead of retry-looping#3640jeffbryner wants to merge 2 commits into
jeffbryner wants to merge 2 commits into
Conversation
…ing the event Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3624
Problem
When Claude Fable's safety classifiers decline a request, the Anthropic API returns HTTP 200 with
stop_reason: "refusal"in themessage_deltaSSE event. Forge'sStopReasonenum didn't know that variant, so:message_deltaevent (itsstop_reasonfield is required),EventData::Unknowncatch-all and was silently converted to an empty assistant message — the finish reason was lost,EmptyCompletionerror,repeated until max retry attempts were exhausted.
Fix
forge_app(Anthropic DTO): addStopReason::Refusal, mapped to the existingFinishReason::ContentFilter(same mapping Bedrock uses forContentFilteredand Google forSAFETY).forge_domain(stream aggregator): when the finish reason isContentFilterwith no tool calls, return a new non-retryableError::Refusalwith an actionable message ("rephrase the request or switch to a different model") instead of entering the retry loop.Intentional behavior change beyond the reported bug
Because the check lives in the provider-agnostic aggregator, content-filter finishes from Google (
SAFETY/RECITATION), Bedrock (ContentFiltered), and OpenAI-compatible (content_filter) providers now also end the turn with the clear error. Previously they re-entered the orchestrator loop (the turn only completes onStop), appending an empty assistant message and re-requesting. In the mid-stream case the partial output (already streamed to the UI) is no longer persisted to conversation context — per Anthropic's guidance, partial output before a refusal should be discarded. Happy to gate this to empty completions only if maintainers prefer the narrower change.Out of scope / possible follow-ups
pause_turnandmodel_context_window_exceededstop reasons are also missing from the enum and currently swallowed the same way;pause_turnneeds orchestrator resume semantics, so it was deliberately excluded here.#[serde(other)]catch-all onStopReasonwould prevent any future unknown stop reason from being silently dropped, at the cost of choosing a default mapping.Testing
stop_reason: "refusal"(asserts it parses as a known event, notEventData::Unknown), mapping test, non-retryable error downcast test, and a mid-stream refusal-after-partial-content test.cargo test -p forge_app -p forge_domain -p forge_repogreen (322 passed in forge_app),cargo clippy -D warningsclean,cargo fmtapplied.🤖 Generated with Claude Code