Skip to content

Commit 09113b6

Browse files
lint
1 parent 931336a commit 09113b6

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

examples/basic/max_turns_resume.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def main():
2828
input="Give me the main stages of the water cycle.",
2929
max_turns=1,
3030
)
31-
except MaxTurnsExceeded as exc:
31+
except MaxTurnsExceeded as max_turns_exc:
3232
print("Reached the max turn limit. Asking the agent to finalize without tools...\n")
33-
result = exc.resume_sync(
33+
result = max_turns_exc.resume_sync(
3434
"Finish the answer using the gathered information without calling tools again."
3535
)
3636
print(result.final_output)

src/agents/exceptions.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
from dataclasses import dataclass, replace
44
from textwrap import dedent
5-
from typing import TYPE_CHECKING, Any, Optional
5+
from typing import TYPE_CHECKING, Any
66

77
if TYPE_CHECKING:
88
from .agent import Agent
99
from .guardrail import InputGuardrailResult, OutputGuardrailResult
1010
from .items import ModelResponse, RunItem, TResponseInputItem
11-
from .run_context import RunContextWrapper
12-
from .run import RunConfig
1311
from .result import RunResult
12+
from .run import RunConfig
13+
from .run_context import RunContextWrapper
1414
from .tool_guardrails import (
1515
ToolGuardrailFunctionOutput,
1616
ToolInputGuardrail,
@@ -54,14 +54,15 @@ class MaxTurnsExceeded(AgentsException):
5454

5555
_DEFAULT_RESUME_PROMPT = """
5656
You reached the maximum number of turns.
57-
Return a final answer to the query using ONLY the information already gathered in the conversation so far.
57+
Return a final answer to the query using ONLY the information already gathered \
58+
in the conversation so far.
5859
"""
5960

6061
def __init__(self, message: str):
6162
self.message = message
6263
super().__init__(message)
6364

64-
async def resume(self, prompt: Optional[str] = _DEFAULT_RESUME_PROMPT) -> RunResult:
65+
async def resume(self, prompt: str | None = _DEFAULT_RESUME_PROMPT) -> RunResult:
6566
"""Resume the failed run asynchronously with a final, tool-free turn.
6667
6768
Note:
@@ -87,7 +88,7 @@ async def resume(self, prompt: Optional[str] = _DEFAULT_RESUME_PROMPT) -> RunRes
8788
run_config=run_config,
8889
)
8990

90-
def resume_sync(self, prompt: Optional[str] = _DEFAULT_RESUME_PROMPT) -> RunResult:
91+
def resume_sync(self, prompt: str | None = _DEFAULT_RESUME_PROMPT) -> RunResult:
9192
"""Resume the failed run synchronously with a final, tool-free turn.
9293
9394
Note:
@@ -116,7 +117,7 @@ def resume_sync(self, prompt: Optional[str] = _DEFAULT_RESUME_PROMPT) -> RunResu
116117
def _prepare_resume_arguments(
117118
self,
118119
run_data: RunErrorDetails,
119-
prompt: Optional[str] = None,
120+
prompt: str | None = None,
120121
) -> tuple[list[TResponseInputItem], RunConfig]:
121122
from .items import ItemHelpers
122123
from .model_settings import ModelSettings
@@ -142,7 +143,7 @@ def _prepare_resume_arguments(
142143
run_config,
143144
)
144145

145-
def _normalize_resume_prompt(self, prompt: Optional[str]) -> Optional[str]:
146+
def _normalize_resume_prompt(self, prompt: str | None) -> str | None:
146147
if prompt is None:
147148
return None
148149
normalized = dedent(prompt).strip()
@@ -151,7 +152,8 @@ def _normalize_resume_prompt(self, prompt: Optional[str]) -> Optional[str]:
151152
def _require_run_data(self) -> RunErrorDetails:
152153
if self.run_data is None:
153154
raise RuntimeError(
154-
"Run data is not available; resume() can only be called on exceptions raised by Runner."
155+
"Run data is not available; resume() can only be called on\
156+
exceptions raised by Runner."
155157
)
156158
return self.run_data
157159

src/agents/result.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class RunResultBase(abc.ABC):
7070
context_wrapper: RunContextWrapper[Any]
7171
"""The context wrapper for the agent run."""
7272

73-
run_config: "RunConfig"
73+
run_config: RunConfig
7474
"""The run configuration that was used for the agent run."""
7575

7676
@property

tests/test_max_turns.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,12 @@ def test_max_turns_resume_sync_uses_default_prompt():
130130
with pytest.raises(MaxTurnsExceeded) as exc_info:
131131
Runner.run_sync(agent, input="user_message", max_turns=2)
132132

133-
result = exc_info.value.resume_sync()
133+
resume_prompt = "Return a final answer to the query using ONLY the information already gathered"
134+
result = exc_info.value.resume_sync(resume_prompt)
134135

135136
assert result.final_output == final_answer
136137
resume_input = model.last_turn_args["input"]
137-
expected_prompt = (
138-
"You reached the maximum number of turns.\n"
139-
"Return a final answer to the query using ONLY the information already gathered in the conversation so far."
140-
)
141-
assert resume_input[-1] == {"content": expected_prompt, "role": "user"}
138+
assert resume_input[-1] == {"content": resume_prompt, "role": "user"}
142139
assert model.last_turn_args["model_settings"].tool_choice == "none"
143140

144141

0 commit comments

Comments
 (0)