Skip to content

Commit 4d0fd70

Browse files
committed
Preserve input empty strings
1 parent c47d352 commit 4d0fd70

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/guardrails/agents.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ def _extract_text_from_input(input_data: Any) -> str:
380380
if field in part:
381381
text = part[field]
382382
break
383-
if text and isinstance(text, str):
383+
# Preserve empty strings, only filter None
384+
if text is not None and isinstance(text, str):
384385
text_parts.append(text)
385386
if text_parts:
386387
return " ".join(text_parts)
@@ -478,8 +479,8 @@ async def single_guardrail(ctx: RunContextWrapper[None], agent: Agent, input_dat
478479

479480
except Exception as e:
480481
if raise_guardrail_errors:
481-
# Re-raise the exception to stop execution
482-
raise e
482+
# Re-raise the exception to stop execution (preserve traceback)
483+
raise
483484
else:
484485
# Current behavior: treat errors as tripwires
485486
# Return structured error info for consistency

tests/unit/test_agents.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,24 @@ def test_extract_text_from_input_with_no_user_messages() -> None:
868868
assert result == "" # noqa: S101
869869

870870

871+
def test_extract_text_from_input_preserves_empty_strings() -> None:
872+
"""Empty strings in content parts should be preserved, not filtered out."""
873+
input_data = [
874+
{
875+
"role": "user",
876+
"type": "message",
877+
"content": [
878+
{"type": "input_text", "text": "Hello"},
879+
{"type": "input_text", "text": ""}, # Empty string should be preserved
880+
{"type": "input_text", "text": "World"},
881+
],
882+
}
883+
]
884+
result = agents._extract_text_from_input(input_data)
885+
# Empty string should be included, resulting in extra space
886+
assert result == "Hello World" # noqa: S101
887+
888+
871889
# =============================================================================
872890
# Tests for updated agent-level guardrail behavior (stage_name and metadata)
873891
# =============================================================================

0 commit comments

Comments
 (0)