From 295ad527d50540a3eb66cb5a29933ef4cac07102 Mon Sep 17 00:00:00 2001 From: Ninad Phalak Date: Thu, 3 Sep 2026 19:13:31 -0500 Subject: [PATCH] fix(guardrails): narrow the stream delta before writing to it basedpyright could not prove the delta was non-None on the write path, and reportOptionalMemberAccess has a zero budget. The guard is also clearer than relying on the text check to imply it. --- .../guardrails/guardrail_hooks/llm_shield/llm_shield.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/llm_shield/llm_shield.py b/litellm/proxy/guardrails/guardrail_hooks/llm_shield/llm_shield.py index fe8b70c08b8..c126bf0de37 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/llm_shield/llm_shield.py +++ b/litellm/proxy/guardrails/guardrail_hooks/llm_shield/llm_shield.py @@ -381,12 +381,12 @@ class LLMShieldGuardrail(CustomGuardrail): text = getattr(delta, "content", None) if delta is not None else None is_final = self._is_final_chunk(chunk) - if not isinstance(text, str) or not text: + if delta is None or not isinstance(text, str) or not text: # Nothing to restore in this chunk, but a final chunk still has to # flush whatever the window is holding. - if is_final and carry: + if is_final and carry and delta is not None: emitted, carry = await self._stream_step("", carry, True, session_id) - if emitted and delta is not None: + if emitted: delta.content = emitted yield chunk continue