Skip to content

Commit a26126d

Browse files
committed
add unit test
1 parent 5a2c212 commit a26126d

File tree

3 files changed

+357
-11
lines changed

3 files changed

+357
-11
lines changed

examples/basic/agents_sdk.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ async def main() -> None:
7272
run_config=RunConfig(tracing_disabled=True),
7373
session=session,
7474
)
75-
agent = result.new_items[0].agent
76-
print(f"Input guardrails: {[x.name for x in agent.input_guardrails]}")
77-
breakpoint()
78-
print(f"Output guardrails: {[x.name for x in agent.output_guardrails]}")
7975
print(f"Assistant: {result.final_output}")
8076
except EOFError:
8177
print("\nExiting.")

src/guardrails/agents.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@ def _extract_text_from_input(input_data: Any) -> str:
356356
return input_data
357357

358358
# If it's a list (conversation history), extract the latest user message
359-
if isinstance(input_data, list) and len(input_data) > 0:
359+
if isinstance(input_data, list):
360+
if len(input_data) == 0:
361+
return "" # Empty list returns empty string
362+
360363
# Iterate from the end to find the latest user message
361364
for msg in reversed(input_data):
362365
if isinstance(msg, dict):
@@ -381,6 +384,9 @@ def _extract_text_from_input(input_data: Any) -> str:
381384
elif content is not None:
382385
return str(content)
383386

387+
# No user message found in list
388+
return ""
389+
384390
# Fallback: convert to string
385391
return str(input_data)
386392

@@ -483,7 +489,7 @@ async def single_guardrail(ctx: RunContextWrapper[None], agent: Agent, input_dat
483489

484490
# Set the function name to the guardrail name (e.g., "Moderation" → "Moderation")
485491
single_guardrail.__name__ = guardrail.definition.name.replace(" ", "_")
486-
492+
487493
return single_guardrail
488494

489495
guardrail_functions = []

0 commit comments

Comments
 (0)