fix(responses): keep reasoning text inside guardrail inspection

Review findings from the automated reviewers on #40407, plus the lint failure.

The important one: typing reasoning parts as `reasoning_text` removed them from
output guardrails. `_has_text_content`, `_extract_output_text_and_images` and
`_apply_guardrail_responses_to_output` each tested `isinstance(content_item,
OutputText)`, and reasoning parts used to be `OutputText`, so they were
inspected. `OutputReasoningText` is not, so a guardrail would have kept
reporting success while no longer reading the content it was configured to
check. That fails open, which is the wrong direction for a guardrail.

All four checks now accept both parts through one
`GUARDRAIL_TEXT_CONTENT_PARTS` tuple, so a future content-part type is added in
one place rather than in four checks that can drift apart. Both classes carry
`text`, so the extraction and rewrite paths need no other change.

Also in this commit:

- ruff UP007: `list[Union[OutputText, OutputReasoningText]]` becomes
  `list[OutputText | OutputReasoningText]`, matching the rest of the module.
- Dropped the comment above it that restated the types.
- `Union` stays imported in `types/responses/main.py` even though unused there:
  `llms/base_llm/image_edit/transformation.py` does `import *` from it and
  annotates with `Union`, so removing it breaks that module at class-definition
  time. Comment added so the next cleanup does not repeat it.

Verified: ruff clean on both files; new guardrail test red with the tuple
narrowed back to `(OutputText,)`, green with both; `tests/test_litellm/responses/`
fails the same 14 pre-existing tests before and after, identical sets.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Luis Castillo 2026-09-21 14:12:14 +02:00
parent 3d62c4d66e
commit 5419935698
No known key found for this signature in database
3 changed files with 86 additions and 7 deletions

View file

@ -85,10 +85,17 @@ from litellm.types.llms.openai import (
from litellm.types.responses.main import (
GenericResponseOutputItem,
OutputFunctionToolCall,
OutputReasoningText,
OutputText,
)
from litellm.types.utils import GenericGuardrailAPIInputs
# Content parts a guardrail inspects and may rewrite. Both carry ``text``.
# ``reasoning`` items type their parts as ``reasoning_text``; until that type
# existed they were emitted as ``output_text``, so checking ``OutputText``
# alone would silently stop scanning reasoning content.
GUARDRAIL_TEXT_CONTENT_PARTS: Final = (OutputText, OutputReasoningText)
if TYPE_CHECKING:
from fastapi import HTTPException
@ -1020,7 +1027,7 @@ class OpenAIResponsesHandler(BaseTranslation):
if content:
for content_item in content:
# Check if it's an OutputText with text
if isinstance(content_item, OutputText):
if isinstance(content_item, GUARDRAIL_TEXT_CONTENT_PARTS):
if content_item.text:
return True
elif isinstance(content_item, dict):
@ -1100,7 +1107,7 @@ class OpenAIResponsesHandler(BaseTranslation):
# Iterate through content items (list of OutputText objects)
for content_idx, content_item in enumerate(content):
# Handle both OutputText objects and dicts
if isinstance(content_item, OutputText):
if isinstance(content_item, GUARDRAIL_TEXT_CONTENT_PARTS):
text_content = content_item.text
elif isinstance(content_item, dict):
text_content = content_item.get("text")
@ -1144,7 +1151,7 @@ class OpenAIResponsesHandler(BaseTranslation):
if isinstance(output_item, GenericResponseOutputItem):
if output_item.content and content_idx < len(output_item.content):
content_item = output_item.content[content_idx]
if isinstance(content_item, OutputText):
if isinstance(content_item, GUARDRAIL_TEXT_CONTENT_PARTS):
content_item.text = guardrail_response
elif isinstance(content_item, dict):
content_item["text"] = guardrail_response
@ -1154,7 +1161,7 @@ class OpenAIResponsesHandler(BaseTranslation):
generic_item = GenericResponseOutputItem.model_validate(output_item.model_dump())
if generic_item.content and content_idx < len(generic_item.content):
content_item = generic_item.content[content_idx]
if isinstance(content_item, OutputText):
if isinstance(content_item, GUARDRAIL_TEXT_CONTENT_PARTS):
content_item.text = guardrail_response
# Update the original response output
if hasattr(output_item, "content") and output_item.content:

View file

@ -1,3 +1,7 @@
# `Union` is no longer referenced in this module, but
# `litellm/llms/base_llm/image_edit/transformation.py` does
# `from litellm.types.responses.main import *` and annotates with it, so
# dropping it here breaks that import at class-definition time.
from typing import Final, Literal, Optional, Union
from openai.types.responses.response_function_tool_call import ResponseFunctionToolCall
@ -124,9 +128,7 @@ class GenericResponseOutputItem(BaseLiteLLMOpenAIResponseObject):
id: str
status: str # "completed", "in_progress", etc.
role: str # "assistant", "user", etc.
# `reasoning` items carry `reasoning_text` parts; `message` items carry
# `output_text` ones.
content: list[Union[OutputText, OutputReasoningText]]
content: list[OutputText | OutputReasoningText]
phase: Phase = None

View file

@ -0,0 +1,70 @@
"""Reasoning content parts must stay visible to output guardrails.
Before `reasoning_text` existed, a reasoning item's parts were emitted as
`output_text`, so the guardrail handler's `isinstance(..., OutputText)` checks
saw them. Introducing `OutputReasoningText` would have silently removed
reasoning text from guardrail inspection and rewriting, which fails open: the
guardrail keeps reporting success while no longer reading the content.
"""
from litellm.llms.openai.responses.guardrail_translation.handler import (
GUARDRAIL_TEXT_CONTENT_PARTS,
OpenAIResponsesHandler,
)
from litellm.types.responses.main import (
GenericResponseOutputItem,
OutputReasoningText,
OutputText,
)
def _reasoning_item(text: str) -> GenericResponseOutputItem:
return GenericResponseOutputItem(
type="reasoning",
id="rs_1",
status="completed",
role="assistant",
content=[OutputReasoningText(type="reasoning_text", text=text)],
)
def _message_item(text: str) -> GenericResponseOutputItem:
return GenericResponseOutputItem(
type="message",
id="msg_1",
status="completed",
role="assistant",
content=[OutputText(type="output_text", text=text, annotations=None)],
)
def test_both_content_part_types_are_inspected():
assert OutputText in GUARDRAIL_TEXT_CONTENT_PARTS
assert OutputReasoningText in GUARDRAIL_TEXT_CONTENT_PARTS
def test_reasoning_text_is_extracted_for_checking():
handler = OpenAIResponsesHandler()
texts: list[str] = []
images: list[str] = []
mappings: list[tuple[int, int]] = []
handler._extract_output_text_and_images(
_reasoning_item("secret chain of thought"), 0, texts, images, mappings
)
assert texts == ["secret chain of thought"]
assert mappings == [(0, 0)]
def test_message_text_is_still_extracted():
handler = OpenAIResponsesHandler()
texts: list[str] = []
images: list[str] = []
mappings: list[tuple[int, int]] = []
handler._extract_output_text_and_images(
_message_item("the answer"), 0, texts, images, mappings
)
assert texts == ["the answer"]