Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Tool Context

ToolContextdataclass

Bases:RunContextWrapper[TContext]

The context of a tool call.

Source code insrc/agents/tool_context.py
@dataclassclassToolContext(RunContextWrapper[TContext]):"""The context of a tool call."""tool_name:str=field(default_factory=_assert_must_pass_tool_name)"""The name of the tool being invoked."""tool_call_id:str=field(default_factory=_assert_must_pass_tool_call_id)"""The ID of the tool call."""tool_arguments:str=field(default_factory=_assert_must_pass_tool_arguments)"""The raw arguments string of the tool call."""tool_call:ResponseFunctionToolCall|None=None"""The tool call object associated with this invocation."""def__init__(self,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,*,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."""resolved_usage=Usage()ifusageis_MISSINGelsecast(Usage,usage)super().__init__(context=context,usage=resolved_usage,turn_input=list(turn_inputor[]),_approvals={}if_approvalsisNoneelse_approvals,tool_input=tool_input,)self.tool_name=(_assert_must_pass_tool_name()iftool_nameis_MISSINGelsecast(str,tool_name))self.tool_arguments=(_assert_must_pass_tool_arguments()iftool_argumentsis_MISSINGelsecast(str,tool_arguments))self.tool_call_id=(_assert_must_pass_tool_call_id()iftool_call_idis_MISSINGelsecast(str,tool_call_id))self.tool_call=tool_call@classmethoddeffrom_agent_context(cls,context:RunContextWrapper[TContext],tool_call_id:str,tool_call:ResponseFunctionToolCall|None=None,)->ToolContext:"""        Create a ToolContext from a RunContextWrapper.        """# Grab the names of the RunContextWrapper's init=True fieldsbase_values:dict[str,Any]={f.name:getattr(context,f.name)forfinfields(RunContextWrapper)iff.init}tool_name=tool_call.nameiftool_callisnotNoneelse_assert_must_pass_tool_name()tool_args=(tool_call.argumentsiftool_callisnotNoneelse_assert_must_pass_tool_arguments())tool_context=cls(tool_name=tool_name,tool_call_id=tool_call_id,tool_arguments=tool_args,tool_call=tool_call,**base_values,)returntool_context

tool_nameclass-attributeinstance-attribute

tool_name:str=(_assert_must_pass_tool_name()iftool_nameis_MISSINGelsecast(str,tool_name))

The name of the tool being invoked.

tool_argumentsclass-attributeinstance-attribute

tool_arguments:str=(_assert_must_pass_tool_arguments()iftool_argumentsis_MISSINGelsecast(str,tool_arguments))

The raw arguments string of the tool call.

tool_call_idclass-attributeinstance-attribute

tool_call_id:str=(_assert_must_pass_tool_call_id()iftool_call_idis_MISSINGelsecast(str,tool_call_id))

The ID of the tool call.

tool_callclass-attributeinstance-attribute

tool_call:ResponseFunctionToolCall|None=tool_call

The tool call object associated with this invocation.

contextinstance-attribute

context:TContext

The context object (or None), passed by you toRunner.run()

usageclass-attributeinstance-attribute

usage:Usage=field(default_factory=Usage)

The usage of the agent run so far. For streamed responses, the usage will be stale until thelast chunk of the stream is processed.

tool_inputclass-attributeinstance-attribute

tool_input:Any|None=None

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,*,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 insrc/agents/tool_context.py
def__init__(self,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,*,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."""resolved_usage=Usage()ifusageis_MISSINGelsecast(Usage,usage)super().__init__(context=context,usage=resolved_usage,turn_input=list(turn_inputor[]),_approvals={}if_approvalsisNoneelse_approvals,tool_input=tool_input,)self.tool_name=(_assert_must_pass_tool_name()iftool_nameis_MISSINGelsecast(str,tool_name))self.tool_arguments=(_assert_must_pass_tool_arguments()iftool_argumentsis_MISSINGelsecast(str,tool_arguments))self.tool_call_id=(_assert_must_pass_tool_call_id()iftool_call_idis_MISSINGelsecast(str,tool_call_id))self.tool_call=tool_call

from_agent_contextclassmethod

from_agent_context(context:RunContextWrapper[TContext],tool_call_id:str,tool_call:ResponseFunctionToolCall|None=None,)->ToolContext

Create a ToolContext from a RunContextWrapper.

Source code insrc/agents/tool_context.py
@classmethoddeffrom_agent_context(cls,context:RunContextWrapper[TContext],tool_call_id:str,tool_call:ResponseFunctionToolCall|None=None,)->ToolContext:"""    Create a ToolContext from a RunContextWrapper.    """# Grab the names of the RunContextWrapper's init=True fieldsbase_values:dict[str,Any]={f.name:getattr(context,f.name)forfinfields(RunContextWrapper)iff.init}tool_name=tool_call.nameiftool_callisnotNoneelse_assert_must_pass_tool_name()tool_args=(tool_call.argumentsiftool_callisnotNoneelse_assert_must_pass_tool_arguments())tool_context=cls(tool_name=tool_name,tool_call_id=tool_call_id,tool_arguments=tool_args,tool_call=tool_call,**base_values,)returntool_context

is_tool_approved

is_tool_approved(tool_name:str,call_id:str)->bool|None

Return True/False/None for the given tool call.

Source code insrc/agents/run_context.py
defis_tool_approved(self,tool_name:str,call_id:str)->bool|None:"""Return True/False/None for the given tool call."""approval_entry=self._approvals.get(tool_name)ifnotapproval_entry:returnNone# Check for permanent approval/rejectionifapproval_entry.approvedisTrueandapproval_entry.rejectedisTrue:# Approval takes precedencereturnTrueifapproval_entry.approvedisTrue:returnTrueifapproval_entry.rejectedisTrue:returnFalseapproved_ids=(set(approval_entry.approved)ifisinstance(approval_entry.approved,list)elseset())rejected_ids=(set(approval_entry.rejected)ifisinstance(approval_entry.rejected,list)elseset())ifcall_idinapproved_ids:returnTrueifcall_idinrejected_ids:returnFalse# Per-call approvals are scoped to the exact call ID, so other calls require a new decision.returnNone

approve_tool

approve_tool(approval_item:ToolApprovalItem,always_approve:bool=False,)->None

Approve a tool call, optionally for all future calls.

Source code insrc/agents/run_context.py
defapprove_tool(self,approval_item:ToolApprovalItem,always_approve:bool=False)->None:"""Approve a tool call, optionally for all future calls."""self._apply_approval_decision(approval_item,always=always_approve,approve=True,)

reject_tool

reject_tool(approval_item:ToolApprovalItem,always_reject:bool=False,)->None

Reject a tool call, optionally for all future calls.

Source code insrc/agents/run_context.py
defreject_tool(self,approval_item:ToolApprovalItem,always_reject:bool=False)->None:"""Reject a tool call, optionally for all future calls."""self._apply_approval_decision(approval_item,always=always_reject,approve=False,)

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.

Source code insrc/agents/run_context.py
defget_approval_status(self,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."""status=self.is_tool_approved(tool_name,call_id)ifstatusisNoneandexisting_pending:fallback_tool_name=self._resolve_tool_name(existing_pending)status=self.is_tool_approved(fallback_tool_name,call_id)returnstatus

[8]ページ先頭

©2009-2026 Movatter.jp