|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from agents import Agent |
| 9 | +from agents.agent_output import AgentOutputSchemaBase |
| 10 | +from agents.exceptions import MaxTurnsExceeded, UserError |
| 11 | +from agents.run_context import RunContextWrapper |
| 12 | +from agents.run_error_handlers import RunErrorData |
| 13 | +from agents.run_internal import error_handlers as run_error_handlers |
| 14 | + |
| 15 | + |
| 16 | +class _CustomSchema(AgentOutputSchemaBase): |
| 17 | + def is_plain_text(self) -> bool: |
| 18 | + return False |
| 19 | + |
| 20 | + def name(self) -> str: |
| 21 | + return "CustomSchema" |
| 22 | + |
| 23 | + def json_schema(self) -> dict[str, Any]: |
| 24 | + return {"type": "object"} |
| 25 | + |
| 26 | + def is_strict_json_schema(self) -> bool: |
| 27 | + return True |
| 28 | + |
| 29 | + def validate_json(self, json_str: str) -> Any: |
| 30 | + return json.loads(json_str) |
| 31 | + |
| 32 | + |
| 33 | +def _make_run_data(agent: Agent[Any]) -> RunErrorData: |
| 34 | + return RunErrorData( |
| 35 | + input="hello", |
| 36 | + new_items=[], |
| 37 | + history=[], |
| 38 | + output=[], |
| 39 | + raw_responses=[], |
| 40 | + last_agent=agent, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def test_format_final_output_text_handles_wrapped_payload() -> None: |
| 45 | + agent = Agent(name="wrapped-output", output_type=list[str]) |
| 46 | + output = {"response": ["a", "b"]} |
| 47 | + |
| 48 | + rendered = run_error_handlers.format_final_output_text(agent, output) |
| 49 | + assert json.loads(rendered) == output |
| 50 | + |
| 51 | + |
| 52 | +def test_validate_handler_final_output_accepts_wrapped_payload() -> None: |
| 53 | + agent = Agent(name="wrapped-validate", output_type=list[str]) |
| 54 | + output = {"response": ["ok"]} |
| 55 | + |
| 56 | + validated = run_error_handlers.validate_handler_final_output(agent, output) |
| 57 | + assert validated == ["ok"] |
| 58 | + |
| 59 | + |
| 60 | +def test_format_final_output_text_uses_custom_schema_and_fallback( |
| 61 | + monkeypatch: pytest.MonkeyPatch, |
| 62 | +) -> None: |
| 63 | + agent = Agent(name="custom-format") |
| 64 | + custom_schema = _CustomSchema() |
| 65 | + monkeypatch.setattr(run_error_handlers, "get_output_schema", lambda _agent: custom_schema) |
| 66 | + |
| 67 | + rendered = run_error_handlers.format_final_output_text(agent, {"ok": True}) |
| 68 | + assert json.loads(rendered) == {"ok": True} |
| 69 | + |
| 70 | + value = object() |
| 71 | + fallback = run_error_handlers.format_final_output_text(agent, value) |
| 72 | + assert fallback == str(value) |
| 73 | + |
| 74 | + |
| 75 | +def test_validate_handler_final_output_raises_for_unserializable_data( |
| 76 | + monkeypatch: pytest.MonkeyPatch, |
| 77 | +) -> None: |
| 78 | + agent = Agent(name="custom-validate") |
| 79 | + custom_schema = _CustomSchema() |
| 80 | + monkeypatch.setattr(run_error_handlers, "get_output_schema", lambda _agent: custom_schema) |
| 81 | + |
| 82 | + with pytest.raises(UserError, match="Invalid run error handler final_output"): |
| 83 | + run_error_handlers.validate_handler_final_output(agent, {"bad": {1, 2}}) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.asyncio |
| 87 | +async def test_resolve_run_error_handler_result_covers_async_and_validation_paths() -> None: |
| 88 | + agent = Agent(name="max-turns") |
| 89 | + context_wrapper: RunContextWrapper[dict[str, Any]] = RunContextWrapper(context={}) |
| 90 | + run_data = _make_run_data(agent) |
| 91 | + error = MaxTurnsExceeded("too many turns") |
| 92 | + |
| 93 | + no_handler = await run_error_handlers.resolve_run_error_handler_result( |
| 94 | + error_handlers={}, |
| 95 | + error=error, |
| 96 | + context_wrapper=context_wrapper, |
| 97 | + run_data=run_data, |
| 98 | + ) |
| 99 | + assert no_handler is None |
| 100 | + |
| 101 | + async def async_handler(_handler_input: Any) -> None: |
| 102 | + return None |
| 103 | + |
| 104 | + async_none = await run_error_handlers.resolve_run_error_handler_result( |
| 105 | + error_handlers={"max_turns": async_handler}, |
| 106 | + error=error, |
| 107 | + context_wrapper=context_wrapper, |
| 108 | + run_data=run_data, |
| 109 | + ) |
| 110 | + assert async_none is None |
| 111 | + |
| 112 | + with pytest.raises(UserError, match="Invalid run error handler result"): |
| 113 | + await run_error_handlers.resolve_run_error_handler_result( |
| 114 | + error_handlers={ |
| 115 | + "max_turns": lambda _handler_input: {"final_output": "x", "extra": "y"} |
| 116 | + }, |
| 117 | + error=error, |
| 118 | + context_wrapper=context_wrapper, |
| 119 | + run_data=run_data, |
| 120 | + ) |
0 commit comments