From e54d4b8d12053d8dbfa72a9024e20dbdb2c8d644 Mon Sep 17 00:00:00 2001 From: Alessandro Pignati Date: Sun, 1 Nov 2026 22:24:57 +0100 Subject: [PATCH] fix(guardrails): label the diff-mode round with the choice it inspected The ended-stream path labelled every inspection, including a single choice, while a diff-mode round only labelled the per-choice rounds of an n>1 stream. A single-choice round logged no choice at all. Label it with the choice its texts came from. --- litellm/llms/openai/chat/guardrail_translation/handler.py | 2 ++ .../chat/guardrail_translation/test_openai_guardrail_handler.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.py index 01cf518e04b..bd76a2ae9f2 100644 --- a/litellm/llms/openai/chat/guardrail_translation/handler.py +++ b/litellm/llms/openai/chat/guardrail_translation/handler.py @@ -951,6 +951,8 @@ class OpenAIChatCompletionsHandler(BaseTranslation): prior_responses: Final = resolved_request_data.get("responses", missing) prior_choice_index: Final = resolved_request_data.get("guardrail_choice_index", missing) resolved_request_data.pop("guardrail_choice_index", None) + if len(indices) == 1: + resolved_request_data["guardrail_choice_index"] = indices[0] if "responses" not in resolved_request_data: resolved_request_data["responses"] = responses_so_far self.merge_user_api_key_metadata_into_request(resolved_request_data, user_api_key_dict) diff --git a/tests/unit/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py b/tests/unit/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py index 73abb328848..d8d06f34519 100644 --- a/tests/unit/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py +++ b/tests/unit/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py @@ -1702,7 +1702,7 @@ class TestOpenAIChatCompletionsHandlerStreamingOutput: request_data=request_data, stream_transform_sink=StreamTransformSink(), ) - assert LabelReadingGuardrail.seen == [None] + assert LabelReadingGuardrail.seen == [0] assert request_data["guardrail_choice_index"] == 7 @pytest.mark.asyncio