Skip to content

Commit 5e684a2

Browse files
anishmehta24claude
andcommitted
Python: mark hosted /responses incomplete on content_filter and length finish reasons
The hosted /responses path iterated each AgentResponseUpdate's contents but never read its finish_reason, so a turn the model stopped early (Azure OpenAI content filter, token limit) ended with status "completed" and no trace of the cut-off. Callers could only detect a filtered turn by matching the canned refusal text. _OutputItemTracker now records truncating finish reasons from both the plain-agent and workflow update loops, and _handle_response ends the response with response.incomplete plus incomplete_details.reason ("content_filter" / "max_output_tokens") instead of response.completed, mirroring the OpenAI Responses shape. The refusal text is still delivered as an output item. Fixes #8475 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 7232569 commit 5e684a2

2 files changed

Lines changed: 150 additions & 5 deletions

File tree

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
OutputItem,
6666
OutputItemReasoningItem,
6767
OutputMessageContent,
68+
ResponseIncompleteReason,
6869
ResponseStreamEvent,
6970
ResponseUsage,
7071
ResponseUsageInputTokensDetails,
@@ -797,8 +798,9 @@ async def _handle_prepared_response(
797798
for event in tracker.close():
798799
yield event
799800

800-
if tracker.oauth_consent_requested:
801-
yield response_event_stream.emit_incomplete(usage=tracker.usage)
801+
incomplete_reason = tracker.incomplete_reason
802+
if tracker.oauth_consent_requested or incomplete_reason is not None:
803+
yield response_event_stream.emit_incomplete(reason=incomplete_reason, usage=tracker.usage)
802804
else:
803805
yield response_event_stream.emit_completed(usage=tracker.usage)
804806
except Exception as ex:
@@ -964,6 +966,7 @@ async def _handle_inner_agent(
964966
)
965967
async with aclosing(agent_stream):
966968
async for update in agent_stream:
969+
tracker.record_finish_reason(update.finish_reason)
967970
for content in update.contents:
968971
async for event in tracker.handle(
969972
content, message_id=update.message_id, approval_storage=approval_storage
@@ -1187,6 +1190,7 @@ async def _handle_inner_workflow(
11871190
)
11881191
yield response_event_stream.checkpoint()
11891192

1193+
tracker.record_finish_reason(update.finish_reason)
11901194
for content in update.contents:
11911195
async for event in tracker.handle(
11921196
content, message_id=update.message_id, approval_storage=approval_storage
@@ -1291,6 +1295,10 @@ def __init__(self, stream: ResponseEventStream) -> None:
12911295
self._mcp_builder: OutputItemMcpCallBuilder | None = None
12921296
self._outstanding_function_calls: dict[str, str | None] = {}
12931297
self._oauth_consent_requests: set[tuple[str, str]] = set()
1298+
# Set when an agent update reports the model stopped early (content filter, token
1299+
# limit); the response then ends as ``incomplete`` instead of ``completed`` so callers
1300+
# can tell a cut-short turn from a successful one.
1301+
self._incomplete_reason: ResponseIncompleteReason | None = None
12941302
for item in stream.response.get("output", []):
12951303
if not isinstance(item, Mapping):
12961304
continue
@@ -1329,6 +1337,24 @@ def oauth_consent_requested(self) -> bool:
13291337
"""Return whether this response emitted an OAuth consent request."""
13301338
return bool(self._oauth_consent_requests)
13311339

1340+
@property
1341+
def incomplete_reason(self) -> ResponseIncompleteReason | None:
1342+
"""Return why the turn was cut short, if any update reported a truncating finish reason."""
1343+
return self._incomplete_reason
1344+
1345+
def record_finish_reason(self, finish_reason: str | None) -> None:
1346+
"""Note the finish reason of an agent update.
1347+
1348+
Only finish reasons that mean the model stopped early are retained, mapped onto the
1349+
Responses ``incomplete_details.reason`` vocabulary. A content filter is kept in
1350+
preference to a token limit if both are seen during a multi-step turn, since it is the
1351+
more actionable signal for the caller.
1352+
"""
1353+
if finish_reason == "content_filter":
1354+
self._incomplete_reason = ResponseIncompleteReason.CONTENT_FILTER
1355+
elif finish_reason == "length" and self._incomplete_reason is None:
1356+
self._incomplete_reason = ResponseIncompleteReason.MAX_OUTPUT_TOKENS
1357+
13321358
async def handle(
13331359
self,
13341360
content: Content,

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

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5262,6 +5262,122 @@ async def test_connect_time_consent_rejects_unsafe_links(self) -> None:
52625262
# region Error handling (response.failed surfacing)
52635263

52645264

5265+
class TestIncompleteFinishReasonSurfacing:
5266+
"""A turn the model stopped early must end as ``incomplete`` with the reason, not ``completed``.
5267+
5268+
Regression coverage for https://github.com/microsoft/agent-framework/issues/8475: the
5269+
underlying chat completion reported ``finish_reason="content_filter"`` but the hosted
5270+
``/responses`` payload said ``status="completed"`` with no trace of the filter.
5271+
"""
5272+
5273+
@staticmethod
5274+
def _filtered_agent(*, finish_reason: str) -> MagicMock:
5275+
return _make_agent(
5276+
stream_updates=[
5277+
AgentResponseUpdate(
5278+
contents=[Content.from_text("I'm sorry, but I cannot assist with that request.")],
5279+
role="assistant",
5280+
finish_reason=finish_reason, # type: ignore[arg-type]
5281+
)
5282+
]
5283+
)
5284+
5285+
async def test_non_streaming_content_filter_marks_response_incomplete(self) -> None:
5286+
server = _make_server(self._filtered_agent(finish_reason="content_filter"))
5287+
5288+
resp = await _post(server, input_text="hello", stream=False)
5289+
assert resp.status_code == 200
5290+
body = resp.json()
5291+
assert body["status"] == "incomplete"
5292+
assert body["incomplete_details"] == {"reason": "content_filter"}
5293+
assert body.get("error") is None
5294+
5295+
# The refusal text is still delivered so the caller can show it if it chooses to.
5296+
messages = [it for it in body["output"] if it["type"] == "message"]
5297+
assert len(messages) == 1
5298+
assert messages[0]["content"][0]["text"] == "I'm sorry, but I cannot assist with that request."
5299+
5300+
async def test_streaming_content_filter_emits_response_incomplete(self) -> None:
5301+
server = _make_server(self._filtered_agent(finish_reason="content_filter"))
5302+
5303+
resp = await _post(server, input_text="hello", stream=True)
5304+
assert resp.status_code == 200
5305+
events = _parse_sse_events(resp.text)
5306+
types = _sse_event_types(events)
5307+
5308+
assert types[-1] == "response.incomplete"
5309+
assert "response.completed" not in types
5310+
incomplete = events[-1]["data"]["response"]
5311+
assert incomplete["status"] == "incomplete"
5312+
assert incomplete["incomplete_details"] == {"reason": "content_filter"}
5313+
# The text item itself still closes normally before the terminal event.
5314+
assert "response.output_text.done" in types
5315+
5316+
async def test_length_finish_reason_maps_to_max_output_tokens(self) -> None:
5317+
server = _make_server(self._filtered_agent(finish_reason="length"))
5318+
5319+
resp = await _post(server, input_text="hello", stream=False)
5320+
body = resp.json()
5321+
assert body["status"] == "incomplete"
5322+
assert body["incomplete_details"] == {"reason": "max_output_tokens"}
5323+
5324+
async def test_normal_finish_reasons_still_complete(self) -> None:
5325+
agent = _make_agent(
5326+
stream_updates=[
5327+
AgentResponseUpdate(contents=[Content.from_text("part one")], role="assistant"),
5328+
AgentResponseUpdate(contents=[Content.from_text(" part two")], role="assistant", finish_reason="stop"),
5329+
]
5330+
)
5331+
server = _make_server(agent)
5332+
5333+
resp = await _post(server, input_text="hello", stream=False)
5334+
body = resp.json()
5335+
assert body["status"] == "completed"
5336+
assert body.get("incomplete_details") is None
5337+
5338+
async def test_content_filter_persists_across_later_updates_in_the_turn(self) -> None:
5339+
"""A filter mid-turn is not erased by a later update that finishes normally."""
5340+
agent = _make_agent(
5341+
stream_updates=[
5342+
AgentResponseUpdate(
5343+
contents=[Content.from_text("filtered")], role="assistant", finish_reason="content_filter"
5344+
),
5345+
AgentResponseUpdate(contents=[Content.from_text("trailing")], role="assistant", finish_reason="stop"),
5346+
]
5347+
)
5348+
server = _make_server(agent)
5349+
5350+
resp = await _post(server, input_text="hello", stream=False)
5351+
body = resp.json()
5352+
assert body["status"] == "incomplete"
5353+
assert body["incomplete_details"] == {"reason": "content_filter"}
5354+
5355+
async def test_content_filter_takes_precedence_over_length(self) -> None:
5356+
agent = _make_agent(
5357+
stream_updates=[
5358+
AgentResponseUpdate(contents=[Content.from_text("cut")], role="assistant", finish_reason="length"),
5359+
AgentResponseUpdate(
5360+
contents=[Content.from_text("filtered")], role="assistant", finish_reason="content_filter"
5361+
),
5362+
]
5363+
)
5364+
server = _make_server(agent)
5365+
5366+
resp = await _post(server, input_text="hello", stream=False)
5367+
body = resp.json()
5368+
assert body["incomplete_details"] == {"reason": "content_filter"}
5369+
5370+
async def test_workflow_agent_content_filter_marks_response_incomplete(self) -> None:
5371+
workflow_agent = _build_text_workflow_agent("filtered by workflow", finish_reason="content_filter")
5372+
server = _make_server(workflow_agent)
5373+
5374+
resp = await _post(server, input_text="hi", stream=False)
5375+
assert resp.status_code == 200
5376+
body = resp.json()
5377+
assert body["status"] == "incomplete"
5378+
assert body["incomplete_details"] == {"reason": "content_filter"}
5379+
5380+
52655381
class TestResponseFailedSurfacing:
52665382
"""Tests that exceptions raised by the hosted agent are converted into
52675383
terminal ``response.failed`` events carrying the exception message,
@@ -5600,15 +5716,16 @@ async def _iter() -> AsyncIterator[AgentResponseUpdate]:
56005716
return ResponseStream(_iter(), finalizer=AgentResponse.from_updates)
56015717

56025718

5603-
def _build_text_workflow_agent(text: str) -> WorkflowAgent:
5719+
def _build_text_workflow_agent(text: str, *, finish_reason: str | None = None) -> WorkflowAgent:
56045720
"""Build a minimal ``WorkflowAgent`` whose inner agent emits a fixed text."""
56055721

56065722
class _TextAgent(SupportsAgentRun):
5607-
def __init__(self, name: str, text: str) -> None:
5723+
def __init__(self, name: str, text: str, finish_reason: str | None) -> None:
56085724
self.id = str(uuid.uuid4())
56095725
self.name = name
56105726
self.description: str | None = None
56115727
self._text = text
5728+
self._finish_reason = finish_reason
56125729

56135730
def create_session(self, **kwargs: Any) -> AgentSession:
56145731
del kwargs
@@ -5652,17 +5769,19 @@ def run(
56525769
assert stream is True, "The inner agent only runs in stream mode in Foundry Hosted Agents."
56535770
text = self._text
56545771
name = self.name
5772+
finish_reason = self._finish_reason
56555773

56565774
async def _aiter() -> AsyncIterator[AgentResponseUpdate]:
56575775
yield AgentResponseUpdate(
56585776
contents=[Content.from_text(text=text)],
56595777
role="assistant",
56605778
author_name=name,
5779+
finish_reason=finish_reason, # type: ignore[arg-type]
56615780
)
56625781

56635782
return ResponseStream(_aiter(), finalizer=AgentResponse.from_updates)
56645783

5665-
inner = _TextAgent("text-agent", text)
5784+
inner = _TextAgent("text-agent", text, finish_reason)
56665785

56675786
@executor
56685787
async def start(messages: list[Message], ctx: WorkflowContext[AgentExecutorRequest]) -> None:

0 commit comments

Comments
 (0)