콘텐츠로 이동

Run State

RunState class for serializing and resuming agent runs with human-in-the-loop support.

RunState dataclass

Bases: Generic[TContext, TAgent]

Serializable snapshot of an agent run, including context, usage, and interruptions.

RunState is the durable pause/resume boundary for human-in-the-loop flows. It stores enough information to continue an interrupted run, including model responses, generated items, approval state, and optional server-managed conversation identifiers.

Context serialization is intentionally conservative:

  • Mapping contexts round-trip directly.
  • Custom contexts may require a serializer and deserializer.
  • When no safe serializer is available, the snapshot is still written but emits warnings and records metadata describing what is required to rebuild the original context type.
ソースコード位置: src/agents/run_state.py
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
@dataclass
class RunState(Generic[TContext, TAgent]):
    """Serializable snapshot of an agent run, including context, usage, and interruptions.

    ``RunState`` is the durable pause/resume boundary for human-in-the-loop flows. It stores
    enough information to continue an interrupted run, including model responses, generated
    items, approval state, and optional server-managed conversation identifiers.

    Context serialization is intentionally conservative:

    - Mapping contexts round-trip directly.
    - Custom contexts may require a serializer and deserializer.
    - When no safe serializer is available, the snapshot is still written but emits warnings and
      records metadata describing what is required to rebuild the original context type.
    """

    _current_turn: int = 0
    """Current turn number in the conversation."""

    _current_agent: TAgent | None = None
    """The agent currently handling the conversation."""

    _starting_agent: TAgent | None = field(default=None, repr=False)
    """The root agent used to derive stable duplicate-name identities during resume."""

    _original_input: str | list[Any] = field(default_factory=list)
    """Original user input prior to any processing."""

    _model_responses: list[ModelResponse] = field(default_factory=list)
    """Responses from the model so far."""

    _context: RunContextWrapper[TContext] | None = None
    """Run context tracking approvals, usage, and other metadata."""

    _generated_items: list[RunItem] = field(default_factory=list)
    """Items used to build model input when resuming; may be filtered by handoffs."""

    _session_items: list[RunItem] = field(default_factory=list)
    """Full, unfiltered run items for session history."""

    _pending_input: list[TResponseInputItem] = field(default_factory=list)
    """Input staged for admission immediately before the next resumed model call."""

    _nested_history_owned_session_item_refs: list[NestedHistoryOwnedItemRef] = field(
        default_factory=list
    )
    """Session-item occurrences also present verbatim in SDK-default nested input history."""

    _max_turns: int | None = 10
    """Maximum allowed turns before forcing termination, or ``None`` for no limit."""

    _conversation_id: str | None = None
    """Conversation identifier for server-managed conversation tracking."""

    _previous_response_id: str | None = None
    """Response identifier of the last server-managed response."""

    _auto_previous_response_id: bool = False
    """Whether the previous response id should be automatically tracked."""

    _generated_prompt_cache_key: str | None = None
    """SDK-generated prompt cache key to preserve across resume flows."""

    _reasoning_item_id_policy: Literal["preserve", "omit"] | None = None
    """How reasoning item IDs are represented in next-turn model input."""

    _input_guardrail_results: list[InputGuardrailResult] = field(default_factory=list)
    """Results from input guardrails applied to the run."""

    _output_guardrail_results: list[OutputGuardrailResult] = field(default_factory=list)
    """Results from output guardrails applied to the run."""

    _tool_input_guardrail_results: list[ToolInputGuardrailResult] = field(default_factory=list)
    """Results from tool input guardrails applied during the run."""

    _tool_output_guardrail_results: list[ToolOutputGuardrailResult] = field(default_factory=list)
    """Results from tool output guardrails applied during the run."""

    _current_step: NextStepInterruption | NextStepRunAgain | None = None
    """Current resumable step, or ``None`` when the state is terminal."""

    _last_processed_response: ProcessedResponse | None = None
    """The last processed model response. This is needed for resuming from interruptions."""

    _generated_items_last_processed_marker: str | None = field(default=None, repr=False)
    """Tracks whether _generated_items already include the current last_processed_response."""

    _current_turn_persisted_item_count: int = 0
    """Tracks how many items from this turn were already written to the session."""

    _tool_use_tracker_snapshot: dict[str, list[str]] = field(default_factory=dict)
    """Serialized snapshot of the AgentToolUseTracker (agent name -> tools used)."""

    _trace_state: TraceState | None = field(default=None, repr=False)
    """Serialized trace metadata for resuming tracing context."""

    _agent_tool_state_scope_id: str | None = field(default=None, repr=False)
    """Private scope id used to isolate agent-tool pending state per RunState instance."""

    _sandbox: dict[str, Any] | None = field(default=None, repr=False)
    """Serialized sandbox resume payload for sandbox-aware runs."""

    _schema_version: str = field(default=CURRENT_SCHEMA_VERSION, repr=False)
    """Schema version the snapshot was loaded from for schema-gated resume compatibility."""

    def __init__(
        self,
        context: RunContextWrapper[TContext],
        original_input: str | list[Any],
        starting_agent: TAgent,
        max_turns: int | None = 10,
        *,
        conversation_id: str | None = None,
        previous_response_id: str | None = None,
        auto_previous_response_id: bool = False,
    ):
        """Initialize a new RunState."""
        self._context = context
        self._original_input = _clone_original_input(original_input)
        self._starting_agent = starting_agent
        self._current_agent = starting_agent
        self._max_turns = max_turns
        self._conversation_id = conversation_id
        self._previous_response_id = previous_response_id
        self._auto_previous_response_id = auto_previous_response_id
        self._generated_prompt_cache_key = None
        self._reasoning_item_id_policy = None
        self._model_responses = []
        self._generated_items = []
        self._session_items = []
        self._pending_input = []
        self._nested_history_owned_session_item_refs = []
        self._input_guardrail_results = []
        self._output_guardrail_results = []
        self._tool_input_guardrail_results = []
        self._tool_output_guardrail_results = []
        self._current_step = None
        self._current_turn = 0
        self._last_processed_response = None
        self._generated_items_last_processed_marker = None
        self._current_turn_persisted_item_count = 0
        self._tool_use_tracker_snapshot = {}
        self._trace_state = None
        self._sandbox = None
        self._schema_version = CURRENT_SCHEMA_VERSION
        from .agent_tool_state import get_agent_tool_state_scope

        self._agent_tool_state_scope_id = get_agent_tool_state_scope(context)

    def _copy_for_result_checkpoint(self) -> RunState[TContext, TAgent]:
        """Copy SDK-owned decision state when nesting this checkpoint in a result snapshot."""
        copied = copy.copy(self)
        if self._context is None:
            return copied
        copied._context = self._context._copy_for_run_state()
        from .agent_tool_state import (
            get_agent_tool_resume_state,
            get_agent_tool_state_scope,
            peek_agent_tool_run_result,
            record_agent_tool_resume_state,
        )

        copied._agent_tool_state_scope_id = get_agent_tool_state_scope(copied._context)
        if self._last_processed_response is None:
            return copied

        for function_run in self._last_processed_response.functions:
            pending_result = peek_agent_tool_run_result(
                function_run.tool_call,
                scope_id=self._agent_tool_state_scope_id,
            )
            interruptions = getattr(pending_result, "interruptions", None)
            to_state = getattr(pending_result, "to_state", None)
            if not isinstance(interruptions, list) or not interruptions or not callable(to_state):
                continue
            pending_state = get_agent_tool_resume_state(pending_result)
            copy_for_checkpoint = getattr(pending_state, "_copy_for_result_checkpoint", None)
            nested_state = copy_for_checkpoint() if callable(copy_for_checkpoint) else to_state()
            if not isinstance(nested_state, RunState) or nested_state is self:
                continue
            record_agent_tool_resume_state(
                function_run.tool_call,
                nested_state,
                scope_id=copied._agent_tool_state_scope_id,
                approval_items=interruptions,
            )
        return copied

    @property
    def pending_input(self) -> list[TResponseInputItem]:
        """Return a copy of input currently staged for the next resumed model call."""
        return copy.deepcopy(self._pending_input)

    def add_input(self, input: str | list[TResponseInputItem]) -> None:
        """Stage input for admission immediately before the next resumed model call.

        String input is normalized to a user message. Multiple calls preserve insertion order.
        The input remains pending until its guardrails and conversation ownership boundary accept
        it. Terminal states reject new input before mutating the state.
        """
        from .run_internal.run_steps import NextStepInterruption, NextStepRunAgain

        if not isinstance(self._current_step, NextStepInterruption | NextStepRunAgain):
            raise UserError("Cannot add input to a terminal RunState")
        if self._max_turns is not None and self._current_turn >= self._max_turns:
            raise UserError("Cannot add input to a RunState with no remaining model turns")
        if isinstance(self._current_step, NextStepInterruption):
            if self._current_step.response_accepted:
                raise UserError(
                    "Cannot add input while an accepted model response is awaiting local processing"
                )
            if self._current_agent is None:
                raise UserError("Cannot add input to a RunState without a current agent")
            tool_use_behavior = self._current_agent.tool_use_behavior
            interrupted_tool_names = {
                item.tool_name
                for item in self._current_step.interruptions
                if item.tool_name is not None
            }
            stops_before_next_model = tool_use_behavior == "stop_on_first_tool" or (
                isinstance(tool_use_behavior, dict)
                and bool(
                    interrupted_tool_names & set(tool_use_behavior.get("stop_at_tool_names", []))
                )
            )
            if stops_before_next_model or callable(tool_use_behavior):
                raise UserError(
                    "Cannot add input to an interrupted RunState whose tool result may end the run"
                )

        normalized = ItemHelpers.input_to_new_input_list(input)
        self._pending_input.extend(copy.deepcopy(normalized))

    def clear_pending_input(self) -> None:
        """Remove all input staged for the next resumed model call."""
        self._pending_input = []

    def get_interruptions(self) -> list[ToolApprovalItem]:
        """Return detached copies of pending interruptions for the current step."""
        # Import at runtime to avoid circular import
        from .run_internal.run_steps import NextStepInterruption

        if self._current_step is None or not isinstance(self._current_step, NextStepInterruption):
            return []
        copy_error: UserError | None = None
        try:
            interruptions: list[ToolApprovalItem] = []
            for item in self._current_step.interruptions:
                copied_raw_item = _copy_tool_approval_raw_item(item.raw_item)
                interruptions.append(
                    dataclasses.replace(
                        item,
                        agent=item.agent,
                        raw_item=copied_raw_item,
                    )
                )
        except Exception as error:
            _prepare_data_redacted_error(error)
            copy_error = UserError(
                "Cannot safely copy pending tool approvals. Ensure each interruption uses a "
                "supported tool call or contains only JSON-compatible mapping data."
            )
        if copy_error is not None:
            _mark_error_data_redacted(copy_error)
            self = cast(Any, None)
            item = cast(Any, None)
            copied_raw_item = None
            interruptions = []
            _raise_data_redacted_error(copy_error)
        return interruptions

    @staticmethod
    def _approval_items_match(
        candidate: ToolApprovalItem,
        approval_item: ToolApprovalItem,
        *,
        approval_is_authoritative: bool = False,
    ) -> bool | None:
        """Compare approval identity, returning None when an owner is unsafe to distinguish."""
        if candidate is approval_item:
            return True
        candidate_agent = candidate.agent
        approval_agent = approval_item.agent
        if (
            candidate_agent is not None
            and approval_agent is not None
            and candidate_agent is not approval_agent
        ):
            return False
        try:
            approval_raw_item = _copy_tool_approval_raw_item(approval_item.raw_item)
        except Exception:
            return None if approval_is_authoritative else False
        try:
            candidate_raw_item = _copy_tool_approval_raw_item(candidate.raw_item)
        except Exception:
            return None
        candidate_identity = tool_invocation_identity(
            candidate_raw_item,
            tool_lookup_key=candidate.tool_lookup_key,
            tool_name=candidate.tool_name,
        )
        approval_identity = tool_invocation_identity(
            approval_raw_item,
            tool_lookup_key=approval_item.tool_lookup_key,
            tool_name=approval_item.tool_name,
        )
        return candidate_identity is not None and candidate_identity == approval_identity

    def _find_current_approval_item(
        self,
        approval_item: ToolApprovalItem,
        *,
        approval_is_authoritative: bool | None = None,
    ) -> ToolApprovalItem | None:
        """Resolve a detached approval snapshot to current authoritative pending state."""
        from .run_internal.run_steps import NextStepInterruption

        if not isinstance(self._current_step, NextStepInterruption):
            return None
        if approval_is_authoritative is None:
            approval_is_authoritative = any(
                candidate is approval_item for candidate in self._current_step.interruptions
            )
        canonical_matches: list[ToolApprovalItem] = []
        has_indeterminate_candidate = False
        for candidate in self._current_step.interruptions:
            if candidate is approval_item:
                canonical_matches.append(candidate)
                continue
            match = self._approval_items_match(
                candidate,
                approval_item,
                approval_is_authoritative=approval_is_authoritative,
            )
            if match is None:
                has_indeterminate_candidate = True
            elif match:
                canonical_matches.append(candidate)
        if has_indeterminate_candidate or len(canonical_matches) > 1:
            raise UserError(
                "Cannot apply approval because multiple current pending approvals contain the "
                "same tool invocation identity, or because it belongs to both the current run "
                "and a nested agent-tool run. Use unique call IDs."
            )
        return canonical_matches[0] if canonical_matches else None

    def _find_nested_approval_state(
        self,
        approval_item: ToolApprovalItem,
    ) -> tuple[RunState[Any, Agent[Any]], ToolApprovalItem] | None:
        """Find the nested agent-tool state that owns an approval interruption."""
        if self._last_processed_response is None:
            return None

        from .agent_tool_state import peek_agent_tool_run_result
        from .run_internal.run_steps import NextStepInterruption

        nested_candidates: list[tuple[RunState[Any, Agent[Any]], ToolApprovalItem]] = []
        for function_run in self._last_processed_response.functions:
            pending_result = peek_agent_tool_run_result(
                function_run.tool_call,
                scope_id=self._agent_tool_state_scope_id,
            )
            interruptions = getattr(pending_result, "interruptions", None)
            to_state = getattr(pending_result, "to_state", None)
            if not isinstance(interruptions, list) or not callable(to_state):
                continue
            nested_state = to_state()
            if not isinstance(nested_state, RunState) or nested_state is self:
                continue
            for candidate in interruptions:
                if not isinstance(candidate, ToolApprovalItem):
                    continue
                recursive_owner = nested_state._find_nested_approval_state(candidate)
                nested_candidates.append(recursive_owner or (nested_state, candidate))

        current_candidates = (
            self._current_step.interruptions
            if isinstance(self._current_step, NextStepInterruption)
            else []
        )
        approval_is_authoritative = any(
            candidate is approval_item for candidate in current_candidates
        ) or any(candidate is approval_item for _, candidate in nested_candidates)
        current_approval_item = self._find_current_approval_item(
            approval_item,
            approval_is_authoritative=approval_is_authoritative,
        )
        canonical_matches: list[tuple[RunState[Any, Agent[Any]], ToolApprovalItem]] = []
        has_indeterminate_candidate = False
        for nested_state, candidate in nested_candidates:
            if candidate is approval_item:
                canonical_matches.append((nested_state, candidate))
                continue
            match = self._approval_items_match(
                candidate,
                approval_item,
                approval_is_authoritative=approval_is_authoritative,
            )
            if match is None:
                has_indeterminate_candidate = True
            elif match:
                canonical_matches.append((nested_state, candidate))

        if has_indeterminate_candidate:
            raise UserError(
                "Cannot apply approval because one or more nested agent-tool approvals cannot be "
                "safely distinguished. Use JSON-compatible approval payloads and unique call IDs."
            )

        identity_item = current_approval_item or approval_item
        approval_identity = tool_invocation_identity_and_scope(
            identity_item.raw_item,
            tool_lookup_key=identity_item.tool_lookup_key,
            tool_name=identity_item.tool_name,
        )
        current_state_owns_approval = False
        if approval_identity is not None and self._context is not None:
            invocation_type, call_id, approval_scope, fingerprint = approval_identity
            current_record = self._context._tool_invocations.get(call_id)
            current_state_owns_approval = current_record is not None and (
                not current_record.completed
                and current_record.invocation_type == invocation_type
                and current_record.approval_scope == approval_scope
                and current_record.fingerprint == fingerprint
            )
            current_response_identities = [
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        tool_lookup_key=get_function_tool_lookup_key_for_tool(run.function_tool),
                    )
                    for run in self._last_processed_response.functions
                ),
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        invocation_role="handoff",
                    )
                    for run in self._last_processed_response.handoffs
                ),
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        tool_name=run.computer_tool.name,
                    )
                    for run in self._last_processed_response.computer_actions
                ),
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        tool_name=run.custom_tool.name,
                    )
                    for run in self._last_processed_response.custom_tool_calls
                ),
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        tool_name=run.local_shell_tool.name,
                    )
                    for run in self._last_processed_response.local_shell_calls
                ),
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        tool_name=run.shell_tool.name,
                    )
                    for run in self._last_processed_response.shell_calls
                ),
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        tool_name=run.apply_patch_tool.name,
                    )
                    for run in self._last_processed_response.apply_patch_calls
                ),
                *(
                    tool_invocation_identity_and_scope(
                        run.tool_call,
                        tool_name=run.tool_name,
                    )
                    for run in self._last_processed_response.function_tools_not_found
                ),
                *(
                    tool_invocation_identity_and_scope(run.request_item)
                    for run in self._last_processed_response.mcp_approval_requests
                ),
            ]
            current_state_owns_approval = (
                current_state_owns_approval and approval_identity in current_response_identities
            )

        if current_state_owns_approval and canonical_matches:
            raise UserError(
                "Cannot apply approval because the same tool invocation identity belongs to both "
                "the current run and a nested agent-tool run. Use distinct call IDs."
            )
        if len(canonical_matches) == 1:
            return canonical_matches[0]
        if len(canonical_matches) > 1:
            raise UserError(
                "Cannot apply approval because multiple nested agent-tool runs contain the same "
                "tool invocation identity. Use unique call IDs within nested runs."
            )
        return None

    def approve(self, approval_item: ToolApprovalItem, always_approve: bool = False) -> None:
        """Approve a tool call and rerun with this state to continue."""
        if self._context is None:
            raise UserError("Cannot approve tool: RunState has no context")
        nested_approval = self._find_nested_approval_state(approval_item)
        if nested_approval is not None:
            nested_state, nested_item = nested_approval
            nested_state.approve(nested_item, always_approve=always_approve)
            return
        current_approval_item = self._find_current_approval_item(approval_item)
        self._context.approve_tool(
            current_approval_item or approval_item,
            always_approve=always_approve,
        )

    def reject(
        self,
        approval_item: ToolApprovalItem,
        always_reject: bool = False,
        *,
        rejection_message: str | None = None,
    ) -> None:
        """Reject a tool call and rerun with this state to continue.

        When ``rejection_message`` is provided, that exact text is sent back to the model when the
        run resumes. Otherwise the run-level tool error formatter or the SDK default message is
        used.
        """
        if self._context is None:
            raise UserError("Cannot reject tool: RunState has no context")
        nested_approval = self._find_nested_approval_state(approval_item)
        if nested_approval is not None:
            nested_state, nested_item = nested_approval
            nested_state.reject(
                nested_item,
                always_reject=always_reject,
                rejection_message=rejection_message,
            )
            return
        self._context.reject_tool(
            self._find_current_approval_item(approval_item) or approval_item,
            always_reject=always_reject,
            rejection_message=rejection_message,
        )

    def _serialize_approvals(self) -> dict[str, dict[str, Any]]:
        """Serialize approval records into a JSON-friendly mapping."""
        if self._context is None:
            return {}
        approvals_dict: dict[str, dict[str, Any]] = {}
        for tool_name, record in self._context._approvals.items():
            if not isinstance(tool_name, str):
                continue
            approvals_dict[tool_name] = {
                "approved": record.approved
                if isinstance(record.approved, bool)
                else list(record.approved),
                "rejected": record.rejected
                if isinstance(record.rejected, bool)
                else list(record.rejected),
            }
            if record.rejection_messages:
                approvals_dict[tool_name]["rejection_messages"] = dict(record.rejection_messages)
            if record.sticky_rejection_message is not None:
                approvals_dict[tool_name]["sticky_rejection_message"] = (
                    record.sticky_rejection_message
                )
            if record.sticky_scope is not None:
                approvals_dict[tool_name]["sticky_scope"] = record.sticky_scope
        return approvals_dict

    def _serialize_tool_invocations(self) -> dict[str, dict[str, Any]]:
        """Serialize the run-owned canonical tool invocation ledger."""
        if self._context is None:
            return {}
        return {
            call_id: {
                "type": invocation.invocation_type,
                "approval_scope": invocation.approval_scope,
                "fingerprint": invocation.fingerprint,
                "executed": invocation.executed,
                "completed": invocation.completed,
            }
            for call_id, invocation in self._context._tool_invocations.items()
        }

    def _serialize_hosted_mcp_approvals(self) -> list[dict[str, Any]]:
        """Serialize hosted MCP approvals with explicit typed identities."""
        if self._context is None:
            return []
        serialized: list[dict[str, Any]] = []
        hosted_records = (
            (identity, record)
            for identity, record in self._context._approvals.items()
            if isinstance(identity, tuple)
        )
        for identity, record in sorted(hosted_records):
            if identity[0] == "hosted_mcp":
                identity_data = {
                    "type": "server_tool",
                    "server_label": identity[1],
                    "tool_name": identity[2],
                }
            elif identity[0] == "hosted_mcp_call":
                identity_data = {
                    "type": "request",
                    "request_id": identity[1],
                }
            else:
                identity_data = {
                    "type": "query",
                    "tool_name": identity[1],
                    "request_id": identity[2],
                }
            decision: dict[str, Any] = {
                "approved": record.approved
                if isinstance(record.approved, bool)
                else list(record.approved),
                "rejected": record.rejected
                if isinstance(record.rejected, bool)
                else list(record.rejected),
            }
            if record.rejection_messages:
                decision["rejection_messages"] = dict(record.rejection_messages)
            if record.sticky_rejection_message is not None:
                decision["sticky_rejection_message"] = record.sticky_rejection_message
            if record.sticky_scope is not None:
                decision["sticky_scope"] = record.sticky_scope
            serialized.append({"identity": identity_data, "decision": decision})
        return serialized

    def _serialize_model_responses(self) -> list[dict[str, Any]]:
        """Serialize model responses."""
        return [
            {
                "usage": serialize_usage(resp.usage),
                "output": [_serialize_raw_item_value(item) for item in resp.output],
                "response_id": resp.response_id,
                "request_id": resp.request_id,
            }
            for resp in self._model_responses
        ]

    def _serialize_input(self, input: str | list[Any]) -> str | list[Any]:
        """Normalize input into the shape expected by Responses API."""
        if not isinstance(input, list):
            return input

        normalized_items = []
        for item in input:
            normalized_item = _serialize_raw_item_value(item)
            if isinstance(normalized_item, dict):
                normalized_item = dict(normalized_item)
                role = normalized_item.get("role")
                if role == "assistant":
                    content = normalized_item.get("content")
                    if isinstance(content, str):
                        normalized_item["content"] = [{"type": "output_text", "text": content}]
                    if "status" not in normalized_item:
                        normalized_item["status"] = "completed"
            normalized_items.append(normalized_item)
        return normalized_items

    def _serialize_original_input(self) -> str | list[Any]:
        """Normalize original input into the shape expected by Responses API."""
        return self._serialize_input(self._original_input)

    def _generated_session_item_indexes(
        self,
        generated_items: Sequence[RunItem],
    ) -> list[int | None]:
        """Map generated occurrences to the same live occurrences in session history."""
        session_indexes_by_identity: dict[int, deque[int]] = {}
        session_indexes_by_occurrence_key: dict[str, deque[int]] = {}
        for index, session_item in enumerate(self._session_items):
            session_indexes_by_identity.setdefault(id(session_item), deque()).append(index)
            occurrence_key = nested_history_run_item_occurrence_key(session_item)
            if occurrence_key is not None:
                session_indexes_by_occurrence_key.setdefault(occurrence_key, deque()).append(index)

        used_session_indexes: set[int] = set()
        indexes: list[int | None] = []

        def _take_unused(candidates: deque[int] | None) -> int | None:
            while candidates:
                candidate = candidates.popleft()
                if candidate not in used_session_indexes:
                    return candidate
            return None

        for generated_item in generated_items:
            session_index = _take_unused(
                session_indexes_by_identity.get(id(generated_item)),
            )
            if session_index is None:
                occurrence_key = nested_history_run_item_occurrence_key(generated_item)
                if occurrence_key is not None:
                    session_index = _take_unused(
                        session_indexes_by_occurrence_key.get(occurrence_key),
                    )
            if session_index is not None:
                used_session_indexes.add(session_index)
            indexes.append(session_index)
        return indexes

    def _serialize_context_payload(
        self,
        *,
        context_serializer: ContextSerializer | None = None,
        strict_context: bool = False,
    ) -> tuple[dict[str, Any] | None, dict[str, Any]]:
        """Validate and serialize the stored run context.

        The returned metadata captures how the context was serialized so restore-time code can
        decide whether a deserializer or override is required. This lets RunState remain durable
        for simple mapping contexts without silently pretending that richer custom objects can be
        reconstructed automatically.
        """
        if self._context is None:
            return None, _build_context_meta(
                None,
                serialized_via="none",
                requires_deserializer=False,
                omitted=False,
            )

        raw_context_payload = self._context.context
        if raw_context_payload is None:
            return None, _build_context_meta(
                raw_context_payload,
                serialized_via="none",
                requires_deserializer=False,
                omitted=False,
            )

        if isinstance(raw_context_payload, Mapping):
            return (
                dict(raw_context_payload),
                _build_context_meta(
                    raw_context_payload,
                    serialized_via="mapping",
                    requires_deserializer=False,
                    omitted=False,
                ),
            )

        if strict_context and context_serializer is None:
            # Avoid silently dropping non-mapping context data when strict mode is requested.
            raise UserError(
                "RunState serialization requires context to be a mapping when strict_context "
                "is True. Provide context_serializer to serialize custom contexts."
            )

        if context_serializer is not None:
            try:
                serialized = context_serializer(raw_context_payload)
            except Exception as exc:
                raise UserError(
                    "Context serializer failed while serializing RunState context."
                ) from exc
            if not isinstance(serialized, Mapping):
                raise UserError("Context serializer must return a mapping.")
            return (
                dict(serialized),
                _build_context_meta(
                    raw_context_payload,
                    serialized_via="context_serializer",
                    requires_deserializer=True,
                    omitted=False,
                ),
            )

        if hasattr(raw_context_payload, "model_dump"):
            try:
                serialized = raw_context_payload.model_dump(exclude_unset=True)
            except TypeError:
                serialized = raw_context_payload.model_dump()
            if not isinstance(serialized, Mapping):
                raise UserError("RunState context model_dump must return a mapping.")
            # We can persist the data, but the original type is lost unless the caller rebuilds it.
            logger.warning(
                "RunState context was serialized from a Pydantic model. "
                "Provide context_deserializer or context_override to restore the original type."
            )
            return (
                dict(serialized),
                _build_context_meta(
                    raw_context_payload,
                    serialized_via="model_dump",
                    requires_deserializer=True,
                    omitted=False,
                ),
            )

        if dataclasses.is_dataclass(raw_context_payload):
            serialized = dataclasses.asdict(cast(Any, raw_context_payload))
            if not isinstance(serialized, Mapping):
                raise UserError("RunState dataclass context must serialize to a mapping.")
            # Dataclass instances serialize to dicts, so reconstruction requires a deserializer.
            logger.warning(
                "RunState context was serialized from a dataclass. "
                "Provide context_deserializer or context_override to restore the original type."
            )
            return (
                dict(serialized),
                _build_context_meta(
                    raw_context_payload,
                    serialized_via="asdict",
                    requires_deserializer=True,
                    omitted=False,
                ),
            )

        # Fall back to an empty dict so the run state remains serializable, but
        # explicitly warn because the original context will be unavailable on restore.
        logger.warning(
            "RunState context of type %s is not serializable; storing empty context. "
            "Provide context_serializer to preserve it.",
            type(raw_context_payload).__name__,
        )
        return (
            {},
            _build_context_meta(
                raw_context_payload,
                serialized_via="omitted",
                requires_deserializer=True,
                omitted=True,
            ),
        )

    def _serialize_tool_input(self, tool_input: Any) -> Any:
        """Normalize tool input for JSON serialization."""
        if tool_input is None:
            return None

        if dataclasses.is_dataclass(tool_input):
            return dataclasses.asdict(cast(Any, tool_input))

        if hasattr(tool_input, "model_dump"):
            try:
                serialized = tool_input.model_dump(exclude_unset=True)
            except TypeError:
                serialized = tool_input.model_dump()
            return _to_dump_compatible(serialized)

        return _to_dump_compatible(tool_input)

    def _current_generated_items_merge_marker(self) -> str | None:
        """Return a marker for the processed response already reflected in _generated_items."""
        if self._last_processed_response is None or not self._last_processed_response.new_items:
            return None

        latest_response_id = (
            self._model_responses[-1].response_id if self._model_responses else None
        )
        agent_identity_keys_by_id = (
            _build_agent_identity_keys_by_id(cast(Agent[Any], self._starting_agent))
            if self._starting_agent is not None
            else None
        )
        serialized_items = [
            self._serialize_item(item, agent_identity_keys_by_id=agent_identity_keys_by_id)
            for item in self._last_processed_response.new_items
        ]
        return json.dumps(
            {
                "current_turn": self._current_turn,
                "last_response_id": latest_response_id,
                "new_items": serialized_items,
            },
            sort_keys=True,
            default=str,
        )

    def _mark_generated_items_merged_with_last_processed(self) -> None:
        """Remember that _generated_items already include the current processed response."""
        self._generated_items_last_processed_marker = self._current_generated_items_merge_marker()

    def _clear_generated_items_last_processed_marker(self) -> None:
        """Forget any prior merge marker after _generated_items is replaced."""
        self._generated_items_last_processed_marker = None

    def _merge_generated_items_with_processed(self) -> list[RunItem]:
        """Merge persisted and newly processed items without duplication."""
        generated_items = list(self._generated_items)
        if self._last_processed_response is None or not self._last_processed_response.new_items:
            return generated_items

        current_merge_marker = self._current_generated_items_merge_marker()
        if (
            current_merge_marker is not None
            and self._generated_items_last_processed_marker == current_merge_marker
        ):
            return generated_items

        seen_id_types: set[tuple[str, str]] = set()
        seen_call_ids: set[str] = set()
        seen_call_id_types: set[tuple[str, str]] = set()

        def _id_type_call(item: Any) -> tuple[str | None, str | None, str | None]:
            item_id = None
            item_type = None
            call_id = None
            if hasattr(item, "raw_item"):
                raw = item.raw_item
                if isinstance(raw, dict):
                    item_id = raw.get("id")
                    item_type = raw.get("type")
                    call_id = raw.get("call_id")
                else:
                    item_id = _get_attr(raw, "id")
                    item_type = _get_attr(raw, "type")
                    call_id = _get_attr(raw, "call_id")
            if item_id is None and hasattr(item, "id"):
                item_id = _get_attr(item, "id")
            if item_type is None and hasattr(item, "type"):
                item_type = _get_attr(item, "type")
            return item_id, item_type, call_id

        for existing in generated_items:
            item_id, item_type, call_id = _id_type_call(existing)
            if item_id and item_type:
                seen_id_types.add((item_id, item_type))
            if call_id and item_type:
                seen_call_id_types.add((call_id, item_type))
            elif call_id:
                seen_call_ids.add(call_id)

        for new_item in self._last_processed_response.new_items:
            item_id, item_type, call_id = _id_type_call(new_item)
            if call_id and item_type:
                if (call_id, item_type) in seen_call_id_types:
                    continue
            elif call_id and call_id in seen_call_ids:
                continue
            if item_id and item_type and (item_id, item_type) in seen_id_types:
                continue
            if item_id and item_type:
                seen_id_types.add((item_id, item_type))
            if call_id and item_type:
                seen_call_id_types.add((call_id, item_type))
            elif call_id:
                seen_call_ids.add(call_id)
            generated_items.append(new_item)

        if current_merge_marker is not None:
            self._generated_items_last_processed_marker = current_merge_marker
        return generated_items

    def to_json(
        self,
        *,
        context_serializer: ContextSerializer | None = None,
        strict_context: bool = False,
        include_tracing_api_key: bool = False,
    ) -> dict[str, Any]:
        """Serializes the run state to a JSON-compatible dictionary.

        This method is used to serialize the run state to a dictionary that can be used to
        resume the run later.

        Args:
            context_serializer: Optional function to serialize non-mapping context values.
            strict_context: When True, require mapping contexts or a context_serializer.
            include_tracing_api_key: When True, include the tracing API key in the trace payload.

        Returns:
            A dictionary representation of the run state.

        Raises:
            UserError: If required state (agent, context) is missing.
        """
        if self._current_agent is None:
            raise UserError("Cannot serialize RunState: No current agent")
        if self._context is None:
            raise UserError("Cannot serialize RunState: No context")

        approvals_dict = self._serialize_approvals()
        tool_invocations = self._serialize_tool_invocations()
        hosted_mcp_approvals = self._serialize_hosted_mcp_approvals()
        model_responses = self._serialize_model_responses()
        original_input_serialized = self._serialize_original_input()
        context_payload, context_meta = self._serialize_context_payload(
            context_serializer=context_serializer,
            strict_context=strict_context,
        )

        context_entry: dict[str, Any] = {
            "usage": serialize_usage(self._context.usage),
            "approvals": approvals_dict,
            "tool_invocations": tool_invocations,
            "context": context_payload,
            # Preserve metadata so deserialization can warn when context types were erased.
            "context_meta": context_meta,
        }
        tool_input = self._serialize_tool_input(self._context.tool_input)
        if tool_input is not None:
            context_entry["tool_input"] = tool_input
        if hosted_mcp_approvals:
            context_entry["hosted_mcp_approvals"] = hosted_mcp_approvals

        agent_identity_keys_by_id = (
            _build_agent_identity_keys_by_id(cast(Agent[Any], self._starting_agent))
            if self._starting_agent is not None
            else None
        )
        current_agent_entry = _serialize_agent_reference(
            cast(Agent[Any], self._current_agent),
            agent_identity_keys_by_id=agent_identity_keys_by_id,
        )
        generated_items = self._merge_generated_items_with_processed()

        result = {
            "$schemaVersion": CURRENT_SCHEMA_VERSION,
            "current_turn": self._current_turn,
            "current_agent": current_agent_entry,
            "original_input": original_input_serialized,
            "pending_input": self._serialize_input(self._pending_input),
            "model_responses": model_responses,
            "context": context_entry,
            "tool_use_tracker": copy.deepcopy(self._tool_use_tracker_snapshot),
            "max_turns": self._max_turns,
            "no_active_agent_run": True,
            "input_guardrail_results": _serialize_guardrail_results(
                self._input_guardrail_results,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
            ),
            "output_guardrail_results": _serialize_guardrail_results(
                self._output_guardrail_results,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
            ),
            "tool_input_guardrail_results": _serialize_tool_guardrail_results(
                self._tool_input_guardrail_results, type_label="tool_input"
            ),
            "tool_output_guardrail_results": _serialize_tool_guardrail_results(
                self._tool_output_guardrail_results, type_label="tool_output"
            ),
            "conversation_id": self._conversation_id,
            "previous_response_id": self._previous_response_id,
            "auto_previous_response_id": self._auto_previous_response_id,
            "generated_prompt_cache_key": self._generated_prompt_cache_key,
            "reasoning_item_id_policy": self._reasoning_item_id_policy,
            "nested_history_owned_session_item_refs": [
                {
                    "index": item_ref.session_index,
                    "digest": item_ref.digest,
                    "input_index": item_ref.input_index,
                }
                for item_ref in self._nested_history_owned_session_item_refs
            ],
            "generated_session_item_indexes": self._generated_session_item_indexes(generated_items),
        }

        result["generated_items"] = [
            self._serialize_item(item, agent_identity_keys_by_id=agent_identity_keys_by_id)
            for item in generated_items
        ]
        result["session_items"] = [
            self._serialize_item(item, agent_identity_keys_by_id=agent_identity_keys_by_id)
            for item in list(self._session_items)
        ]
        result["current_step"] = self._serialize_current_step()
        result["last_model_response"] = _serialize_last_model_response(model_responses)
        result["last_processed_response"] = (
            self._serialize_processed_response(
                self._last_processed_response,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
                context_serializer=context_serializer,
                strict_context=strict_context,
                include_tracing_api_key=include_tracing_api_key,
            )
            if self._last_processed_response is not None
            else None
        )
        result["current_turn_persisted_item_count"] = self._current_turn_persisted_item_count
        result["trace"] = self._serialize_trace_data(
            include_tracing_api_key=include_tracing_api_key
        )
        if self._sandbox is not None:
            from .sandbox._mount_security import (
                _raise_invalid_run_state_sandbox_envelope,
                sanitize_run_state_sandbox_mount_authority,
            )

            if not isinstance(self._sandbox, Mapping):
                self._sandbox = None
                _raise_invalid_run_state_sandbox_envelope()

            sanitized_sandbox, _redacted = sanitize_run_state_sandbox_mount_authority(self._sandbox)
            result["sandbox"] = sanitized_sandbox

        return result

    def _serialize_processed_response(
        self,
        processed_response: ProcessedResponse,
        *,
        agent_identity_keys_by_id: Mapping[int, str] | None = None,
        context_serializer: ContextSerializer | None = None,
        strict_context: bool = False,
        include_tracing_api_key: bool = False,
    ) -> dict[str, Any]:
        """Serialize a ProcessedResponse to JSON format.

        Args:
            processed_response: The ProcessedResponse to serialize.

        Returns:
            A dictionary representation of the ProcessedResponse.
        """

        action_groups = _serialize_tool_action_groups(processed_response)
        _serialize_pending_nested_agent_tool_runs(
            parent_state=self,
            function_entries=action_groups.get("functions", []),
            function_runs=processed_response.functions,
            scope_id=self._agent_tool_state_scope_id,
            context_serializer=context_serializer,
            strict_context=strict_context,
            include_tracing_api_key=include_tracing_api_key,
        )

        interruptions_data = [
            _serialize_tool_approval_interruption(
                interruption,
                include_tool_name=True,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
            )
            for interruption in processed_response.interruptions
            if isinstance(interruption, ToolApprovalItem)
        ]

        return {
            "new_items": [
                self._serialize_item(item, agent_identity_keys_by_id=agent_identity_keys_by_id)
                for item in processed_response.new_items
            ],
            "tools_used": processed_response.tools_used,
            **action_groups,
            "interruptions": interruptions_data,
        }

    def _serialize_current_step(self) -> dict[str, Any] | None:
        """Serialize the current resumable step."""
        # Import at runtime to avoid circular import
        from .run_internal.run_steps import NextStepInterruption, NextStepRunAgain

        agent_identity_keys_by_id = (
            _build_agent_identity_keys_by_id(cast(Agent[Any], self._starting_agent))
            if self._starting_agent is not None
            else None
        )

        if isinstance(self._current_step, NextStepRunAgain):
            return {"type": "next_step_run_again"}

        if self._current_step is None or not isinstance(self._current_step, NextStepInterruption):
            return None

        interruptions_data = [
            _serialize_tool_approval_interruption(
                item,
                include_tool_name=item.tool_name is not None,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
            )
            for item in self._current_step.interruptions
            if isinstance(item, ToolApprovalItem)
        ]

        return {
            "type": "next_step_interruption",
            "data": {
                "interruptions": interruptions_data,
                "response_accepted": self._current_step.response_accepted,
                "llm_end_hooks_started": self._current_step.llm_end_hooks_started,
            },
        }

    def _serialize_item(
        self,
        item: RunItem,
        *,
        agent_identity_keys_by_id: Mapping[int, str] | None = None,
    ) -> dict[str, Any]:
        """Serialize a run item to JSON-compatible dict."""
        raw_item_dict: Any = _serialize_raw_item_value(item.raw_item)

        result: dict[str, Any] = {
            "type": item.type,
            "raw_item": raw_item_dict,
            "agent": _serialize_agent_reference(
                item.agent,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
            ),
        }

        if isinstance(item, InputItem):
            result["input_id"] = item.input_id

        # Add additional fields based on item type
        if hasattr(item, "output"):
            try:
                serialized_output = _ensure_json_compatible(_serialize_output_value(item.output))
            except Exception:
                serialized_output = str(item.output)
            result["output"] = serialized_output
        if hasattr(item, "source_agent"):
            result["source_agent"] = _serialize_agent_reference(
                item.source_agent,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
            )
        if hasattr(item, "target_agent"):
            result["target_agent"] = _serialize_agent_reference(
                item.target_agent,
                agent_identity_keys_by_id=agent_identity_keys_by_id,
            )
        if hasattr(item, "tool_name") and item.tool_name is not None:
            result["tool_name"] = item.tool_name
        if hasattr(item, "tool_namespace") and item.tool_namespace is not None:
            result["tool_namespace"] = item.tool_namespace
        tool_lookup_key = serialize_function_tool_lookup_key(getattr(item, "tool_lookup_key", None))
        if tool_lookup_key is not None:
            result["tool_lookup_key"] = tool_lookup_key
        if getattr(item, "_allow_bare_name_alias", False):
            result["allow_bare_name_alias"] = True
        if hasattr(item, "description") and item.description is not None:
            result["description"] = item.description
        if hasattr(item, "title") and item.title is not None:
            result["title"] = item.title
        tool_origin = getattr(item, "tool_origin", None)
        if isinstance(tool_origin, ToolOrigin):
            result["tool_origin"] = tool_origin.to_json_dict()
        custom_data = getattr(item, "custom_data", None)
        if isinstance(custom_data, dict) and custom_data:
            result["custom_data"] = _ensure_json_compatible(custom_data)

        return result

    def _lookup_function_name(self, call_id: str) -> str:
        """Attempt to find the function name for the provided call_id."""
        if not call_id:
            return ""

        def _extract_name(raw: Any) -> str | None:
            if isinstance(raw, dict):
                candidate_call_id = cast(str | None, raw.get("call_id"))
                if candidate_call_id == call_id:
                    name_value = raw.get("name", "")
                    return str(name_value) if name_value else ""
            else:
                candidate_call_id = cast(str | None, _get_attr(raw, "call_id"))
                if candidate_call_id == call_id:
                    name_value = _get_attr(raw, "name", "")
                    return str(name_value) if name_value else ""
            return None

        # Search generated items first
        for run_item in self._generated_items:
            if run_item.type != "tool_call_item":
                continue
            name = _extract_name(run_item.raw_item)
            if name is not None:
                return name

        # Inspect last processed response
        if self._last_processed_response is not None:
            for run_item in self._last_processed_response.new_items:
                if run_item.type != "tool_call_item":
                    continue
                name = _extract_name(run_item.raw_item)
                if name is not None:
                    return name

        # Finally, inspect the original input list where the function call originated
        if isinstance(self._original_input, list):
            for input_item in self._original_input:
                if not isinstance(input_item, dict):
                    continue
                if input_item.get("type") != "function_call":
                    continue
                item_call_id = cast(str | None, input_item.get("call_id"))
                if item_call_id == call_id:
                    name_value = input_item.get("name", "")
                    return str(name_value) if name_value else ""

        return ""

    def to_string(
        self,
        *,
        context_serializer: ContextSerializer | None = None,
        strict_context: bool = False,
        include_tracing_api_key: bool = False,
    ) -> str:
        """Serializes the run state to a JSON string.

        Args:
            include_tracing_api_key: When True, include the tracing API key in the trace payload.

        Returns:
            JSON string representation of the run state.
        """
        return json.dumps(
            self.to_json(
                context_serializer=context_serializer,
                strict_context=strict_context,
                include_tracing_api_key=include_tracing_api_key,
            ),
            indent=2,
        )

    def set_trace(self, trace: Trace | None) -> None:
        """Capture trace metadata for serialization/resumption."""
        self._trace_state = TraceState.from_trace(trace)

    def _serialize_trace_data(self, *, include_tracing_api_key: bool) -> dict[str, Any] | None:
        if self._trace_state is None:
            return None
        return self._trace_state.to_json(include_tracing_api_key=include_tracing_api_key)

    def set_tool_use_tracker_snapshot(self, snapshot: Mapping[str, Sequence[str]] | None) -> None:
        """Store a copy of the serialized tool-use tracker data."""
        if not snapshot:
            self._tool_use_tracker_snapshot = {}
            return

        normalized: dict[str, list[str]] = {}
        for agent_name, tools in snapshot.items():
            if not isinstance(agent_name, str):
                continue
            normalized[agent_name] = [tool for tool in tools if isinstance(tool, str)]
        self._tool_use_tracker_snapshot = normalized

    def set_reasoning_item_id_policy(self, policy: Literal["preserve", "omit"] | None) -> None:
        """Store how reasoning item IDs should appear in next-turn model input."""
        self._reasoning_item_id_policy = policy

    def get_tool_use_tracker_snapshot(self) -> dict[str, list[str]]:
        """Return a defensive copy of the tool-use tracker snapshot."""
        return {
            agent_name: list(tool_names)
            for agent_name, tool_names in self._tool_use_tracker_snapshot.items()
        }

    @staticmethod
    async def from_string(
        initial_agent: Agent[Any],
        state_string: str,
        *,
        context_override: ContextOverride | None = None,
        context_deserializer: ContextDeserializer | None = None,
        strict_context: bool = False,
    ) -> RunState[Any, Agent[Any]]:
        """Deserializes a run state from a JSON string.

        This method is used to deserialize a run state from a string that was serialized using
        the `to_string()` method.

        Args:
            initial_agent: The initial agent (used to build agent map for resolution).
            state_string: The JSON string to deserialize.
            context_override: Optional context mapping or RunContextWrapper to use instead of the
                serialized context.
            context_deserializer: Optional function to rebuild non-mapping context values.
            strict_context: When True, require a deserializer or override for non-mapping contexts.

        Returns:
            A reconstructed RunState instance.

        Raises:
            UserError: If the string is invalid JSON or has incompatible schema version.
        """
        parse_error: BaseException | None = None
        try:
            state_json = json.loads(state_string)
        except json.JSONDecodeError as error:
            state_string = "<redacted>"
            _prepare_data_redacted_error(error)
            parse_error = UserError("Failed to parse run state JSON")
        except BaseException as error:
            state_string = "<redacted>"
            prepared_error = _prepare_data_redacted_error(error)
            if type(prepared_error) in {asyncio.CancelledError, KeyboardInterrupt, SystemExit}:
                parse_error = prepared_error
            else:
                parse_error = UserError("Failed to parse run state JSON")

        state_string = "<redacted>"
        if parse_error is not None:
            _mark_error_data_redacted(parse_error)
            initial_agent = cast(Any, None)
            context_override = None
            context_deserializer = None
            _raise_data_redacted_error(parse_error)

        safe_error: BaseException | None = None
        try:
            return await RunState.from_json(
                initial_agent=initial_agent,
                state_json=state_json,
                context_override=context_override,
                context_deserializer=context_deserializer,
                strict_context=strict_context,
            )
        except BaseException as error:
            trusted_error_message = _known_run_state_error_message(error)
            safe_error = _prepare_data_redacted_error(
                error,
                trusted_error_message=trusted_error_message,
            )

        state_json = cast(Any, None)
        initial_agent = cast(Any, None)
        context_override = None
        context_deserializer = None
        assert safe_error is not None
        _raise_data_redacted_error(safe_error)

    @staticmethod
    async def from_json(
        initial_agent: Agent[Any],
        state_json: dict[str, Any],
        *,
        context_override: ContextOverride | None = None,
        context_deserializer: ContextDeserializer | None = None,
        strict_context: bool = False,
    ) -> RunState[Any, Agent[Any]]:
        """Deserializes a run state from a JSON dictionary.

        This method is used to deserialize a run state from a dict that was created using
        the `to_json()` method.

        Args:
            initial_agent: The initial agent (used to build agent map for resolution).
            state_json: The JSON dictionary to deserialize.
            context_override: Optional context mapping or RunContextWrapper to use instead of the
                serialized context.
            context_deserializer: Optional function to rebuild non-mapping context values.
            strict_context: When True, require a deserializer or override for non-mapping contexts.

        Returns:
            A reconstructed RunState instance.

        Raises:
            UserError: If the dict has incompatible schema version.
        """
        restore_error: BaseException | None = None
        trusted_validation_errors: list[tuple[BaseException, str]] = []

        def validation_error_factory(
            message: str,
            error_type: RunStateValidationErrorType,
        ) -> RunStateValidationError:
            error = error_type(message)
            trusted_validation_errors.append((error, message))
            return error

        try:
            if not isinstance(state_json, dict):
                state_json = cast(Any, None)
                raise validation_error_factory("Run state JSON must be an object", UserError)

            _validate_run_state_json_value(state_json)

            _validate_run_state_schema_version(
                state_json,
                validation_error_factory=validation_error_factory,
            )

            from .sandbox._mount_security import sanitize_run_state_sandbox_mount_authority

            if "sandbox" in state_json:
                if not isinstance(state_json["sandbox"], Mapping):
                    state_json["sandbox"] = {}
                    raise validation_error_factory(
                        "RunState sandbox resume state has an invalid envelope",
                        ValueError,
                    )
                sanitized_sandbox, _redacted = sanitize_run_state_sandbox_mount_authority(
                    state_json["sandbox"],
                    validation_error_factory=lambda message: cast(
                        ValueError,
                        validation_error_factory(message, ValueError),
                    ),
                )
                state_json["sandbox"] = sanitized_sandbox

            return await _build_run_state_from_json(
                initial_agent=initial_agent,
                state_json=state_json,
                context_override=context_override,
                context_deserializer=context_deserializer,
                strict_context=strict_context,
                validation_error_factory=validation_error_factory,
            )
        except BaseException as error:
            trusted_error_message = _trusted_run_state_validation_message(
                error,
                trusted_validation_errors,
            )
            restore_error = _prepare_data_redacted_error(
                error,
                trusted_error_message=trusted_error_message,
            )
            trusted_validation_errors.clear()

        state_json = cast(Any, None)
        initial_agent = cast(Any, None)
        context_override = None
        context_deserializer = None
        assert restore_error is not None
        _raise_data_redacted_error(restore_error)

pending_input property

pending_input: list[TResponseInputItem]

Return a copy of input currently staged for the next resumed model call.

__init__

__init__(
    context: RunContextWrapper[TContext],
    original_input: str | list[Any],
    starting_agent: TAgent,
    max_turns: int | None = 10,
    *,
    conversation_id: str | None = None,
    previous_response_id: str | None = None,
    auto_previous_response_id: bool = False,
)

Initialize a new RunState.

ソースコード位置: src/agents/run_state.py
def __init__(
    self,
    context: RunContextWrapper[TContext],
    original_input: str | list[Any],
    starting_agent: TAgent,
    max_turns: int | None = 10,
    *,
    conversation_id: str | None = None,
    previous_response_id: str | None = None,
    auto_previous_response_id: bool = False,
):
    """Initialize a new RunState."""
    self._context = context
    self._original_input = _clone_original_input(original_input)
    self._starting_agent = starting_agent
    self._current_agent = starting_agent
    self._max_turns = max_turns
    self._conversation_id = conversation_id
    self._previous_response_id = previous_response_id
    self._auto_previous_response_id = auto_previous_response_id
    self._generated_prompt_cache_key = None
    self._reasoning_item_id_policy = None
    self._model_responses = []
    self._generated_items = []
    self._session_items = []
    self._pending_input = []
    self._nested_history_owned_session_item_refs = []
    self._input_guardrail_results = []
    self._output_guardrail_results = []
    self._tool_input_guardrail_results = []
    self._tool_output_guardrail_results = []
    self._current_step = None
    self._current_turn = 0
    self._last_processed_response = None
    self._generated_items_last_processed_marker = None
    self._current_turn_persisted_item_count = 0
    self._tool_use_tracker_snapshot = {}
    self._trace_state = None
    self._sandbox = None
    self._schema_version = CURRENT_SCHEMA_VERSION
    from .agent_tool_state import get_agent_tool_state_scope

    self._agent_tool_state_scope_id = get_agent_tool_state_scope(context)

add_input

add_input(input: str | list[TResponseInputItem]) -> None

Stage input for admission immediately before the next resumed model call.

String input is normalized to a user message. Multiple calls preserve insertion order. The input remains pending until its guardrails and conversation ownership boundary accept it. Terminal states reject new input before mutating the state.

ソースコード位置: src/agents/run_state.py
def add_input(self, input: str | list[TResponseInputItem]) -> None:
    """Stage input for admission immediately before the next resumed model call.

    String input is normalized to a user message. Multiple calls preserve insertion order.
    The input remains pending until its guardrails and conversation ownership boundary accept
    it. Terminal states reject new input before mutating the state.
    """
    from .run_internal.run_steps import NextStepInterruption, NextStepRunAgain

    if not isinstance(self._current_step, NextStepInterruption | NextStepRunAgain):
        raise UserError("Cannot add input to a terminal RunState")
    if self._max_turns is not None and self._current_turn >= self._max_turns:
        raise UserError("Cannot add input to a RunState with no remaining model turns")
    if isinstance(self._current_step, NextStepInterruption):
        if self._current_step.response_accepted:
            raise UserError(
                "Cannot add input while an accepted model response is awaiting local processing"
            )
        if self._current_agent is None:
            raise UserError("Cannot add input to a RunState without a current agent")
        tool_use_behavior = self._current_agent.tool_use_behavior
        interrupted_tool_names = {
            item.tool_name
            for item in self._current_step.interruptions
            if item.tool_name is not None
        }
        stops_before_next_model = tool_use_behavior == "stop_on_first_tool" or (
            isinstance(tool_use_behavior, dict)
            and bool(
                interrupted_tool_names & set(tool_use_behavior.get("stop_at_tool_names", []))
            )
        )
        if stops_before_next_model or callable(tool_use_behavior):
            raise UserError(
                "Cannot add input to an interrupted RunState whose tool result may end the run"
            )

    normalized = ItemHelpers.input_to_new_input_list(input)
    self._pending_input.extend(copy.deepcopy(normalized))

clear_pending_input

clear_pending_input() -> None

Remove all input staged for the next resumed model call.

ソースコード位置: src/agents/run_state.py
def clear_pending_input(self) -> None:
    """Remove all input staged for the next resumed model call."""
    self._pending_input = []

get_interruptions

get_interruptions() -> list[ToolApprovalItem]

Return detached copies of pending interruptions for the current step.

ソースコード位置: src/agents/run_state.py
def get_interruptions(self) -> list[ToolApprovalItem]:
    """Return detached copies of pending interruptions for the current step."""
    # Import at runtime to avoid circular import
    from .run_internal.run_steps import NextStepInterruption

    if self._current_step is None or not isinstance(self._current_step, NextStepInterruption):
        return []
    copy_error: UserError | None = None
    try:
        interruptions: list[ToolApprovalItem] = []
        for item in self._current_step.interruptions:
            copied_raw_item = _copy_tool_approval_raw_item(item.raw_item)
            interruptions.append(
                dataclasses.replace(
                    item,
                    agent=item.agent,
                    raw_item=copied_raw_item,
                )
            )
    except Exception as error:
        _prepare_data_redacted_error(error)
        copy_error = UserError(
            "Cannot safely copy pending tool approvals. Ensure each interruption uses a "
            "supported tool call or contains only JSON-compatible mapping data."
        )
    if copy_error is not None:
        _mark_error_data_redacted(copy_error)
        self = cast(Any, None)
        item = cast(Any, None)
        copied_raw_item = None
        interruptions = []
        _raise_data_redacted_error(copy_error)
    return interruptions

approve

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

Approve a tool call and rerun with this state to continue.

ソースコード位置: src/agents/run_state.py
def approve(self, approval_item: ToolApprovalItem, always_approve: bool = False) -> None:
    """Approve a tool call and rerun with this state to continue."""
    if self._context is None:
        raise UserError("Cannot approve tool: RunState has no context")
    nested_approval = self._find_nested_approval_state(approval_item)
    if nested_approval is not None:
        nested_state, nested_item = nested_approval
        nested_state.approve(nested_item, always_approve=always_approve)
        return
    current_approval_item = self._find_current_approval_item(approval_item)
    self._context.approve_tool(
        current_approval_item or approval_item,
        always_approve=always_approve,
    )

reject

reject(
    approval_item: ToolApprovalItem,
    always_reject: bool = False,
    *,
    rejection_message: str | None = None,
) -> None

Reject a tool call and rerun with this state to continue.

When rejection_message is provided, that exact text is sent back to the model when the run resumes. Otherwise the run-level tool error formatter or the SDK default message is used.

ソースコード位置: src/agents/run_state.py
def reject(
    self,
    approval_item: ToolApprovalItem,
    always_reject: bool = False,
    *,
    rejection_message: str | None = None,
) -> None:
    """Reject a tool call and rerun with this state to continue.

    When ``rejection_message`` is provided, that exact text is sent back to the model when the
    run resumes. Otherwise the run-level tool error formatter or the SDK default message is
    used.
    """
    if self._context is None:
        raise UserError("Cannot reject tool: RunState has no context")
    nested_approval = self._find_nested_approval_state(approval_item)
    if nested_approval is not None:
        nested_state, nested_item = nested_approval
        nested_state.reject(
            nested_item,
            always_reject=always_reject,
            rejection_message=rejection_message,
        )
        return
    self._context.reject_tool(
        self._find_current_approval_item(approval_item) or approval_item,
        always_reject=always_reject,
        rejection_message=rejection_message,
    )

to_json

to_json(
    *,
    context_serializer: ContextSerializer | None = None,
    strict_context: bool = False,
    include_tracing_api_key: bool = False,
) -> dict[str, Any]

Serializes the run state to a JSON-compatible dictionary.

This method is used to serialize the run state to a dictionary that can be used to resume the run later.

引数:

名前 タイプ デスクリプション デフォルト
context_serializer ContextSerializer | None

Optional function to serialize non-mapping context values.

None
strict_context bool

When True, require mapping contexts or a context_serializer.

False
include_tracing_api_key bool

When True, include the tracing API key in the trace payload.

False

戻り値:

タイプ デスクリプション
dict[str, Any]

A dictionary representation of the run state.

発生:

タイプ デスクリプション
UserError

If required state (agent, context) is missing.

ソースコード位置: src/agents/run_state.py
def to_json(
    self,
    *,
    context_serializer: ContextSerializer | None = None,
    strict_context: bool = False,
    include_tracing_api_key: bool = False,
) -> dict[str, Any]:
    """Serializes the run state to a JSON-compatible dictionary.

    This method is used to serialize the run state to a dictionary that can be used to
    resume the run later.

    Args:
        context_serializer: Optional function to serialize non-mapping context values.
        strict_context: When True, require mapping contexts or a context_serializer.
        include_tracing_api_key: When True, include the tracing API key in the trace payload.

    Returns:
        A dictionary representation of the run state.

    Raises:
        UserError: If required state (agent, context) is missing.
    """
    if self._current_agent is None:
        raise UserError("Cannot serialize RunState: No current agent")
    if self._context is None:
        raise UserError("Cannot serialize RunState: No context")

    approvals_dict = self._serialize_approvals()
    tool_invocations = self._serialize_tool_invocations()
    hosted_mcp_approvals = self._serialize_hosted_mcp_approvals()
    model_responses = self._serialize_model_responses()
    original_input_serialized = self._serialize_original_input()
    context_payload, context_meta = self._serialize_context_payload(
        context_serializer=context_serializer,
        strict_context=strict_context,
    )

    context_entry: dict[str, Any] = {
        "usage": serialize_usage(self._context.usage),
        "approvals": approvals_dict,
        "tool_invocations": tool_invocations,
        "context": context_payload,
        # Preserve metadata so deserialization can warn when context types were erased.
        "context_meta": context_meta,
    }
    tool_input = self._serialize_tool_input(self._context.tool_input)
    if tool_input is not None:
        context_entry["tool_input"] = tool_input
    if hosted_mcp_approvals:
        context_entry["hosted_mcp_approvals"] = hosted_mcp_approvals

    agent_identity_keys_by_id = (
        _build_agent_identity_keys_by_id(cast(Agent[Any], self._starting_agent))
        if self._starting_agent is not None
        else None
    )
    current_agent_entry = _serialize_agent_reference(
        cast(Agent[Any], self._current_agent),
        agent_identity_keys_by_id=agent_identity_keys_by_id,
    )
    generated_items = self._merge_generated_items_with_processed()

    result = {
        "$schemaVersion": CURRENT_SCHEMA_VERSION,
        "current_turn": self._current_turn,
        "current_agent": current_agent_entry,
        "original_input": original_input_serialized,
        "pending_input": self._serialize_input(self._pending_input),
        "model_responses": model_responses,
        "context": context_entry,
        "tool_use_tracker": copy.deepcopy(self._tool_use_tracker_snapshot),
        "max_turns": self._max_turns,
        "no_active_agent_run": True,
        "input_guardrail_results": _serialize_guardrail_results(
            self._input_guardrail_results,
            agent_identity_keys_by_id=agent_identity_keys_by_id,
        ),
        "output_guardrail_results": _serialize_guardrail_results(
            self._output_guardrail_results,
            agent_identity_keys_by_id=agent_identity_keys_by_id,
        ),
        "tool_input_guardrail_results": _serialize_tool_guardrail_results(
            self._tool_input_guardrail_results, type_label="tool_input"
        ),
        "tool_output_guardrail_results": _serialize_tool_guardrail_results(
            self._tool_output_guardrail_results, type_label="tool_output"
        ),
        "conversation_id": self._conversation_id,
        "previous_response_id": self._previous_response_id,
        "auto_previous_response_id": self._auto_previous_response_id,
        "generated_prompt_cache_key": self._generated_prompt_cache_key,
        "reasoning_item_id_policy": self._reasoning_item_id_policy,
        "nested_history_owned_session_item_refs": [
            {
                "index": item_ref.session_index,
                "digest": item_ref.digest,
                "input_index": item_ref.input_index,
            }
            for item_ref in self._nested_history_owned_session_item_refs
        ],
        "generated_session_item_indexes": self._generated_session_item_indexes(generated_items),
    }

    result["generated_items"] = [
        self._serialize_item(item, agent_identity_keys_by_id=agent_identity_keys_by_id)
        for item in generated_items
    ]
    result["session_items"] = [
        self._serialize_item(item, agent_identity_keys_by_id=agent_identity_keys_by_id)
        for item in list(self._session_items)
    ]
    result["current_step"] = self._serialize_current_step()
    result["last_model_response"] = _serialize_last_model_response(model_responses)
    result["last_processed_response"] = (
        self._serialize_processed_response(
            self._last_processed_response,
            agent_identity_keys_by_id=agent_identity_keys_by_id,
            context_serializer=context_serializer,
            strict_context=strict_context,
            include_tracing_api_key=include_tracing_api_key,
        )
        if self._last_processed_response is not None
        else None
    )
    result["current_turn_persisted_item_count"] = self._current_turn_persisted_item_count
    result["trace"] = self._serialize_trace_data(
        include_tracing_api_key=include_tracing_api_key
    )
    if self._sandbox is not None:
        from .sandbox._mount_security import (
            _raise_invalid_run_state_sandbox_envelope,
            sanitize_run_state_sandbox_mount_authority,
        )

        if not isinstance(self._sandbox, Mapping):
            self._sandbox = None
            _raise_invalid_run_state_sandbox_envelope()

        sanitized_sandbox, _redacted = sanitize_run_state_sandbox_mount_authority(self._sandbox)
        result["sandbox"] = sanitized_sandbox

    return result

to_string

to_string(
    *,
    context_serializer: ContextSerializer | None = None,
    strict_context: bool = False,
    include_tracing_api_key: bool = False,
) -> str

Serializes the run state to a JSON string.

引数:

名前 タイプ デスクリプション デフォルト
include_tracing_api_key bool

When True, include the tracing API key in the trace payload.

False

戻り値:

タイプ デスクリプション
str

JSON string representation of the run state.

ソースコード位置: src/agents/run_state.py
def to_string(
    self,
    *,
    context_serializer: ContextSerializer | None = None,
    strict_context: bool = False,
    include_tracing_api_key: bool = False,
) -> str:
    """Serializes the run state to a JSON string.

    Args:
        include_tracing_api_key: When True, include the tracing API key in the trace payload.

    Returns:
        JSON string representation of the run state.
    """
    return json.dumps(
        self.to_json(
            context_serializer=context_serializer,
            strict_context=strict_context,
            include_tracing_api_key=include_tracing_api_key,
        ),
        indent=2,
    )

set_trace

set_trace(trace: Trace | None) -> None

Capture trace metadata for serialization/resumption.

ソースコード位置: src/agents/run_state.py
def set_trace(self, trace: Trace | None) -> None:
    """Capture trace metadata for serialization/resumption."""
    self._trace_state = TraceState.from_trace(trace)

set_tool_use_tracker_snapshot

set_tool_use_tracker_snapshot(
    snapshot: Mapping[str, Sequence[str]] | None,
) -> None

Store a copy of the serialized tool-use tracker data.

ソースコード位置: src/agents/run_state.py
def set_tool_use_tracker_snapshot(self, snapshot: Mapping[str, Sequence[str]] | None) -> None:
    """Store a copy of the serialized tool-use tracker data."""
    if not snapshot:
        self._tool_use_tracker_snapshot = {}
        return

    normalized: dict[str, list[str]] = {}
    for agent_name, tools in snapshot.items():
        if not isinstance(agent_name, str):
            continue
        normalized[agent_name] = [tool for tool in tools if isinstance(tool, str)]
    self._tool_use_tracker_snapshot = normalized

set_reasoning_item_id_policy

set_reasoning_item_id_policy(
    policy: Literal["preserve", "omit"] | None,
) -> None

Store how reasoning item IDs should appear in next-turn model input.

ソースコード位置: src/agents/run_state.py
def set_reasoning_item_id_policy(self, policy: Literal["preserve", "omit"] | None) -> None:
    """Store how reasoning item IDs should appear in next-turn model input."""
    self._reasoning_item_id_policy = policy

get_tool_use_tracker_snapshot

get_tool_use_tracker_snapshot() -> dict[str, list[str]]

Return a defensive copy of the tool-use tracker snapshot.

ソースコード位置: src/agents/run_state.py
def get_tool_use_tracker_snapshot(self) -> dict[str, list[str]]:
    """Return a defensive copy of the tool-use tracker snapshot."""
    return {
        agent_name: list(tool_names)
        for agent_name, tool_names in self._tool_use_tracker_snapshot.items()
    }

from_string async staticmethod

from_string(
    initial_agent: Agent[Any],
    state_string: str,
    *,
    context_override: ContextOverride | None = None,
    context_deserializer: ContextDeserializer | None = None,
    strict_context: bool = False,
) -> RunState[Any, Agent[Any]]

Deserializes a run state from a JSON string.

This method is used to deserialize a run state from a string that was serialized using the to_string() method.

引数:

名前 タイプ デスクリプション デフォルト
initial_agent Agent[Any]

The initial agent (used to build agent map for resolution).

必須
state_string str

The JSON string to deserialize.

必須
context_override ContextOverride | None

Optional context mapping or RunContextWrapper to use instead of the serialized context.

None
context_deserializer ContextDeserializer | None

Optional function to rebuild non-mapping context values.

None
strict_context bool

When True, require a deserializer or override for non-mapping contexts.

False

戻り値:

タイプ デスクリプション
RunState[Any, Agent[Any]]

A reconstructed RunState instance.

発生:

タイプ デスクリプション
UserError

If the string is invalid JSON or has incompatible schema version.

ソースコード位置: src/agents/run_state.py
@staticmethod
async def from_string(
    initial_agent: Agent[Any],
    state_string: str,
    *,
    context_override: ContextOverride | None = None,
    context_deserializer: ContextDeserializer | None = None,
    strict_context: bool = False,
) -> RunState[Any, Agent[Any]]:
    """Deserializes a run state from a JSON string.

    This method is used to deserialize a run state from a string that was serialized using
    the `to_string()` method.

    Args:
        initial_agent: The initial agent (used to build agent map for resolution).
        state_string: The JSON string to deserialize.
        context_override: Optional context mapping or RunContextWrapper to use instead of the
            serialized context.
        context_deserializer: Optional function to rebuild non-mapping context values.
        strict_context: When True, require a deserializer or override for non-mapping contexts.

    Returns:
        A reconstructed RunState instance.

    Raises:
        UserError: If the string is invalid JSON or has incompatible schema version.
    """
    parse_error: BaseException | None = None
    try:
        state_json = json.loads(state_string)
    except json.JSONDecodeError as error:
        state_string = "<redacted>"
        _prepare_data_redacted_error(error)
        parse_error = UserError("Failed to parse run state JSON")
    except BaseException as error:
        state_string = "<redacted>"
        prepared_error = _prepare_data_redacted_error(error)
        if type(prepared_error) in {asyncio.CancelledError, KeyboardInterrupt, SystemExit}:
            parse_error = prepared_error
        else:
            parse_error = UserError("Failed to parse run state JSON")

    state_string = "<redacted>"
    if parse_error is not None:
        _mark_error_data_redacted(parse_error)
        initial_agent = cast(Any, None)
        context_override = None
        context_deserializer = None
        _raise_data_redacted_error(parse_error)

    safe_error: BaseException | None = None
    try:
        return await RunState.from_json(
            initial_agent=initial_agent,
            state_json=state_json,
            context_override=context_override,
            context_deserializer=context_deserializer,
            strict_context=strict_context,
        )
    except BaseException as error:
        trusted_error_message = _known_run_state_error_message(error)
        safe_error = _prepare_data_redacted_error(
            error,
            trusted_error_message=trusted_error_message,
        )

    state_json = cast(Any, None)
    initial_agent = cast(Any, None)
    context_override = None
    context_deserializer = None
    assert safe_error is not None
    _raise_data_redacted_error(safe_error)

from_json async staticmethod

from_json(
    initial_agent: Agent[Any],
    state_json: dict[str, Any],
    *,
    context_override: ContextOverride | None = None,
    context_deserializer: ContextDeserializer | None = None,
    strict_context: bool = False,
) -> RunState[Any, Agent[Any]]

Deserializes a run state from a JSON dictionary.

This method is used to deserialize a run state from a dict that was created using the to_json() method.

引数:

名前 タイプ デスクリプション デフォルト
initial_agent Agent[Any]

The initial agent (used to build agent map for resolution).

必須
state_json dict[str, Any]

The JSON dictionary to deserialize.

必須
context_override ContextOverride | None

Optional context mapping or RunContextWrapper to use instead of the serialized context.

None
context_deserializer ContextDeserializer | None

Optional function to rebuild non-mapping context values.

None
strict_context bool

When True, require a deserializer or override for non-mapping contexts.

False

戻り値:

タイプ デスクリプション
RunState[Any, Agent[Any]]

A reconstructed RunState instance.

発生:

タイプ デスクリプション
UserError

If the dict has incompatible schema version.

ソースコード位置: src/agents/run_state.py
@staticmethod
async def from_json(
    initial_agent: Agent[Any],
    state_json: dict[str, Any],
    *,
    context_override: ContextOverride | None = None,
    context_deserializer: ContextDeserializer | None = None,
    strict_context: bool = False,
) -> RunState[Any, Agent[Any]]:
    """Deserializes a run state from a JSON dictionary.

    This method is used to deserialize a run state from a dict that was created using
    the `to_json()` method.

    Args:
        initial_agent: The initial agent (used to build agent map for resolution).
        state_json: The JSON dictionary to deserialize.
        context_override: Optional context mapping or RunContextWrapper to use instead of the
            serialized context.
        context_deserializer: Optional function to rebuild non-mapping context values.
        strict_context: When True, require a deserializer or override for non-mapping contexts.

    Returns:
        A reconstructed RunState instance.

    Raises:
        UserError: If the dict has incompatible schema version.
    """
    restore_error: BaseException | None = None
    trusted_validation_errors: list[tuple[BaseException, str]] = []

    def validation_error_factory(
        message: str,
        error_type: RunStateValidationErrorType,
    ) -> RunStateValidationError:
        error = error_type(message)
        trusted_validation_errors.append((error, message))
        return error

    try:
        if not isinstance(state_json, dict):
            state_json = cast(Any, None)
            raise validation_error_factory("Run state JSON must be an object", UserError)

        _validate_run_state_json_value(state_json)

        _validate_run_state_schema_version(
            state_json,
            validation_error_factory=validation_error_factory,
        )

        from .sandbox._mount_security import sanitize_run_state_sandbox_mount_authority

        if "sandbox" in state_json:
            if not isinstance(state_json["sandbox"], Mapping):
                state_json["sandbox"] = {}
                raise validation_error_factory(
                    "RunState sandbox resume state has an invalid envelope",
                    ValueError,
                )
            sanitized_sandbox, _redacted = sanitize_run_state_sandbox_mount_authority(
                state_json["sandbox"],
                validation_error_factory=lambda message: cast(
                    ValueError,
                    validation_error_factory(message, ValueError),
                ),
            )
            state_json["sandbox"] = sanitized_sandbox

        return await _build_run_state_from_json(
            initial_agent=initial_agent,
            state_json=state_json,
            context_override=context_override,
            context_deserializer=context_deserializer,
            strict_context=strict_context,
            validation_error_factory=validation_error_factory,
        )
    except BaseException as error:
        trusted_error_message = _trusted_run_state_validation_message(
            error,
            trusted_validation_errors,
        )
        restore_error = _prepare_data_redacted_error(
            error,
            trusted_error_message=trusted_error_message,
        )
        trusted_validation_errors.clear()

    state_json = cast(Any, None)
    initial_agent = cast(Any, None)
    context_override = None
    context_deserializer = None
    assert restore_error is not None
    _raise_data_redacted_error(restore_error)