Items
Item utilities for the run pipeline. Hosts input normalization helpers and lightweight builders for synthetic run items or IDs used during tool execution. Internal use only.
NestedHistoryOwnedItem
dataclass
A run item and the exact nested-input occurrence that represents it.
Source code in src/agents/run_internal/items.py
NestedHistoryOwnedItemRef
dataclass
Durable coordinates plus the live object for one owned session occurrence.
Source code in src/agents/run_internal/items.py
nested_history_run_item_occurrence_key
nested_history_run_item_occurrence_key(
run_item: RunItem | None,
) -> str | None
Return the private copy-lineage key for a run item, when one exists.
Source code in src/agents/run_internal/items.py
ensure_nested_history_run_item_occurrence_key
ensure_nested_history_run_item_occurrence_key(
run_item: RunItem,
) -> str
Bind an ephemeral key that survives object copies but never enters model payloads.
Source code in src/agents/run_internal/items.py
copy_input_items
copy_input_items(
value: str | list[TResponseInputItem],
) -> str | list[TResponseInputItem]
Return a shallow copy of input items so mutations do not leak between turns.
run_item_to_input_item
run_item_to_input_item(
run_item: RunItem,
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
) -> TResponseInputItem | None
Convert a run item to model input, optionally stripping reasoning IDs.
Source code in src/agents/run_internal/items.py
run_items_to_input_items
run_items_to_input_items(
run_items: Sequence[RunItem],
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
) -> list[TResponseInputItem]
Convert run items to model input items while skipping approvals.
Source code in src/agents/run_internal/items.py
drop_orphan_function_calls
drop_orphan_function_calls(
items: list[TResponseInputItem],
*,
pruning_indexes: set[int] | None = None,
) -> list[TResponseInputItem]
Remove tool and program call items that do not have corresponding outputs so resumptions or
retries do not replay stale calls. Program-owned items are removed with an orphan program,
while programs with retained hosted calls or tool outputs remain available for continuation.
Reasoning items that immediately precede a call dropped by this pass are also removed, since
the Responses API rejects reasoning items that are not followed by their associated
model-emitted item (Item 'rs_...' of type 'reasoning' was provided without its required
following item).
Source code in src/agents/run_internal/items.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
ensure_input_item_format
ensure_input_item_format(
item: TResponseInputItem,
) -> TResponseInputItem
Ensure a single item is normalized for model input.
Source code in src/agents/run_internal/items.py
normalize_input_items_for_api
normalize_input_items_for_api(
items: list[TResponseInputItem],
) -> list[TResponseInputItem]
Normalize input items for API submission.
Source code in src/agents/run_internal/items.py
prepare_model_input_items
prepare_model_input_items(
caller_items: Sequence[TResponseInputItem],
generated_items: Sequence[TResponseInputItem] = (),
) -> list[TResponseInputItem]
Normalize model input while pruning orphans only from runner-generated history.
Source code in src/agents/run_internal/items.py
normalize_resumed_input
normalize_resumed_input(
raw_input: str | list[TResponseInputItem],
) -> str | list[TResponseInputItem]
Normalize resumed list inputs and drop orphan tool calls.
Source code in src/agents/run_internal/items.py
fingerprint_input_item
Hashable fingerprint used to dedupe or rewind input items across resumes.
Source code in src/agents/run_internal/items.py
digest_input_item
Return a fixed-size digest of an input item for durable occurrence tracking.
Source code in src/agents/run_internal/items.py
filter_nested_history_owned_item_refs_for_input
filter_nested_history_owned_item_refs_for_input(
input: str | Sequence[TResponseInputItem],
owned_item_refs: Sequence[NestedHistoryOwnedItemRef],
) -> list[NestedHistoryOwnedItemRef]
Keep ownership whose exact clean input occurrence is still present.
Source code in src/agents/run_internal/items.py
reconcile_nested_history_owned_input_after_rewrite
reconcile_nested_history_owned_input_after_rewrite(
previous_input: str | Sequence[TResponseInputItem],
rewritten_input: str | Sequence[TResponseInputItem],
owned_item_refs: Sequence[NestedHistoryOwnedItemRef],
) -> tuple[
str | list[TResponseInputItem],
list[NestedHistoryOwnedItemRef],
]
Rebind ownership after an unambiguous input rewrite.
Source code in src/agents/run_internal/items.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
resolve_nested_history_owned_item_indexes
resolve_nested_history_owned_item_indexes(
run_items: Sequence[RunItem],
owned_item_refs: Sequence[NestedHistoryOwnedItemRef],
) -> set[int]
Resolve ownership references without dropping a different item after list mutation.
Source code in src/agents/run_internal/items.py
rebase_nested_history_owned_item_refs
rebase_nested_history_owned_item_refs(
input: str | Sequence[TResponseInputItem],
run_items: Sequence[RunItem],
owned_item_refs: Sequence[NestedHistoryOwnedItemRef],
) -> list[NestedHistoryOwnedItemRef]
Rebase surviving ownership onto exact live input and session occurrences.
Source code in src/agents/run_internal/items.py
strip_internal_input_item_metadata
strip_internal_input_item_metadata(
item: TResponseInputItem,
) -> TResponseInputItem
Remove SDK-only session metadata before sending items back to the model.
Source code in src/agents/run_internal/items.py
deduplicate_input_items
deduplicate_input_items(
items: Sequence[TResponseInputItem],
) -> list[TResponseInputItem]
Remove duplicate items that share stable identifiers to avoid re-sending tool outputs.
Source code in src/agents/run_internal/items.py
deduplicate_input_items_preferring_latest
deduplicate_input_items_preferring_latest(
items: Sequence[TResponseInputItem],
) -> list[TResponseInputItem]
Deduplicate by stable identifiers while keeping the latest occurrence.
Source code in src/agents/run_internal/items.py
function_tool_error_output
function_tool_error_output(
tool_call: Any,
output: Any,
*,
output_json_schema: dict[str, Any] | None,
) -> Any
Encode SDK-generated programmatic tool errors as provider-compatible JSON objects.
Source code in src/agents/run_internal/items.py
function_rejection_item
function_rejection_item(
agent: Any,
tool_call: Any,
*,
rejection_message: str = REJECTION_MESSAGE,
output_json_schema: dict[str, Any] | None = None,
scope_id: str | None = None,
tool_origin: Any = None,
) -> ToolCallOutputItem
Build a ToolCallOutputItem representing a rejected function tool call.
Source code in src/agents/run_internal/items.py
shell_rejection_item
shell_rejection_item(
agent: Any,
call_id: str,
*,
tool_call: Any | None = None,
rejection_message: str = REJECTION_MESSAGE,
) -> ToolCallOutputItem
Build a ToolCallOutputItem representing a rejected shell call.
Source code in src/agents/run_internal/items.py
apply_patch_rejection_item
apply_patch_rejection_item(
agent: Any,
call_id: str,
*,
tool_call: Any | None = None,
output_type: Literal[
"apply_patch_call_output", "custom_tool_call_output"
] = "apply_patch_call_output",
rejection_message: str = REJECTION_MESSAGE,
) -> ToolCallOutputItem
Build a ToolCallOutputItem representing a rejected apply_patch call.
Source code in src/agents/run_internal/items.py
extract_mcp_request_id
Pull the request id from hosted MCP approval payloads.
Source code in src/agents/run_internal/items.py
extract_mcp_request_id_from_run
Extract the hosted MCP request id from a streaming run item.