### Summary Introduced the `RunErrorDetails` object to get partial results from a run interrupted by `MaxTurnsExceeded` exception. In this proposal the `RunErrorDetails` object contains all the fields from `RunResult` with `final_output` set to `None` and `output_guardrail_results` set to an empty list. We can decide to return less information. @rm-openai At the moment the exception doesn't return the `RunErrorDetails` object for the streaming mode. Do you have any suggestions on how to deal with it? In the `_check_errors` function of `agents/result.py` file. ### Test plan I have not implemented any tests currently, but if needed I can implement a basic test to retrieve partial data. ### Issue number This PR is an attempt to solve issue #719 ### Checks - [✅ ] I've added new tests (if relevant) - [ ] I've added/updated the relevant documentation - [ ✅] I've run `make lint` and `make format` - [ ✅] I've made sure tests pass
44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from agents import Agent, MaxTurnsExceeded, RunErrorDetails, Runner
|
|
|
|
from .fake_model import FakeModel
|
|
from .test_responses import get_function_tool, get_function_tool_call, get_text_message
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_error_includes_data():
|
|
model = FakeModel()
|
|
agent = Agent(name="test", model=model, tools=[get_function_tool("foo", "res")])
|
|
model.add_multiple_turn_outputs([
|
|
[get_text_message("1"), get_function_tool_call("foo", json.dumps({"a": "b"}))],
|
|
[get_text_message("done")],
|
|
])
|
|
with pytest.raises(MaxTurnsExceeded) as exc:
|
|
await Runner.run(agent, input="hello", max_turns=1)
|
|
data = exc.value.run_data
|
|
assert isinstance(data, RunErrorDetails)
|
|
assert data.last_agent == agent
|
|
assert len(data.raw_responses) == 1
|
|
assert len(data.new_items) > 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_streamed_run_error_includes_data():
|
|
model = FakeModel()
|
|
agent = Agent(name="test", model=model, tools=[get_function_tool("foo", "res")])
|
|
model.add_multiple_turn_outputs([
|
|
[get_text_message("1"), get_function_tool_call("foo", json.dumps({"a": "b"}))],
|
|
[get_text_message("done")],
|
|
])
|
|
result = Runner.run_streamed(agent, input="hello", max_turns=1)
|
|
with pytest.raises(MaxTurnsExceeded) as exc:
|
|
async for _ in result.stream_events():
|
|
pass
|
|
data = exc.value.run_data
|
|
assert isinstance(data, RunErrorDetails)
|
|
assert data.last_agent == agent
|
|
assert len(data.raw_responses) == 1
|
|
assert len(data.new_items) > 0
|