Tool Context
ToolContext
dataclass
Bases: RunContextWrapper[TContext]
The context of a tool call.
Source code in src/agents/tool_context.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
tool_name
class-attribute
instance-attribute
tool_name: str = (
_assert_must_pass_tool_name()
if tool_name is _MISSING
else cast(str, tool_name)
)
The name of the tool being invoked.
tool_arguments
class-attribute
instance-attribute
tool_arguments: str = (
_assert_must_pass_tool_arguments()
if tool_arguments is _MISSING
else cast(str, tool_arguments)
)
The raw arguments string of the tool call.
tool_call_id
class-attribute
instance-attribute
tool_call_id: str = (
_assert_must_pass_tool_call_id()
if tool_call_id is _MISSING
else cast(str, tool_call_id)
)
The ID of the tool call.
tool_call
class-attribute
instance-attribute
The tool call object associated with this invocation.
agent
class-attribute
instance-attribute
agent: AgentBase[Any] | None = agent
The active agent for this tool call, when available.
run_config
class-attribute
instance-attribute
run_config: RunConfig | None = run_config
The active run config for this tool call, when available.
context
instance-attribute
The context object (or None), passed by you to Runner.run()
usage
class-attribute
instance-attribute
The usage of the agent run so far. For streamed responses, the usage will be stale until the last chunk of the stream is processed.
tool_input
class-attribute
instance-attribute
Structured input for the current agent tool run, when available.
__init__
__init__(
context: TContext,
usage: Usage | object = _MISSING,
tool_name: str | object = _MISSING,
tool_call_id: str | object = _MISSING,
tool_arguments: str | object = _MISSING,
tool_call: ResponseFunctionToolCall | None = None,
*,
agent: AgentBase[Any] | None = None,
run_config: RunConfig | None = None,
turn_input: list[TResponseInputItem] | None = None,
_approvals: dict[str, _ApprovalRecord] | None = None,
tool_input: Any | None = None,
) -> None
Preserve the v0.7 positional constructor while accepting new context fields.
Source code in src/agents/tool_context.py
from_agent_context
classmethod
from_agent_context(
context: RunContextWrapper[TContext],
tool_call_id: str,
tool_call: ResponseFunctionToolCall | None = None,
agent: AgentBase[Any] | None = None,
*,
run_config: RunConfig | None = None,
) -> ToolContext
Create a ToolContext from a RunContextWrapper.
Source code in src/agents/tool_context.py
is_tool_approved
Return True/False/None for the given tool call.
Source code in src/agents/run_context.py
approve_tool
approve_tool(
approval_item: ToolApprovalItem,
always_approve: bool = False,
) -> None
Approve a tool call, optionally for all future calls.
Source code in src/agents/run_context.py
reject_tool
reject_tool(
approval_item: ToolApprovalItem,
always_reject: bool = False,
) -> None
Reject a tool call, optionally for all future calls.
Source code in src/agents/run_context.py
get_approval_status
get_approval_status(
tool_name: str,
call_id: str,
*,
existing_pending: ToolApprovalItem | None = None,
) -> bool | None
Return approval status, retrying with pending item's tool name if necessary.