Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

refactor: type the buffered stream rewrite helpers without Any

mateo-berri113350756595
diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.py
index cbbccac17f3..6f966ae1a02 100644
--- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py
+++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py
@@ -13,7 +13,7 @@ Pattern Overview:
"""
import json
-from collections.abc import Mapping, Sequence
+from collections.abc import Mapping, MutableSequence, Sequence
from copy import deepcopy
from dataclasses import dataclass
from itertools import chain, repeat
@@ -1262,7 +1262,7 @@ class AnthropicMessagesHandler(BaseTranslation):
@staticmethod
def _write_ended_stream_text_rewrite(
- responses_so_far: list[Any], # mutable-ok: rewrites the caller's buffered chunks in place
+ responses_so_far: MutableSequence[object], # mutable-ok: rewrites the caller's buffered chunks in place
rewritten_text: str,
) -> None:
"""Deliver an ended-stream guardrail text rewrite by rewriting the
@@ -1284,7 +1284,7 @@ class AnthropicMessagesHandler(BaseTranslation):
@classmethod
def _write_ended_stream_tool_call_rewrites(
cls,
- responses_so_far: list[Any], # mutable-ok: rewrites the caller's buffered chunks in place
+ responses_so_far: MutableSequence[object], # mutable-ok: rewrites the caller's buffered chunks in place
*,
pre_guardrail_tool_calls: tuple[_ToolCallShape, ...],
post_guardrail_tool_calls: tuple[_ToolCallShape, ...],
@@ -1346,7 +1346,7 @@ class AnthropicMessagesHandler(BaseTranslation):
@staticmethod
def _rewrite_ended_stream_events(
- responses_so_far: list[Any], # mutable-ok: rewrites the caller's buffered chunks in place
+ responses_so_far: MutableSequence[object], # mutable-ok: rewrites the caller's buffered chunks in place
rewrite_event: _SSEEventRewriter,
) -> None:
"""Replace every buffered event ``rewrite_event`` returns a rewrite for, in
diff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py
index fa1e969b277..6ed4ec6618f 100644
--- a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py
+++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py
@@ -17,6 +17,7 @@ from fastapi import HTTPException
from openai.types.responses import ResponseFunctionToolCall
from litellm.integrations.custom_guardrail import CustomGuardrail
+from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.llms import get_guardrail_translation_mapping
from litellm.llms.openai.responses.guardrail_translation.handler import (
OpenAIResponsesHandler,
@@ -1238,7 +1239,7 @@ class TestOpenAIResponsesHandlerStreamingOutputProcessing:
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
- logging_obj: Optional[Any] = None,
+ logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
tool_calls = [
{**tool_call, "function": {**tool_call["function"], "arguments": '{"fruit": "[MASKED]"}'}}