Skip to content

Don't cache agent tools during a run #803

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
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/agents/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@dataclass
class RunErrorDetails:
"""Data collected from an agent run when an exception occurs."""

input: str | list[TResponseInputItem]
new_items: list[RunItem]
raw_responses: list[ModelResponse]
Expand All @@ -29,6 +30,7 @@ def __str__(self) -> str:

class AgentsException(Exception):
"""Base class for all exceptions in the Agents SDK."""

run_data: RunErrorDetails | None

def __init__(self, *args: object) -> None:
Expand Down
6 changes: 4 additions & 2 deletions src/agents/extensions/models/litellm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ async def get_response(
input_tokens_details=InputTokensDetails(
cached_tokens=getattr(
response_usage.prompt_tokens_details, "cached_tokens", 0
) or 0
)
or 0
),
output_tokens_details=OutputTokensDetails(
reasoning_tokens=getattr(
response_usage.completion_tokens_details, "reasoning_tokens", 0
) or 0
)
or 0
),
)
if response.usage
Expand Down
10 changes: 5 additions & 5 deletions src/agents/mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def create_streams(
tuple[
MemoryObjectReceiveStream[SessionMessage | Exception],
MemoryObjectSendStream[SessionMessage],
GetSessionIdCallback | None
GetSessionIdCallback | None,
]
]:
"""Create the streams for the server."""
Expand Down Expand Up @@ -243,7 +243,7 @@ def create_streams(
tuple[
MemoryObjectReceiveStream[SessionMessage | Exception],
MemoryObjectSendStream[SessionMessage],
GetSessionIdCallback | None
GetSessionIdCallback | None,
]
]:
"""Create the streams for the server."""
Expand Down Expand Up @@ -314,7 +314,7 @@ def create_streams(
tuple[
MemoryObjectReceiveStream[SessionMessage | Exception],
MemoryObjectSendStream[SessionMessage],
GetSessionIdCallback | None
GetSessionIdCallback | None,
]
]:
"""Create the streams for the server."""
Expand Down Expand Up @@ -394,7 +394,7 @@ def create_streams(
tuple[
MemoryObjectReceiveStream[SessionMessage | Exception],
MemoryObjectSendStream[SessionMessage],
GetSessionIdCallback | None
GetSessionIdCallback | None,
]
]:
"""Create the streams for the server."""
Expand All @@ -403,7 +403,7 @@ def create_streams(
headers=self.params.get("headers", None),
timeout=self.params.get("timeout", timedelta(seconds=30)),
sse_read_timeout=self.params.get("sse_read_timeout", timedelta(seconds=60 * 5)),
terminate_on_close=self.params.get("terminate_on_close", True)
terminate_on_close=self.params.get("terminate_on_close", True),
)

@property
Expand Down
1 change: 0 additions & 1 deletion src/agents/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,3 @@ def _cleanup_tasks(self):

def __str__(self) -> str:
return pretty_print_run_result_streaming(self)

15 changes: 6 additions & 9 deletions src/agents/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from __future__ import annotations

import asyncio
Expand Down Expand Up @@ -182,6 +181,8 @@ async def run(

try:
while True:
all_tools = await cls._get_all_tools(current_agent)

# Start an agent span if we don't have one. This span is ended if the current
# agent changes, or if the agent loop ends.
if current_span is None:
Expand All @@ -197,8 +198,6 @@ async def run(
output_type=output_type_name,
)
current_span.start(mark_as_current=True)

all_tools = await cls._get_all_tools(current_agent)
current_span.span_data.tools = [t.name for t in all_tools]

current_turn += 1
Expand All @@ -210,9 +209,7 @@ async def run(
data={"max_turns": max_turns},
),
)
raise MaxTurnsExceeded(
f"Max turns ({max_turns}) exceeded"
)
raise MaxTurnsExceeded(f"Max turns ({max_turns}) exceeded")

logger.debug(
f"Running agent {current_agent.name} (turn {current_turn})",
Expand Down Expand Up @@ -295,7 +292,7 @@ async def run(
last_agent=current_agent,
context_wrapper=context_wrapper,
input_guardrail_results=input_guardrail_results,
output_guardrail_results=[]
output_guardrail_results=[],
)
raise
finally:
Expand Down Expand Up @@ -528,6 +525,8 @@ async def _run_streamed_impl(
if streamed_result.is_complete:
break

all_tools = await cls._get_all_tools(current_agent)

# Start an agent span if we don't have one. This span is ended if the current
# agent changes, or if the agent loop ends.
if current_span is None:
Expand All @@ -543,8 +542,6 @@ async def _run_streamed_impl(
output_type=output_type_name,
)
current_span.start(mark_as_current=True)

all_tools = await cls._get_all_tools(current_agent)
tool_names = [t.name for t in all_tools]
current_span.span_data.tools = tool_names
current_turn += 1
Expand Down
2 changes: 2 additions & 0 deletions src/agents/voice/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
TTSVoice = Literal["alloy", "ash", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer"]
"""Exportable type for the TTSModelSettings voice enum"""


@dataclass
class TTSModelSettings:
"""Settings for a TTS model."""

voice: TTSVoice | None = None
"""
The voice to use for the TTS model. If not provided, the default voice for the respective model
Expand Down
60 changes: 39 additions & 21 deletions tests/mcp/test_mcp_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ async def test_mcp_tracing():
{
"workflow_name": "Agent workflow",
"children": [
{
"type": "mcp_tools",
"data": {"server": "fake_mcp_server", "result": ["test_tool_1"]},
},
{
"type": "agent",
"data": {
Expand All @@ -53,10 +57,6 @@ async def test_mcp_tracing():
"output_type": "str",
},
"children": [
{
"type": "mcp_tools",
"data": {"server": "fake_mcp_server", "result": ["test_tool_1"]},
},
{
"type": "function",
"data": {
Expand All @@ -66,8 +66,12 @@ async def test_mcp_tracing():
"mcp_data": {"server": "fake_mcp_server"},
},
},
{
"type": "mcp_tools",
"data": {"server": "fake_mcp_server", "result": ["test_tool_1"]},
},
],
}
},
],
}
]
Expand Down Expand Up @@ -100,6 +104,13 @@ async def test_mcp_tracing():
{
"workflow_name": "Agent workflow",
"children": [
{
"type": "mcp_tools",
"data": {
"server": "fake_mcp_server",
"result": ["test_tool_1", "test_tool_2"],
},
},
{
"type": "agent",
"data": {
Expand All @@ -109,13 +120,6 @@ async def test_mcp_tracing():
"output_type": "str",
},
"children": [
{
"type": "mcp_tools",
"data": {
"server": "fake_mcp_server",
"result": ["test_tool_1", "test_tool_2"],
},
},
{
"type": "function",
"data": {
Expand All @@ -133,8 +137,15 @@ async def test_mcp_tracing():
"mcp_data": {"server": "fake_mcp_server"},
},
},
{
"type": "mcp_tools",
"data": {
"server": "fake_mcp_server",
"result": ["test_tool_1", "test_tool_2"],
},
},
],
}
},
],
}
]
Expand Down Expand Up @@ -165,6 +176,13 @@ async def test_mcp_tracing():
{
"workflow_name": "Agent workflow",
"children": [
{
"type": "mcp_tools",
"data": {
"server": "fake_mcp_server",
"result": ["test_tool_1", "test_tool_2", "test_tool_3"],
},
},
{
"type": "agent",
"data": {
Expand All @@ -174,13 +192,6 @@ async def test_mcp_tracing():
"output_type": "str",
},
"children": [
{
"type": "mcp_tools",
"data": {
"server": "fake_mcp_server",
"result": ["test_tool_1", "test_tool_2", "test_tool_3"],
},
},
{
"type": "function",
"data": {
Expand All @@ -190,8 +201,15 @@ async def test_mcp_tracing():
"mcp_data": {"server": "fake_mcp_server"},
},
},
{
"type": "mcp_tools",
"data": {
"server": "fake_mcp_server",
"result": ["test_tool_1", "test_tool_2", "test_tool_3"],
},
},
],
}
},
],
}
]
Expand Down
3 changes: 1 addition & 2 deletions tests/models/test_litellm_extra_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ async def fake_acompletion(model, messages=None, **kwargs):

monkeypatch.setattr(litellm, "acompletion", fake_acompletion)
settings = ModelSettings(
temperature=0.1,
extra_body={"cached_content": "some_cache", "foo": 123}
temperature=0.1, extra_body={"cached_content": "some_cache", "foo": 123}
)
model = LitellmModel(model="test-model")

Expand Down
35 changes: 35 additions & 0 deletions tests/test_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,38 @@ async def test_previous_response_id_passed_between_runs_streamed_multi_turn():
pass

assert model.last_turn_args.get("previous_response_id") == "resp-stream-test"


@pytest.mark.asyncio
async def test_dynamic_tool_addition_run() -> None:
"""Test that tools can be added to an agent during a run."""
model = FakeModel()

executed: dict[str, bool] = {"called": False}

agent = Agent(name="test", model=model, tool_use_behavior="run_llm_again")

@function_tool(name_override="tool2")
def tool2() -> str:
executed["called"] = True
return "result2"

@function_tool(name_override="add_tool")
async def add_tool() -> str:
agent.tools.append(tool2)
return "added"

agent.tools.append(add_tool)

model.add_multiple_turn_outputs(
[
[get_function_tool_call("add_tool", json.dumps({}))],
[get_function_tool_call("tool2", json.dumps({}))],
[get_text_message("done")],
]
)

result = await Runner.run(agent, input="start")

assert executed["called"] is True
assert result.final_output == "done"
37 changes: 37 additions & 0 deletions tests/test_agent_runner_streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
RunContextWrapper,
Runner,
UserError,
function_tool,
handoff,
)
from agents.items import RunItem
Expand Down Expand Up @@ -684,3 +685,39 @@ async def test_streaming_events():
assert len(agent_data) == 2, "should have 2 agent updated events"
assert agent_data[0].new_agent == agent_2, "should have started with agent_2"
assert agent_data[1].new_agent == agent_1, "should have handed off to agent_1"


@pytest.mark.asyncio
async def test_dynamic_tool_addition_run_streamed() -> None:
model = FakeModel()

executed: dict[str, bool] = {"called": False}

agent = Agent(name="test", model=model, tool_use_behavior="run_llm_again")

@function_tool(name_override="tool2")
def tool2() -> str:
executed["called"] = True
return "result2"

@function_tool(name_override="add_tool")
async def add_tool() -> str:
agent.tools.append(tool2)
return "added"

agent.tools.append(add_tool)

model.add_multiple_turn_outputs(
[
[get_function_tool_call("add_tool", json.dumps({}))],
[get_function_tool_call("tool2", json.dumps({}))],
[get_text_message("done")],
]
)

result = Runner.run_streamed(agent, input="start")
async for _ in result.stream_events():
pass

assert executed["called"] is True
assert result.final_output == "done"
Loading