From e4a16241a17beb869429a7e7ec4fefd44a5a2e36 Mon Sep 17 00:00:00 2001 From: Alexander Grattan Date: Tue, 27 Jan 2026 10:17:03 -0500 Subject: [PATCH] fix: resolve mypy union-attr errors in anthropic guardrail handler Cast choices[0] to Choices type before accessing .message attribute to satisfy mypy's union type checking for Choices | StreamingChoices. Co-Authored-By: Claude Opus 4.5 --- .../llms/anthropic/chat/guardrail_translation/handler.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.py index c51c1c19804..6f502e9870b 100644 --- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py +++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py @@ -34,6 +34,7 @@ from litellm.types.llms.openai import ( ) from litellm.types.utils import ( ChatCompletionMessageToolCall, + Choices, GenericGuardrailAPIInputs, ModelResponse, ) @@ -376,11 +377,12 @@ class AnthropicMessagesHandler(BaseTranslation): and built_response.choices ): model_response = cast(ModelResponse, built_response) + first_choice = cast(Choices, model_response.choices[0]) tool_calls_list = cast( Optional[List[ChatCompletionMessageToolCall]], - model_response.choices[0].message.tool_calls, + first_choice.message.tool_calls, ) - string_so_far = model_response.choices[0].message.content + string_so_far = first_choice.message.content guardrail_inputs = GenericGuardrailAPIInputs() if string_so_far: guardrail_inputs["texts"] = [string_so_far]