Skip to content

Commit a53d6bd

Browse files
committed
Revert "Add agent to ToolContext calls"
This reverts commit 7722970.
1 parent 5a43ea7 commit a53d6bd

8 files changed

Lines changed: 1 addition & 69 deletions

File tree

src/agents/agent.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ async def _run_agent_impl(context: ToolContext, input_json: str) -> Any:
599599
tool_call_id=context.tool_call_id,
600600
tool_arguments=context.tool_arguments,
601601
tool_call=context.tool_call,
602-
agent=context.agent,
603602
)
604603
if should_capture_tool_input:
605604
nested_context.tool_input = params_data

src/agents/realtime/session.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ async def _handle_tool_call(
600600
tool_name=event.name,
601601
tool_call_id=event.call_id,
602602
tool_arguments=event.arguments,
603-
agent=agent,
604603
)
605604
result = await func_tool.on_invoke_tool(tool_context, event.arguments)
606605

@@ -627,7 +626,6 @@ async def _handle_tool_call(
627626
tool_name=event.name,
628627
tool_call_id=event.call_id,
629628
tool_arguments=event.arguments,
630-
agent=agent,
631629
)
632630

633631
# Execute the handoff to get the new agent

src/agents/run_internal/tool_execution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,6 @@ async def run_single_tool(func_tool: FunctionTool, tool_call: ResponseFunctionTo
820820
context_wrapper,
821821
tool_call.call_id,
822822
tool_call=tool_call,
823-
agent=agent,
824823
)
825824
agent_hooks = agent.hooks
826825
if config.trace_include_sensitive_data:

src/agents/tool_context.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from .usage import Usage
1010

1111
if TYPE_CHECKING:
12-
from .agent import AgentBase
1312
from .items import TResponseInputItem
1413
from .run_context import _ApprovalRecord
1514

@@ -45,9 +44,6 @@ class ToolContext(RunContextWrapper[TContext]):
4544
tool_call: ResponseFunctionToolCall | None = None
4645
"""The tool call object associated with this invocation."""
4746

48-
agent: AgentBase[Any] | None = None
49-
"""The active agent for this tool call, when available."""
50-
5147
def __init__(
5248
self,
5349
context: TContext,
@@ -56,7 +52,6 @@ def __init__(
5652
tool_call_id: str | object = _MISSING,
5753
tool_arguments: str | object = _MISSING,
5854
tool_call: ResponseFunctionToolCall | None = None,
59-
agent: AgentBase[Any] | None = None,
6055
*,
6156
turn_input: list[TResponseInputItem] | None = None,
6257
_approvals: dict[str, _ApprovalRecord] | None = None,
@@ -85,15 +80,13 @@ def __init__(
8580
else cast(str, tool_call_id)
8681
)
8782
self.tool_call = tool_call
88-
self.agent = agent
8983

9084
@classmethod
9185
def from_agent_context(
9286
cls,
9387
context: RunContextWrapper[TContext],
9488
tool_call_id: str,
9589
tool_call: ResponseFunctionToolCall | None = None,
96-
agent: AgentBase[Any] | None = None,
9790
) -> ToolContext:
9891
"""
9992
Create a ToolContext from a RunContextWrapper.
@@ -106,16 +99,12 @@ def from_agent_context(
10699
tool_args = (
107100
tool_call.arguments if tool_call is not None else _assert_must_pass_tool_arguments()
108101
)
109-
tool_agent = agent
110-
if tool_agent is None and isinstance(context, ToolContext):
111-
tool_agent = context.agent
112102

113103
tool_context = cls(
114104
tool_name=tool_name,
115105
tool_call_id=tool_call_id,
116106
tool_arguments=tool_args,
117107
tool_call=tool_call,
118-
agent=tool_agent,
119108
**base_values,
120109
)
121110
return tool_context

tests/realtime/test_session.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,6 @@ async def test_function_tool_execution_success(
989989
call_args = mock_function_tool.on_invoke_tool.call_args
990990
tool_context = call_args[0][0]
991991
assert isinstance(tool_context, ToolContext)
992-
assert tool_context.agent == mock_agent
993992
assert call_args[0][1] == '{"param": "value"}'
994993

995994
# Verify tool output was sent to model

tests/test_agent_runner.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
from agents.run_internal.tool_use_tracker import AgentToolUseTracker
6464
from agents.run_state import RunState
6565
from agents.tool import ComputerTool, FunctionToolResult, function_tool
66-
from agents.tool_context import ToolContext
6766
from agents.usage import Usage
6867

6968
from .fake_model import FakeModel
@@ -438,36 +437,6 @@ async def test_tool_call_runs():
438437
)
439438

440439

441-
@pytest.mark.asyncio
442-
async def test_tool_call_context_includes_current_agent() -> None:
443-
model = FakeModel()
444-
captured_contexts: list[ToolContext[Any]] = []
445-
446-
@function_tool(name_override="foo")
447-
def foo(context: ToolContext[Any]) -> str:
448-
captured_contexts.append(context)
449-
return "tool_result"
450-
451-
agent = Agent(
452-
name="test",
453-
model=model,
454-
tools=[foo],
455-
)
456-
457-
model.add_multiple_turn_outputs(
458-
[
459-
[get_function_tool_call("foo", "{}")],
460-
[get_text_message("done")],
461-
]
462-
)
463-
464-
result = await Runner.run(agent, input="user_message")
465-
466-
assert result.final_output == "done"
467-
assert len(captured_contexts) == 1
468-
assert captured_contexts[0].agent is agent
469-
470-
471440
@pytest.mark.asyncio
472441
async def test_handoffs():
473442
model = FakeModel()

tests/test_source_compat_constructors.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ def test_tool_context_v070_positional_constructor_still_works() -> None:
8383
assert context.tool_name == "tool_name"
8484
assert context.tool_call_id == "call_id"
8585
assert context.tool_arguments == '{"x":1}'
86-
assert context.agent is None
87-
88-
89-
def test_tool_context_supports_appended_agent_positional_argument() -> None:
90-
usage = Usage()
91-
agent = Agent(name="agent")
92-
context = ToolContext(None, usage, "tool_name", "call_id", '{"x":1}', None, agent)
93-
94-
assert context.usage is usage
95-
assert context.tool_name == "tool_name"
96-
assert context.tool_call_id == "call_id"
97-
assert context.tool_arguments == '{"x":1}'
98-
assert context.agent is agent
9986

10087

10188
def test_run_result_v070_positional_constructor_still_works() -> None:

tests/test_tool_context.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from openai.types.responses import ResponseFunctionToolCall
33

4-
from agents import Agent
54
from agents.run_context import RunContextWrapper
65
from agents.tool_context import ToolContext
76
from tests.utils.hitl import make_context_wrapper
@@ -31,16 +30,9 @@ def test_tool_context_from_agent_context_populates_fields() -> None:
3130
arguments='{"a": 1}',
3231
)
3332
ctx = make_context_wrapper()
34-
agent = Agent(name="agent")
3533

36-
tool_ctx = ToolContext.from_agent_context(
37-
ctx,
38-
tool_call_id="call-123",
39-
tool_call=tool_call,
40-
agent=agent,
41-
)
34+
tool_ctx = ToolContext.from_agent_context(ctx, tool_call_id="call-123", tool_call=tool_call)
4235

4336
assert tool_ctx.tool_name == "test_tool"
4437
assert tool_ctx.tool_call_id == "call-123"
4538
assert tool_ctx.tool_arguments == '{"a": 1}'
46-
assert tool_ctx.agent is agent

0 commit comments

Comments
 (0)