@@ -11582,3 +11582,117 @@ async def test_schema_1_13_hosted_mcp_orphaned_call_decisions_require_reapproval
1158211582 )
1158311583 == "legacy exact denial"
1158411584 )
11585+
11586+
11587+ @pytest .mark .asyncio
11588+ async def test_runner_guardrail_models_survive_state_serialization () -> None :
11589+ from agents .testing import assistant_message
11590+
11591+ class Verdict (BaseModel ):
11592+ allowed : bool
11593+ reason : str
11594+
11595+ class Answer (BaseModel ):
11596+ text : str
11597+
11598+ async def check (* args : Any ) -> GuardrailFunctionOutput :
11599+ return GuardrailFunctionOutput (
11600+ output_info = Verdict (allowed = True , reason = "approved" ),
11601+ tripwire_triggered = False ,
11602+ )
11603+
11604+ agent = Agent (
11605+ name = "Audit" ,
11606+ model = ScriptedModel ([[assistant_message ('{"text":"hello"}' )]]),
11607+ output_type = Answer ,
11608+ input_guardrails = [InputGuardrail (check )],
11609+ output_guardrails = [OutputGuardrail (check )],
11610+ )
11611+ result = await Runner .run (agent , "hello" , run_config = RunConfig (tracing_disabled = True ))
11612+ restored = await RunState .from_json (agent , json .loads (result .to_state ().to_string ()))
11613+
11614+ assert restored ._input_guardrail_results [0 ].output .output_info == {
11615+ "allowed" : True ,
11616+ "reason" : "approved" ,
11617+ }
11618+ assert restored ._output_guardrail_results [0 ].output .output_info == {
11619+ "allowed" : True ,
11620+ "reason" : "approved" ,
11621+ }
11622+ assert restored ._output_guardrail_results [0 ].agent_output == {"text" : "hello" }
11623+
11624+
11625+ @pytest .mark .asyncio
11626+ async def test_tool_guardrail_dataclasses_survive_state_serialization () -> None :
11627+ @dataclass
11628+ class Verdict :
11629+ allowed : bool
11630+ reason : str
11631+
11632+ verdict = Verdict (allowed = True , reason = "approved" )
11633+ output = ToolGuardrailFunctionOutput (
11634+ output_info = {"checks" : [verdict ]}, behavior = AllowBehavior (type = "allow" )
11635+ )
11636+ agent = Agent (name = "Audit" )
11637+ state = make_state (agent , context = RunContextWrapper (context = None ))
11638+ state ._tool_input_guardrail_results = [
11639+ ToolInputGuardrailResult (
11640+ guardrail = ToolInputGuardrail (lambda data : output , name = "input" ),
11641+ output = output ,
11642+ )
11643+ ]
11644+ state ._tool_output_guardrail_results = [
11645+ ToolOutputGuardrailResult (
11646+ guardrail = ToolOutputGuardrail (lambda data : output , name = "output" ),
11647+ output = output ,
11648+ )
11649+ ]
11650+ restored = await RunState .from_json (agent , json .loads (state .to_string ()))
11651+ expected = {"checks" : [{"allowed" : True , "reason" : "approved" }]}
11652+ assert restored ._tool_input_guardrail_results [0 ].output .output_info == expected
11653+ assert restored ._tool_output_guardrail_results [0 ].output .output_info == expected
11654+
11655+
11656+ @pytest .mark .asyncio
11657+ async def test_guardrail_state_keeps_fallback_when_model_serializer_raises () -> None :
11658+ class Diagnostic (BaseModel ):
11659+ reason : str
11660+
11661+ @model_serializer
11662+ def serialize (self ) -> dict [str , Any ]:
11663+ raise ValueError ("Serializer unavailable." )
11664+
11665+ diagnostic = Diagnostic (reason = "approved" )
11666+ output = GuardrailFunctionOutput (output_info = diagnostic , tripwire_triggered = False )
11667+ agent = Agent (name = "Audit" )
11668+ state = make_state (agent , context = RunContextWrapper (context = None ))
11669+ state ._input_guardrail_results = [
11670+ InputGuardrailResult (guardrail = InputGuardrail (lambda * args : output ), output = output )
11671+ ]
11672+ state ._output_guardrail_results = [
11673+ OutputGuardrailResult (
11674+ guardrail = OutputGuardrail (lambda * args : output ),
11675+ agent = agent ,
11676+ agent_output = diagnostic ,
11677+ output = output ,
11678+ )
11679+ ]
11680+ tool_output = ToolGuardrailFunctionOutput (
11681+ output_info = diagnostic , behavior = AllowBehavior (type = "allow" )
11682+ )
11683+ state ._tool_input_guardrail_results = [
11684+ ToolInputGuardrailResult (
11685+ guardrail = ToolInputGuardrail (lambda data : tool_output ), output = tool_output
11686+ )
11687+ ]
11688+ state ._tool_output_guardrail_results = [
11689+ ToolOutputGuardrailResult (
11690+ guardrail = ToolOutputGuardrail (lambda data : tool_output ), output = tool_output
11691+ )
11692+ ]
11693+ restored = await RunState .from_json (agent , json .loads (state .to_string ()))
11694+ assert restored ._input_guardrail_results [0 ].output .output_info == "reason='approved'"
11695+ assert restored ._output_guardrail_results [0 ].output .output_info == "reason='approved'"
11696+ assert restored ._output_guardrail_results [0 ].agent_output == "reason='approved'"
11697+ assert restored ._tool_input_guardrail_results [0 ].output .output_info == "reason='approved'"
11698+ assert restored ._tool_output_guardrail_results [0 ].output .output_info == "reason='approved'"
0 commit comments