Repository navigation
Conversation
|
| if "stop_details" in delta: | ||
| response["stop_details"] = delta["stop_details"] |
There was a problem hiding this comment.
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)
f6896ae to
0481fa6
Compare
| _message = json_mode_message | ||
|
|
||
| model_response.choices[0].message = _message | ||
| model_response.choices[0].provider_specific_fields = provider_specific_fields |
There was a problem hiding this comment.
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.
PR overviewThe 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 |
Merging this PR will not alter performance
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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_reasonto"content_filter"for OpenAI schema compatibility, but previously droppedstop_reasonandstop_details. Consequently:ModelResponseIterator),message_deltaevents did not populatedelta.provider_specific_fieldswith refusal metadata.stream_chunk_builder),combined_provider_fieldswas attached to_choice.message.provider_specific_fields, but was not propagated to_choice.provider_specific_fieldsorresponse._hidden_params["provider_specific_fields"].AnthropicConfig._build_provider_specific_fields),stop_reasonandstop_detailswere omitted when present.agentic_streaming_iterator.py),stop_detailsindeltawas omitted from the response dictionary.Changes
litellm/llms/anthropic/chat/handler.py: Inchunk_parser(), checkmessage_deltaforstop_reason == "refusal"orstop_details is not Noneand attach them toprovider_specific_fields.litellm/llms/anthropic/chat/transformation.py: In_build_provider_specific_fields(), checkcompletion_responseforstop_reason == "refusal"orstop_details is not Noneand attach them toprovider_specific_fields. Also setmodel_response.choices[0].provider_specific_fields = provider_specific_fields.litellm/main.py: Instream_chunk_builder(), propagatecombined_provider_fieldsto_choice.provider_specific_fieldsand updateresponse._hidden_params["provider_specific_fields"].litellm/llms/anthropic/experimental_pass_through/messages/agentic_streaming_iterator.py: In_handle_message_delta(), capturestop_detailsif present indelta.tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py: Added automated regression tests for:stream_chunk_builder