Skip to content

Commit 59c9174

Browse files
committed
feat: add convenience properties (tool_name, call_id) to ToolCallItem and ToolCallOutputItem
ToolApprovalItem already exposes tool_name and call_id as properties, but ToolCallItem and ToolCallOutputItem require users to reach into raw_item internals. This adds the same convenience accessors to both classes, following the existing ToolApprovalItem patterns. Closes #2886
1 parent 86739b1 commit 59c9174

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/agents/items.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,20 @@ class ToolCallItem(RunItemBase[Any]):
358358
title: str | None = None
359359
"""Optional short display label if known at item creation time."""
360360

361+
@property
362+
def tool_name(self) -> str | None:
363+
"""Return the tool name from the raw item, if available."""
364+
if isinstance(self.raw_item, dict):
365+
return self.raw_item.get("name")
366+
return getattr(self.raw_item, "name", None)
367+
368+
@property
369+
def call_id(self) -> str | None:
370+
"""Return the call identifier from the raw item, if available."""
371+
if isinstance(self.raw_item, dict):
372+
return self.raw_item.get("call_id") or self.raw_item.get("id")
373+
return getattr(self.raw_item, "call_id", None) or getattr(self.raw_item, "id", None)
374+
361375

362376
ToolCallOutputTypes: TypeAlias = Union[
363377
FunctionCallOutput,
@@ -382,6 +396,14 @@ class ToolCallOutputItem(RunItemBase[Any]):
382396

383397
type: Literal["tool_call_output_item"] = "tool_call_output_item"
384398

399+
@property
400+
def call_id(self) -> str | None:
401+
"""Return the call identifier from the raw item, if available."""
402+
if isinstance(self.raw_item, dict):
403+
cid = self.raw_item.get("call_id") or self.raw_item.get("id")
404+
return str(cid) if cid is not None else None
405+
return getattr(self.raw_item, "call_id", None) or getattr(self.raw_item, "id", None)
406+
385407
def to_input_item(self) -> TResponseInputItem:
386408
"""Converts the tool output into an input item for the next model turn.
387409

0 commit comments

Comments
 (0)