Skip to content

Commit 3f11ad2

Browse files
committed
fix: typing updates to pass make old_version_tests
1 parent 52a86f7 commit 3f11ad2

File tree

4 files changed

+712
-60
lines changed

4 files changed

+712
-60
lines changed

src/agents/run.py

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ def run_streamed(
19451945
else:
19461946
# input is already str | list[TResponseInputItem] when not RunState
19471947
# Reuse input_for_result variable from outer scope
1948-
input_for_result = cast(str | list[TResponseInputItem], input)
1948+
input_for_result = cast(Union[str, list[TResponseInputItem]], input)
19491949
context_wrapper = RunContextWrapper(context=context) # type: ignore
19501950
# input_for_state is the same as input_for_result here
19511951
input_for_state = input_for_result
@@ -3600,59 +3600,6 @@ async def _get_single_step_result_from_response(
36003600
run_config=run_config,
36013601
)
36023602

3603-
@classmethod
3604-
async def _get_single_step_result_from_streamed_response(
3605-
cls,
3606-
*,
3607-
agent: Agent[TContext],
3608-
all_tools: list[Tool],
3609-
streamed_result: RunResultStreaming,
3610-
new_response: ModelResponse,
3611-
output_schema: AgentOutputSchemaBase | None,
3612-
handoffs: list[Handoff],
3613-
hooks: RunHooks[TContext],
3614-
context_wrapper: RunContextWrapper[TContext],
3615-
run_config: RunConfig,
3616-
tool_use_tracker: AgentToolUseTracker,
3617-
) -> SingleStepResult:
3618-
original_input = streamed_result.input
3619-
# When resuming from a RunState, items from streamed_result.new_items were already saved
3620-
# to the session, so we should start with empty pre_step_items to avoid duplicates.
3621-
# pre_step_items should only include items from the current run.
3622-
pre_step_items: list[RunItem] = []
3623-
event_queue = streamed_result._event_queue
3624-
3625-
processed_response = RunImpl.process_model_response(
3626-
agent=agent,
3627-
all_tools=all_tools,
3628-
response=new_response,
3629-
output_schema=output_schema,
3630-
handoffs=handoffs,
3631-
)
3632-
new_items_processed_response = processed_response.new_items
3633-
tool_use_tracker.add_tool_use(agent, processed_response.tools_used)
3634-
RunImpl.stream_step_items_to_queue(new_items_processed_response, event_queue)
3635-
3636-
single_step_result = await RunImpl.execute_tools_and_side_effects(
3637-
agent=agent,
3638-
original_input=original_input,
3639-
pre_step_items=pre_step_items,
3640-
new_response=new_response,
3641-
processed_response=processed_response,
3642-
output_schema=output_schema,
3643-
hooks=hooks,
3644-
context_wrapper=context_wrapper,
3645-
run_config=run_config,
3646-
)
3647-
new_step_items = [
3648-
item
3649-
for item in single_step_result.new_step_items
3650-
if item not in new_items_processed_response
3651-
]
3652-
RunImpl.stream_step_items_to_queue(new_step_items, event_queue)
3653-
3654-
return single_step_result
3655-
36563603
@classmethod
36573604
async def _run_input_guardrails(
36583605
cls,

src/agents/run_state.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
from collections.abc import Mapping, Sequence
88
from dataclasses import dataclass, field
9-
from typing import TYPE_CHECKING, Any, Generic, cast
9+
from typing import TYPE_CHECKING, Any, Generic, Optional, cast
1010

1111
from openai.types.responses import (
1212
ResponseComputerToolCall,
@@ -741,7 +741,7 @@ def _serialize_item(self, item: RunItem) -> dict[str, Any]:
741741
def _convert_output_item_to_protocol(self, raw_item_dict: dict[str, Any]) -> dict[str, Any]:
742742
"""Convert API-format tool output items to protocol format."""
743743
converted = dict(raw_item_dict)
744-
call_id = cast(str | None, converted.get("call_id") or converted.get("callId"))
744+
call_id = cast(Optional[str], converted.get("call_id") or converted.get("callId"))
745745

746746
converted["type"] = "function_call_result"
747747

@@ -761,13 +761,13 @@ def _lookup_function_name(self, call_id: str) -> str:
761761
def _extract_name(raw: Any) -> str | None:
762762
candidate_call_id: str | None = None
763763
if isinstance(raw, dict):
764-
candidate_call_id = cast(str | None, raw.get("call_id") or raw.get("callId"))
764+
candidate_call_id = cast(Optional[str], raw.get("call_id") or raw.get("callId"))
765765
if candidate_call_id == call_id:
766766
name_value = raw.get("name", "")
767767
return str(name_value) if name_value else ""
768768
else:
769769
candidate_call_id = cast(
770-
str | None,
770+
Optional[str],
771771
getattr(raw, "call_id", None) or getattr(raw, "callId", None),
772772
)
773773
if candidate_call_id == call_id:
@@ -800,7 +800,7 @@ def _extract_name(raw: Any) -> str | None:
800800
if input_item.get("type") != "function_call":
801801
continue
802802
item_call_id = cast(
803-
str | None, input_item.get("call_id") or input_item.get("callId")
803+
Optional[str], input_item.get("call_id") or input_item.get("callId")
804804
)
805805
if item_call_id == call_id:
806806
name_value = input_item.get("name", "")

tests/fake_model.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ResponseContentPartAddedEvent,
1010
ResponseContentPartDoneEvent,
1111
ResponseCreatedEvent,
12+
ResponseCustomToolCall,
1213
ResponseFunctionCallArgumentsDeltaEvent,
1314
ResponseFunctionCallArgumentsDoneEvent,
1415
ResponseFunctionToolCall,
@@ -121,8 +122,26 @@ async def get_response(
121122
)
122123
raise output
123124

125+
# Convert apply_patch_call dicts to ResponseCustomToolCall
126+
# to avoid Pydantic validation errors
127+
converted_output = []
128+
for item in output:
129+
if isinstance(item, dict) and item.get("type") == "apply_patch_call":
130+
import json
131+
operation = item.get("operation", {})
132+
operation_json = json.dumps(operation) if isinstance(operation, dict) else str(operation)
133+
converted_item = ResponseCustomToolCall(
134+
type="custom_tool_call",
135+
name="apply_patch",
136+
call_id=item.get("call_id") or item.get("callId", ""),
137+
input=operation_json,
138+
)
139+
converted_output.append(converted_item)
140+
else:
141+
converted_output.append(item)
142+
124143
return ModelResponse(
125-
output=output,
144+
output=converted_output,
126145
usage=self.hardcoded_usage or Usage(),
127146
response_id="resp-789",
128147
)

0 commit comments

Comments
 (0)