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 <noreply@anthropic.com>
This commit is contained in:
Alexander Grattan 2026-01-27 10:17:03 -05:00
parent 090743d8ee
commit e4a16241a1

View file

@ -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]