Skip to content

Commit 79aa6e3

Browse files
fix(python): tighten failed-response provider types
1 parent ced7475 commit 79aa6e3

2 files changed

Lines changed: 80 additions & 61 deletions

File tree

‎python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import logging
1010
import os
1111
import re
12-
from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Generator, Mapping, Sequence
12+
from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Generator, Iterable, Mapping, Sequence
1313
from contextlib import AbstractAsyncContextManager, AsyncExitStack, aclosing, suppress
1414
from dataclasses import asdict, dataclass, is_dataclass
1515
from typing import Generic, Literal, TypeGuard, TypeVar, cast
@@ -35,7 +35,9 @@
3535
from agent_framework.exceptions import AgentFrameworkException
3636
from azure.ai.agentserver.core import get_request_context
3737
from azure.ai.agentserver.responses import (
38+
PlatformContext,
3839
ResponseContext,
40+
ResponseObject,
3941
ResponseProviderProtocol,
4042
ResponsesServerOptions,
4143
)
@@ -372,25 +374,17 @@ def consent_url_from_error(exc: BaseException) -> list[ConsentError] | None:
372374
# endregion Foundry Toolbox Auth integration
373375

374376

375-
def _response_field(response: Any, name: str) -> Any:
376-
"""Read a field from a mapping or attribute-bearing response envelope."""
377-
if isinstance(response, Mapping):
378-
return cast(Mapping[str, Any], response).get(name)
379-
return getattr(response, name, None)
380-
381-
382-
def _is_failed_stored_response(response: Any) -> bool:
377+
def _is_failed_stored_response(response: ResponseObject) -> bool:
383378
"""Return whether a persisted response envelope is a failed turn."""
384-
return _response_field(response, "status") == "failed"
379+
return response.get("status") == "failed"
385380

386381

387382
class _OmitFailedConversationInputProvider:
388383
"""Omit failed-turn input from the Responses chat-history store.
389384
390385
The agentserver orchestrator persists input items for every stored response,
391386
including ``status=failed``. Conversation history then replays those items on
392-
the next turn, which is the #7630 failure mode. Azure OpenAI does not keep
393-
failed input on the conversation.
387+
the next turn. Azure OpenAI does not keep failed input on the conversation.
394388
395389
For synchronous requests, the host knows the terminal status before the
396390
provider sees the initial create. Failed responses therefore omit their
@@ -407,22 +401,27 @@ def __init__(self, inner: ResponseProviderProtocol, failed_response_ids: set[str
407401

408402
async def create_response(
409403
self,
410-
response: Any,
411-
input_items: Any,
412-
history_item_ids: Any,
404+
response: ResponseObject,
405+
input_items: Iterable[OutputItem] | None,
406+
history_item_ids: Iterable[str] | None,
413407
*,
414-
context: Any = None,
408+
context: PlatformContext | None = None,
415409
) -> None:
416410
"""Persist ``response``, dropping input items when the turn failed."""
417-
response_id = _response_field(response, "id")
418-
known_failed = response_id is not None and str(response_id) in self._failed_response_ids
411+
response_id = response["id"]
412+
known_failed = response_id in self._failed_response_ids
419413
if _is_failed_stored_response(response) or known_failed:
420414
input_items = None
421415
await self._inner.create_response(response, input_items, history_item_ids, context=context)
422416
if known_failed:
423-
self._failed_response_ids.discard(str(response_id))
417+
self._failed_response_ids.discard(response_id)
424418

425-
async def update_response(self, response: Any, *, context: Any = None) -> None:
419+
async def update_response(
420+
self,
421+
response: ResponseObject,
422+
*,
423+
context: PlatformContext | None = None,
424+
) -> None:
426425
"""Update ``response`` without replacing the existing store entry."""
427426
await self._inner.update_response(response, context=context)
428427

@@ -649,7 +648,8 @@ async def _buffer_sync_response_events(
649648
except Exception as ex:
650649
handler_error = ex
651650
failed = handler_error is not None or any(
652-
_response_field(event, "type") == "response.failed" for event in buffered
651+
isinstance(event, Mapping) and cast(Mapping[str, object], event).get("type") == "response.failed"
652+
for event in buffered
653653
)
654654
if store and failed:
655655
self._failed_sync_response_ids.add(response_id)

‎python/packages/foundry_hosting/tests/test_responses.py‎

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
InMemoryResponseProvider,
5656
ResponseContext,
5757
ResponseExitForRecovery,
58+
ResponseObject,
5859
ResponsesServerOptions,
5960
)
6061
from azure.ai.agentserver.responses.aio import ResponseEventStream
@@ -1092,57 +1093,69 @@ def run_dispatch(*args: Any, **kwargs: Any) -> ResponseStream[AgentResponseUpdat
10921093
async def test_omit_failed_conversation_input_provider_drops_terminal_failed_input(self) -> None:
10931094
inner = InMemoryResponseProvider()
10941095
store = _OmitFailedConversationInputProvider(inner, set())
1095-
poison_item: dict[str, Any] = {
1096-
"id": "item_poison",
1097-
"type": "function_call_output",
1098-
"call_id": "call_12345abc",
1099-
"output": "example function call output",
1100-
"status": "completed",
1101-
}
1102-
ok_item: dict[str, Any] = {
1103-
"id": "item_ok",
1104-
"type": "message",
1105-
"role": "user",
1106-
"content": [{"type": "input_text", "text": "Hello, how are you?"}],
1107-
"status": "completed",
1108-
}
1096+
poison_item = cast(
1097+
OutputItem,
1098+
{
1099+
"id": "item_poison",
1100+
"type": "function_call_output",
1101+
"call_id": "call_12345abc",
1102+
"output": "example function call output",
1103+
"status": "completed",
1104+
},
1105+
)
1106+
ok_item = cast(
1107+
OutputItem,
1108+
{
1109+
"id": "item_ok",
1110+
"type": "message",
1111+
"role": "user",
1112+
"content": [{"type": "input_text", "text": "Hello, how are you?"}],
1113+
"status": "completed",
1114+
},
1115+
)
11091116

11101117
await store.create_response(
1111-
{"id": "resp_failed", "status": "failed", "conversation": "conv-1", "output": []},
1118+
cast(ResponseObject, {"id": "resp_failed", "status": "failed", "conversation": "conv-1", "output": []}),
11121119
[poison_item],
11131120
None,
11141121
)
11151122
await store.create_response(
1116-
{"id": "resp_ok", "status": "completed", "conversation": "conv-1", "output": []},
1123+
cast(ResponseObject, {"id": "resp_ok", "status": "completed", "conversation": "conv-1", "output": []}),
11171124
[ok_item],
11181125
None,
11191126
)
11201127

11211128
history_ids = await store.get_history_item_ids(None, "conv-1", 100)
11221129
assert "item_poison" not in history_ids
11231130
assert "item_ok" in history_ids
1124-
assert _is_failed_stored_response({"status": "failed", "conversation": "conv-1"})
1125-
assert not _is_failed_stored_response({"status": "completed", "conversation": "conv-1"})
1131+
assert _is_failed_stored_response(cast(ResponseObject, {"id": "resp_failed", "status": "failed"}))
1132+
assert not _is_failed_stored_response(cast(ResponseObject, {"id": "resp_ok", "status": "completed"}))
11261133

11271134
async def test_omit_failed_conversation_input_provider_updates_existing_response_in_place(self) -> None:
11281135
inner = InMemoryResponseProvider()
11291136
store = _OmitFailedConversationInputProvider(inner, set())
1130-
poison_item: dict[str, Any] = {
1131-
"id": "item_poison",
1132-
"type": "function_call_output",
1133-
"call_id": "call_12345abc",
1134-
"output": "example function call output",
1135-
"status": "completed",
1136-
}
1137-
in_progress: dict[str, Any] = {
1138-
"id": "resp_stream_fail",
1139-
"status": "in_progress",
1140-
"conversation": "conv-1",
1141-
"output": [],
1142-
}
1137+
poison_item = cast(
1138+
OutputItem,
1139+
{
1140+
"id": "item_poison",
1141+
"type": "function_call_output",
1142+
"call_id": "call_12345abc",
1143+
"output": "example function call output",
1144+
"status": "completed",
1145+
},
1146+
)
1147+
in_progress = cast(
1148+
ResponseObject,
1149+
{
1150+
"id": "resp_stream_fail",
1151+
"status": "in_progress",
1152+
"conversation": "conv-1",
1153+
"output": [],
1154+
},
1155+
)
11431156

11441157
await store.create_response(in_progress, [poison_item], None)
1145-
await store.update_response({**in_progress, "status": "failed"})
1158+
await store.update_response(cast(ResponseObject, {**in_progress, "status": "failed"}))
11461159

11471160
persisted = await inner.get_response("resp_stream_fail")
11481161
input_items = await inner.get_input_items("resp_stream_fail", ascending=True)
@@ -1153,16 +1166,22 @@ async def test_omit_failed_conversation_input_provider_drops_known_failed_initia
11531166
inner = InMemoryResponseProvider()
11541167
failed_response_ids = {"resp_failed"}
11551168
store = _OmitFailedConversationInputProvider(inner, failed_response_ids)
1156-
poison_item: dict[str, Any] = {
1157-
"id": "item_poison",
1158-
"type": "function_call_output",
1159-
"call_id": "call_12345abc",
1160-
"output": "example function call output",
1161-
"status": "completed",
1162-
}
1169+
poison_item = cast(
1170+
OutputItem,
1171+
{
1172+
"id": "item_poison",
1173+
"type": "function_call_output",
1174+
"call_id": "call_12345abc",
1175+
"output": "example function call output",
1176+
"status": "completed",
1177+
},
1178+
)
11631179

11641180
await store.create_response(
1165-
{"id": "resp_failed", "status": "in_progress", "conversation": "conv-1", "output": []},
1181+
cast(
1182+
ResponseObject,
1183+
{"id": "resp_failed", "status": "in_progress", "conversation": "conv-1", "output": []},
1184+
),
11661185
[poison_item],
11671186
None,
11681187
)

0 commit comments

Comments
 (0)