[Bug]: Anthropic codec maps `stop_reason: "refusal"` to `FinishReason::Unknown` instead of `ContentFilter`
Affected area
Rust core runtime, Observability or exporters
Current behavior
map_anthropic_stop_reason in crates/codecs/src/anthropic.rs maps only end_turn, max_tokens and tool_use; everything else becomes FinishReason::Unknown. Anthropic's refusal therefore becomes Unknown("refusal"), although FinishReason::ContentFilter ("Content was filtered by safety systems") exists and the other codecs use it for safety stops:
| Codec | Safety stop | Normalized to |
|---|---|---|
| OpenAI Chat | content_filter |
ContentFilter |
| OpenAI Responses | incomplete_details.reason: content_filter |
ContentFilter |
| Gemini | policy and safety reasons such as SAFETY and PROHIBITED_CONTENT |
ContentFilter |
| OCI | content_filter |
ContentFilter |
| Anthropic | refusal |
Unknown("refusal") |
The OpenTelemetry GenAI and OpenInference exporters write "content_filter" for ContentFilter but pass Unknown values through, so an Anthropic refusal is reported as "refusal" and an OpenAI one as "content_filter". The raw stop_reason is still kept on the annotated response.
No test covers the Anthropic refusal mapping.
Expected behavior
Anthropic's refusal should normalize to FinishReason::ContentFilter, consistent with the other codecs' safety stops.
Steps to reproduce
- Add to
crates/codecs/tests/unit/anthropic_tests.rs:#[test] fn test_decode_response_finish_reason_refusal() { let codec = AnthropicMessagesCodec; let response = json!({ "content": [], "stop_reason": "refusal", "usage": { "input_tokens": 1, "output_tokens": 1 } }); let resp = codec.decode_response(&response).unwrap(); assert_eq!(resp.finish_reason, Some(FinishReason::ContentFilter)); } - Run
cargo nextest run -p nemo-relay-codecs -E 'test(finish_reason_refusal)'. - It fails:
left: Some(Unknown("refusal")) right: Some(ContentFilter)
Environment
- NeMo Relay version or commit:
mainat76843c17 - OS and architecture: macOS 27.2, Apple Silicon (arm64); platform-independent
- Rust version: 1.96.1 (pinned by
rust-toolchain.toml) - Python version: N/A
- Node.js version: N/A
- Go version: N/A
- Install/build command: from source; cargo-nextest 0.9.133
Impact
Consumers relying on the normalized finish_reason, including telemetry filters on content_filter, miss Anthropic refusals. Workaround: read the raw stop_reason.
Proposed fix: add "refusal" => FinishReason::ContentFilter to map_anthropic_stop_reason; with it, the test above and the rest of the codec suite pass (564 tests). If you'd prefer a dedicated variant for refusals, I'll follow that instead.
Related: stop_sequence also maps to Unknown, asserted by test_decode_response_finish_reason_stop_sequence, while the OCI codec maps Cohere's STOP_SEQUENCE to Complete. I've left it out unless you want it aligned.
I'd like to work on this and will open a PR once you're OK with the approach.
Hey @Sakshitapkir03 I'd be interested in working on this issue. The proposed normalization to FinishReason::ContentFilter makes sense, especially for maintaining consistency across provider codecs and telemetry exporters.
I noticed the suggested implementation is fairly scoped, so I can take care of the regression test and the mapping change.
Are you already working on this, or would it be okay if I picked it up and opened a PR?
Thanks for the interest, @jyotir07 ! I'm already working on this: the regression test is on my fork and the fix is verified locally. I'll open the PR once a maintainer confirms the approach.
Thanks @Sakshitapkir03 , please target release/0.11.
@greysonlalonde opened #1752 against release/0.11.