-   Notifications  
You must be signed in to change notification settings  - Fork 2.8k
 
Added RunErrorDetails object for MaxTurnsExceeded exception #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Merged  
  rm-openai merged 5 commits into openai:main from DanieleMorotti:max_turn_exception_result        May 29, 2025  
    Merged  
 Changes from 1 commit
 Commits 
  Show all changes 
  5 commits   Select commit Hold shift + click to select a range 
 c1e43a6  Added RunErrorDetails object for MaxTurnsExceeded exception 
  DanieleMorotti 387b5eb  Changes requested by rm-openai 
  DanieleMorotti 7989e0d  Merge branch 'openai:main' into max_turn_exception_result 
  DanieleMorotti f083c48  Return error details for streaming + tests 
  DanieleMorotti 6736687  Implemented utility function to create `RunErrorDetails` avoiding cod… 
  DanieleMorotti File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
 Return error details for streaming + tests 
 - Loading branch information
 
   commit f083c48e8a870e83036663a69acb41c9dba71ec7  
 There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 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 | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
      Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code seems ~identical to lines 236-243 and the ones before, thoughts on extracting into
self._create_error_details?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. I also noticed it but I was waiting for your feedback. I'll change this.
Do I also need to add an explanation of these changes to the documentation?