Skip to content

fix(anthropic): preserve native refusal metadata through streaming aggregation and non-streaming responses (#43752) - #43760

Closed
shotsan wants to merge 1 commit into
BerriAI:mainfrom
shotsan:feat/preserve-anthropic-refusal-metadata
Closed

shotsan wants to merge 1 commit into
BerriAI:mainfrom
shotsan:feat/preserve-anthropic-refusal-metadata

Conversation

@shotsan

@shotsan shotsan commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Closes #43752.

Summary

When Anthropic models refuse a request (e.g. content policy / safety triggers like cyber attacks or hate speech), Anthropic returns:

  • stop_reason: "refusal"
  • stop_details: {"type": "refusal", "category": "cyber", ...}

LiteLLM maps finish_reason to "content_filter" for OpenAI schema compatibility, but previously dropped stop_reason and stop_details. Consequently:

  1. In streaming mode (ModelResponseIterator), message_delta events did not populate delta.provider_specific_fields with refusal metadata.
  2. In streaming aggregation (stream_chunk_builder), combined_provider_fields was attached to _choice.message.provider_specific_fields, but was not propagated to _choice.provider_specific_fields or response._hidden_params["provider_specific_fields"].
  3. In non-streaming mode (AnthropicConfig._build_provider_specific_fields), stop_reason and stop_details were omitted when present.
  4. In pass-through streaming iterator (agentic_streaming_iterator.py), stop_details in delta was omitted from the response dictionary.

Changes

  • litellm/llms/anthropic/chat/handler.py: In chunk_parser(), check message_delta for stop_reason == "refusal" or stop_details is not None and attach them to provider_specific_fields.
  • litellm/llms/anthropic/chat/transformation.py: In _build_provider_specific_fields(), check completion_response for stop_reason == "refusal" or stop_details is not None and attach them to provider_specific_fields. Also set model_response.choices[0].provider_specific_fields = provider_specific_fields.
  • litellm/main.py: In stream_chunk_builder(), propagate combined_provider_fields to _choice.provider_specific_fields and update response._hidden_params["provider_specific_fields"].
  • litellm/llms/anthropic/experimental_pass_through/messages/agentic_streaming_iterator.py: In _handle_message_delta(), capture stop_details if present in delta.
  • tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py: Added automated regression tests for:
    • Streaming refusal metadata preservation through stream_chunk_builder
    • Non-streaming refusal metadata preservation
    • Normal completion verification ensuring no false-positive refusal fields are injected

@shotsan
shotsan requested a review from a team September 29, 2026 19:44
@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Adds refusal metadata preservation to Anthropic response handling.

The PR appears safe to merge; a focused agentic pass-through regression test would improve coverage.

Findings

  1. P2 Agentic refusal path lacks coverage ▶

Summary

Preserves Anthropic refusal stop_reason and stop_details across streaming chunks, assembled responses, non-streaming responses, and agentic pass-through reconstruction.

  • Adds refusal and normal-completion regression tests for the chat-handler paths.
  • The agentic pass-through change still needs a focused regression test.

Reviews (1) · Last reviewed commit: "fix(anthropic): preserve native refusal ..."

Comment on lines +154 to +155
if "stop_details" in delta:
response["stop_details"] = delta["stop_details"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Agentic refusal path lacks coverage

The new chat-handler tests cover refusal metadata, but no test checks that rebuilding an agentic pass-through message_delta retains stop_details. The existing agentic delta test supplies only a stop reason. Without a focused test, a future change could silently drop refusal details from the response passed to agentic hooks.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

…etails) through streaming aggregation and non-streaming responses (BerriAI#43752)
@shotsan
shotsan force-pushed the feat/preserve-anthropic-refusal-metadata branch from f6896ae to 0481fa6 Compare October 13, 2026 19:55
_message = json_mode_message

model_response.choices[0].message = _message
model_response.choices[0].provider_specific_fields = provider_specific_fields

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Choice-level metadata bypasses log redaction

Assigning the entire dictionary here—and in litellm/litellm_core_utils/streaming_chunk_builder_utils.py:1586—duplicates thinking_blocks and citations onto the choice, while _redact_choice_content and _redact_provider_specific_fields_dict only sanitize message/delta fields. Recipients of logging callbacks or stored S3 payloads can now read private reasoning and cited text from choices[0].provider_specific_fields even with turn_off_message_logging=True; extend both object and dict redaction paths to sanitize the choice-level fields.

@veria-ai

veria-ai Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

PR overview

The PR preserves Anthropic’s native refusal metadata through streaming aggregation and non-streaming responses, carrying provider-specific metadata at the choice level.

One issue remains open: choice-level metadata bypasses existing log redaction, exposing private reasoning and cited text to logging callback recipients or stored S3 payloads even when message logging is disabled. No issues have been addressed yet, and both object and dictionary redaction paths need to sanitize these choice-level fields.

Open issues (1)

Fixed/addressed: 0 · PR risk: 5/10

@codspeed

codspeed Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 31 untouched benchmarks


Comparing shotsan:feat/preserve-anthropic-refusal-metadata (0481fa6) with main (829bd6a)

Open in CodSpeed

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

#43752 was fixed by #46104 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Preserve Anthropic-native refusal metadata (stop_reason, stop_details) through streaming aggregation

1 participant