Skip to content

Commit a2af559

Browse files
committed
Address Copilot comments
1 parent 4d0fd70 commit a2af559

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/guardrails/agents.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ def _extract_text_from_input(input_data: Any) -> str:
370370
if isinstance(content, str):
371371
return content
372372
elif isinstance(content, list):
373+
if not content:
374+
# Empty content list returns empty string (consistent with no text parts found)
375+
return ""
373376
# Extract text from content parts
374377
text_parts = []
375378
for part in content:
@@ -385,6 +388,8 @@ def _extract_text_from_input(input_data: Any) -> str:
385388
text_parts.append(text)
386389
if text_parts:
387390
return " ".join(text_parts)
391+
# No text parts found, return empty string
392+
return ""
388393
# If content is something else, try to stringify it
389394
elif content is not None:
390395
return str(content)
@@ -452,8 +457,12 @@ class DefaultContext:
452457

453458
def _create_individual_guardrail(guardrail):
454459
"""Create a function for a single specific guardrail."""
455-
async def single_guardrail(ctx: RunContextWrapper[None], agent: Agent, input_data: str) -> GuardrailFunctionOutput:
456-
"""Guardrail function for a specific guardrail check."""
460+
async def single_guardrail(ctx: RunContextWrapper[None], agent: Agent, input_data: str | list) -> GuardrailFunctionOutput:
461+
"""Guardrail function for a specific guardrail check.
462+
463+
Note: input_data is typed as str in Agents SDK, but can actually be a list
464+
of message objects when conversation history is used. We handle both cases.
465+
"""
457466
try:
458467
# Extract text from input_data (handle both string and conversation history formats)
459468
text_data = _extract_text_from_input(input_data)

tests/unit/test_agents.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections.abc import Callable
88
from dataclasses import dataclass
99
from types import SimpleNamespace
10-
from typing import Any, TypedDict
10+
from typing import Any
1111

1212
import pytest
1313

@@ -130,15 +130,6 @@ async def run(self, *args: Any, **kwargs: Any) -> Any:
130130
import guardrails.runtime as runtime_module # noqa: E402
131131

132132

133-
# Add mock for TResponseInputItem for testing
134-
class TResponseInputItem(TypedDict):
135-
"""Mock type for Agents SDK response input item."""
136-
137-
role: str
138-
content: Any
139-
type: str
140-
141-
142133
def _make_guardrail(name: str) -> Any:
143134
class _DummyCtxModel:
144135
model_fields: dict[str, Any] = {}
@@ -886,6 +877,20 @@ def test_extract_text_from_input_preserves_empty_strings() -> None:
886877
assert result == "Hello World" # noqa: S101
887878

888879

880+
def test_extract_text_from_input_with_empty_content_list() -> None:
881+
"""Empty content list should return empty string, not stringified list."""
882+
input_data = [
883+
{
884+
"role": "user",
885+
"type": "message",
886+
"content": [], # Empty content list
887+
}
888+
]
889+
result = agents._extract_text_from_input(input_data)
890+
# Should return empty string, not "[]"
891+
assert result == "" # noqa: S101
892+
893+
889894
# =============================================================================
890895
# Tests for updated agent-level guardrail behavior (stage_name and metadata)
891896
# =============================================================================

0 commit comments

Comments
 (0)