MCPcopy Index your code
hub / github.com/openai/openai-agents-python / RunContextWrapper

Class RunContextWrapper

src/agents/run_context.py:44–485  ·  view source on GitHub ↗

This wraps the context object that you passed to `Runner.run()`. It also contains information about the usage of the agent run so far. NOTE: Contexts are not passed to the LLM. They're a way to pass dependencies and data to code you implement, like tool functions, callbacks, hooks, etc.

Source from the content-addressed store, hash-verified

42
43@dataclass(eq=False)
44class RunContextWrapper(Generic[TContext]):
45 """This wraps the context object that you passed to `Runner.run()`. It also contains
46 information about the usage of the agent run so far.
47
48 NOTE: Contexts are not passed to the LLM. They're a way to pass dependencies and data to code
49 you implement, like tool functions, callbacks, hooks, etc.
50 """
51
52 context: TContext
53 """The context object (or None), passed by you to `Runner.run()`"""
54
55 usage: Usage = field(default_factory=Usage)
56 """The usage of the agent run so far. For streamed responses, the usage will be stale until the
57 last chunk of the stream is processed.
58 """
59
60 turn_input: list[TResponseInputItem] = field(default_factory=list)
61 _approvals: dict[str, _ApprovalRecord] = field(default_factory=dict)
62 tool_input: Any | None = None
63 """Structured input for the current agent tool run, when available."""
64
65 @staticmethod
66 def _to_str_or_none(value: Any) -> str | None:
67 if isinstance(value, str):
68 return value
69 if value is not None:
70 try:
71 return str(value)
72 except Exception:
73 return None
74 return None
75
76 @staticmethod
77 def _resolve_tool_name(approval_item: ToolApprovalItem) -> str:
78 raw = approval_item.raw_item
79 if approval_item.tool_name:
80 return approval_item.tool_name
81 candidate: Any | None
82 if isinstance(raw, dict):
83 candidate = raw.get("name") or raw.get("type")
84 else:
85 candidate = getattr(raw, "name", None) or getattr(raw, "type", None)
86 return RunContextWrapper._to_str_or_none(candidate) or "unknown_tool"
87
88 @staticmethod
89 def _resolve_tool_namespace(approval_item: ToolApprovalItem) -> str | None:
90 raw = approval_item.raw_item
91 if isinstance(approval_item.tool_namespace, str) and approval_item.tool_namespace:
92 return approval_item.tool_namespace
93 if isinstance(raw, dict):
94 candidate = raw.get("namespace")
95 else:
96 candidate = getattr(raw, "namespace", None)
97 return RunContextWrapper._to_str_or_none(candidate)
98
99 @staticmethod
100 def _resolve_approval_key(approval_item: ToolApprovalItem) -> str:
101 tool_name = RunContextWrapper._resolve_tool_name(approval_item)

Calls

no outgoing calls